Basic RAG Chain
A basic RAG (Retrieval-Augmented Generation) chain demonstrates the fundamental pattern of retrieving relevant documents and using them to generate contextually accurate responses.Overview
This example shows how to build a complete RAG system called PharmaQuery - a pharmaceutical insight retrieval system that helps users gain meaningful insights from research papers.Document Loading
Load and process PDF documents with PyPDFLoader
Text Splitting
Split documents into chunks using SentenceTransformers
Vector Storage
Store embeddings in ChromaDB for fast retrieval
Query Processing
Retrieve relevant chunks and generate answers with Gemini
Architecture
Implementation
Installation
1
Install dependencies
2
Set up API keys
Complete Code
- app.py
- requirements.txt
Key Components
Document Loading
PyPDFLoader extracts text while preserving document structure and metadata.
Text Splitting
Vector Storage
Retrieval & Generation
Usage
1
Run the application
2
Enter API key
Paste your Google API Key in the sidebar
3
Upload documents
Upload research papers (PDFs) to build your knowledge base
4
Query the system
Ask questions about the uploaded documents
Example Queries
Pharmaceutical Research
Pharmaceutical Research
- “What are the clinical trial results for this drug?”
- “Summarize the methodology used in this study”
- “What safety concerns were identified?”
- “Compare efficacy across different patient groups”
Technical Questions
Technical Questions
- “What statistical methods were used?”
- “What were the inclusion/exclusion criteria?”
- “How was the sample size determined?”
- “What were the limitations of this study?”
Customization Options
Embedding Models
- Google Gemini
- OpenAI
- HuggingFace (Local)
Vector Databases
- ChromaDB
- Qdrant
- FAISS
Best Practices
Chunk Size Selection
Chunk Size Selection
- Small chunks (256-512 tokens): Better precision, more retrievals needed
- Medium chunks (512-1024 tokens): Balanced approach (recommended)
- Large chunks (1024-2048 tokens): More context, but less precise
Overlap Strategy
Overlap Strategy
- Use 10-20% overlap to maintain context across chunks
- Too much overlap increases storage and redundancy
- Too little overlap may lose important context at boundaries
Retrieval Parameters
Retrieval Parameters
Troubleshooting
Empty or irrelevant responses
Empty or irrelevant responses
Solutions:
- Increase
kvalue to retrieve more documents - Lower
score_thresholdto allow less similar matches - Improve document chunking strategy
- Use query expansion or reformulation
Slow performance
Slow performance
Solutions:
- Reduce chunk size to decrease embedding time
- Use a faster embedding model
- Enable vector store caching
- Consider using a more efficient vector database
High memory usage
High memory usage
Solutions:
- Process documents in batches
- Use a disk-based vector store (ChromaDB, Qdrant)
- Reduce embedding dimensions if possible
- Clean up temporary files after processing
Related Examples
Agentic RAG
Add reasoning and self-correction to your RAG system
Corrective RAG
Implement self-evaluating retrieval with fallback strategies
Hybrid Search
Combine vector search with keyword search for better results
Local RAG
Build a privacy-focused RAG system with local models
