Skip to main content

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

Get your API key from Google AI Studio

Complete Code

Key Components

Document Loading

PyPDFLoader extracts text while preserving document structure and metadata.

Text Splitting

Use token-based splitting with SentenceTransformers for better semantic coherence compared to character-based 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

  • “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”
  • “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

Vector Databases

Best Practices

  • 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
Adjust based on your document type and query patterns.
  • 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

Troubleshooting

Solutions:
  • Increase k value to retrieve more documents
  • Lower score_threshold to allow less similar matches
  • Improve document chunking strategy
  • Use query expansion or reformulation
Solutions:
  • Reduce chunk size to decrease embedding time
  • Use a faster embedding model
  • Enable vector store caching
  • Consider using a more efficient vector database
Solutions:
  • Process documents in batches
  • Use a disk-based vector store (ChromaDB, Qdrant)
  • Reduce embedding dimensions if possible
  • Clean up temporary files after processing

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