This repository contains implementations of two Retrieval-Augmented Generation (RAG) systems using the Cohere API. The project provides both a simple RAG implementation and an enhanced contextual RAG implementation to demonstrate different approaches and performance characteristics.
The project implements two RAG architectures:
-
Simple RAG 🔍: An implementation following the basic RAG pattern - document loading, chunking, embedding, retrieval, and generation.
-
Contextual RAG 🧠: A more sophisticated implementation with additional features:
- Context generation for document chunks
- Query reformulation based on conversation history
- Enhanced document retrieval with contextual awareness
Both systems leverage Cohere's API for embeddings and language models, and process PDF documents as knowledge sources.
-
Clone the repository:
git clone https://github.com/your-username/rag-system-cohere.git cd rag-system-cohere -
Create a virtual environment and install dependencies:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -r requirements.txt
-
Create a
.envfile in the root directory with your Cohere API key:COHERE_API_KEY=your_cohere_api_key_here
API keys can be obtained by signing up at cohere.ai.
The system can be used in two modes: single query mode or interactive mode.
# Single query mode
python app.py simple --pdf_path path/to/your/document.pdf --query "Your question here"
# Interactive mode
python app.py simple --pdf_path path/to/your/document.pdf --interactive
# Process a directory of PDFs
python app.py simple --pdf_path path/to/pdf/directory --interactive# Single query mode
python app.py contextual --pdf_path path/to/your/document.pdf --query "Your question here"
# Interactive mode
python app.py contextual --pdf_path path/to/your/document.pdf --interactive
# Process a directory of PDFs
python app.py contextual --pdf_path path/to/pdf/directory --interactiveBoth implementations can be tested to determine which best fits specific use cases.
The included Jupyter notebook comparison.ipynb provides a detailed comparison between the Simple and Contextual RAG implementations. The notebook:
- Loads identical document sets into both systems
- Executes benchmark queries through both systems
- Compares response quality and relevance
- Analyzes performance differences between approaches
- Visualizes results for comparative analysis
To run the notebook:
jupyter notebook comparison.ipynb- Document processing pipeline with PyPDF for loading PDFs
- Text chunking with RecursiveCharacterTextSplitter
- Vector storage with Chroma
- Basic retrieval using similarity search
- Answer generation using retrieved context
- Enhanced document processing with context generation
- Context summaries created for each document chunk
- Conversation-aware query reformulation
- Improved retrieval with contextual information
- Rate limiting to manage API usage
Both systems process PDF documents by:
- Loading the PDF using PyPDFLoader
- Splitting the text into manageable chunks
- Storing chunks in a vector database (Chroma)
The Contextual RAG system adds an additional step of generating contextual summaries for each chunk using Cohere's API.
- Simple RAG: Directly uses the user's query for retrieval
- Contextual RAG: Reformulates queries based on conversation history for improved context awareness
The config.py file contains configurable parameters:
- API keys
- Vector database settings
- Text chunking parameters
- Default retrieval settings
- The Contextual RAG system makes more API calls and has higher latency due to the additional context generation step.
- The Simple RAG system is faster but may lack contextual awareness in multi-turn conversations.
- Rate limiting has been implemented in the Contextual RAG system to avoid API throttling.
The two RAG systems offer different trade-offs:
- Simple RAG 🔍: Provides a lightweight, faster implementation suitable for straightforward question-answering tasks.
- Contextual RAG 🧠: Delivers more contextually relevant answers, especially in multi-turn conversations, at the cost of increased latency and API usage.
Selection between implementations depends on specific use case requirements regarding response quality, conversation handling, and performance constraints.
Potential enhancements:
- 📚 Integration with additional document types (beyond PDFs)
- 🔄 Implementation of hybrid retrieval techniques
- ⚡ Caching mechanisms to reduce API calls
- 🔧 Fine-tuning of chunking and retrieval parameters
- 🖥️ UI implementation for improved user interaction
Contributions welcome for any of these enhancements or other improvements.