Skip to content

amanparganiha/PdfRagAgent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– AI Agent β€” Chat with your PDFs

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.

Python OpenAI Streamlit License


🌐 Live Demo

Try it here: pdfragagent.streamlit.app

Open in Streamlit

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.

πŸ“Έ Screenshot

App screenshot

Placeholder β€” replace docs/screenshot.png with an actual screenshot of your deployed app.


✨ Features

  • πŸ“„ 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.

πŸ—οΈ Architecture

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:

  1. PDFs are extracted β†’ split into overlapping chunks (~800 chars)
  2. Each chunk is embedded using text-embedding-3-small
  3. On query, the user's question is embedded and compared via cosine similarity
  4. Top 5 matching chunks are sent to GPT-4o as context
  5. The model generates an answer citing the source PDF

πŸš€ Quick Start

1. Clone the repo

git clone https://github.com/amanparganiha/PdfRagAgent.git
cd PdfRagAgent

2. Install dependencies

pip install -r requirements.txt

3. Set up your API key

cp .env.example .env

Open .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

4. Add your PDFs

Drop PDF files into the pdfs/ folder (created automatically on first run):

your-project/
β”œβ”€β”€ chatbot.py
β”œβ”€β”€ pdfs/
β”‚   β”œβ”€β”€ research_paper.pdf
β”‚   └── company_report.pdf
└── ...

5. Run

Web app (recommended):

streamlit run app.py

Opens 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

πŸ’¬ Usage

Chat normally

πŸ§‘ You: What is the main conclusion of the research paper?
πŸ€– Agent: According to research_paper.pdf, the main conclusion is...

Ask about the web

πŸ§‘ You: What's the latest news about OpenAI?
πŸ€– Agent: πŸ” Searching web...

Load a PDF mid-conversation

πŸ§‘ You: /load C:\Users\me\Documents\notes.pdf
πŸ“„ Processing: notes.pdf
   βœ‚οΈ Split into 24 chunks
   πŸ“Š Embedded 24/24 chunks
   βœ… Done!

Slash Commands

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

βš™οΈ Configuration

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.


πŸ“ Project Structure

.
β”œβ”€β”€ 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 the memory/ folder, since cloud hosts have ephemeral disks. The pdfs/ and memory/ folders are used by the CLI version.


πŸ’° Cost Estimate

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.


πŸ›‘οΈ Security

  • API keys are stored in .env and never committed to git
  • .gitignore excludes .env, memory/, and pdfs/
  • 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.


🀝 Contributing

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

πŸ“„ License

MIT License β€” use it however you want.

About

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.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages