A conversational AI agent powered by OpenAI's GPT-4o that can read your PDF documents and answer questions about them using RAG (Retrieval-Augmented Generation). Also supports web search and persistent memory across sessions.
Try it here: pdfragagent.streamlit.app
Bring your own key: the live demo doesn't ship an API key β paste your own OpenAI key in the sidebar (it's used only for your session and never stored).
Upload a PDF in the sidebar, then ask questions about it β or ask anything and let the agent search the web.
Deploying for the first time? See DEPLOY.md for a 5-minute Streamlit Cloud walkthrough.
Placeholder β replace
docs/screenshot.pngwith an actual screenshot of your deployed app.
- π PDF Question Answering (RAG) β Upload PDFs and ask natural language questions. The agent finds the most relevant sections and generates accurate answers with source citations.
- π Web Search β Searches the web via DuckDuckGo when your documents don't have the answer. No extra API key required.
- π§ Persistent Memory β Conversation history is saved to disk. Restart the app and it picks up right where you left off.
- β‘ Smart Caching β PDF embeddings are cached locally. Load a file once and it's instantly available on every restart.
- π οΈ Function Calling β The agent automatically decides whether to search your documents, the web, or just reply from memory. You don't have to tell it what to do.
User Question
β
βΌ
ββββββββββββ
β GPT-4o β ββββ decides which tool to use
ββββββββββββ
β
ββββΆ search_documents βββΆ Embedding similarity search over PDF chunks
β
ββββΆ web_search βββΆ DuckDuckGo real-time results
β
βΌ
Final Answer (with sources)
RAG Pipeline:
- PDFs are extracted β split into overlapping chunks (~800 chars)
- Each chunk is embedded using
text-embedding-3-small - On query, the user's question is embedded and compared via cosine similarity
- Top 5 matching chunks are sent to GPT-4o as context
- The model generates an answer citing the source PDF
git clone https://github.com/amanparganiha/PdfRagAgent.git
cd PdfRagAgentpip install -r requirements.txtcp .env.example .envOpen .env and paste your OpenAI API key:
OPENAI_API_KEY=sk-proj-your-actual-key-here
MODEL=gpt-4o
Get your key at platform.openai.com/api-keys
Drop PDF files into the pdfs/ folder (created automatically on first run):
your-project/
βββ chatbot.py
βββ pdfs/
β βββ research_paper.pdf
β βββ company_report.pdf
βββ ...
Web app (recommended):
streamlit run app.pyOpens at http://localhost:8501 β upload PDFs and chat in the browser. This is also what gets deployed (see DEPLOY.md).
Terminal version:
python chatbot.pyπ§ You: What is the main conclusion of the research paper?
π€ Agent: According to research_paper.pdf, the main conclusion is...
π§ You: What's the latest news about OpenAI?
π€ Agent: π Searching web...
π§ You: /load C:\Users\me\Documents\notes.pdf
π Processing: notes.pdf
βοΈ Split into 24 chunks
π Embedded 24/24 chunks
β
Done!
| Command | Description |
|---|---|
/load <path> |
Load a PDF file into the agent |
/docs |
List all loaded documents and chunk counts |
/clear |
Reset conversation memory |
/history |
View last 10 messages |
quit |
Exit the agent |
All settings are in the top section of chatbot.py:
| Setting | Default | Description |
|---|---|---|
EMBEDDING_MODEL |
text-embedding-3-small |
OpenAI embedding model |
CHUNK_SIZE |
800 |
Characters per text chunk |
CHUNK_OVERLAP |
200 |
Overlap between chunks for context continuity |
TOP_K |
5 |
Number of chunks retrieved per query |
MAX_HISTORY |
50 |
Max messages kept in conversation memory |
You can also change MODEL in your .env file to use gpt-4o-mini or gpt-3.5-turbo for cheaper usage.
.
βββ app.py # Streamlit web UI (deployed version)
βββ chatbot.py # Terminal/CLI version
βββ requirements.txt # Python dependencies
βββ DEPLOY.md # Deployment guide (Streamlit Cloud, etc.)
βββ .env.example # Template for API keys (CLI / local)
βββ .env # Your actual keys (git-ignored)
βββ .gitignore # Keeps secrets & caches out of git
βββ .streamlit/
β βββ secrets.toml.example # Key template for Streamlit (git-ignored when real)
βββ docs/
β βββ screenshot.png # App screenshot for the README
βββ pdfs/ # Drop your PDF files here, CLI only (git-ignored)
βββ memory/ # CLI persistence (git-ignored)
βββ chat_history.json # Conversation memory
βββ embeddings/ # Cached PDF embeddings
Note: The web app (
app.py) keeps chat and embeddings in the browser session rather than thememory/folder, since cloud hosts have ephemeral disks. Thepdfs/andmemory/folders are used by the CLI version.
| Action | Model | Approx. Cost |
|---|---|---|
| Embed a 50-page PDF | text-embedding-3-small |
~$0.002 |
| One chat message | gpt-4o |
~$0.01β0.03 |
| One chat message | gpt-4o-mini |
~$0.001 |
| Web search | DuckDuckGo | Free |
Embeddings are cached, so you only pay once per PDF.
- API keys are stored in
.envand never committed to git .gitignoreexcludes.env,memory/, andpdfs/- No data is sent anywhere except OpenAI's API
β οΈ Never hardcode your API key in the source code. If you accidentally push a key to GitHub, revoke it immediately at platform.openai.com/api-keys.
Pull requests are welcome! Some ideas for improvements:
- Support for more file types (Word, TXT, CSV)
- Streaming responses
- Web UI with Streamlit or Gradio
- Multiple conversation threads
- Summarize entire documents
- Token usage tracking
MIT License β use it however you want.
