Chat with your PDF (RAG)
Upload notes or a textbook PDF and ask questions; answers come only from the document, with page citations.
What you'll learn
- âRetrieval-augmented generation (RAG)
- âChunking and embeddings
- âGrounding answers and citing sources
What you'll build
- âPDF upload
- âAnswers with page references
- âSays "not in the document" when unsure
- âChat history
Step-by-step guide
- 1
Extract text by page
pypdf: keep (page_number, text) pairs.
- 2
Chunk
Split into ~500-word chunks with 50-word overlap; keep the page number on each chunk.
- 3
Embed
Create embeddings with an embedding model (from your LLM provider or sentence-transformers) and store them in a vector store (Chroma, FAISS or pgvector).
- 4
Retrieve
Embed the question and fetch the top 4 similar chunks.
- 5
Prompt
Send the chunks plus the question to an LLM API (for example Claude, GPT or Gemini) with instructions: answer only from the context, cite pages, say if the answer is not there.
- 6
UI
Streamlit chat (st.chat_input, st.chat_message) showing cited pages under each answer.
- 7
Evaluate
Write 20 questions with known answers; check accuracy and whether citations are correct. Keep the API key in an environment variable, never in code.
đ Make it stand out
- +Multiple documents
- +Hybrid search (keyword + vector)
- +Show the source passage highlighted