This project implements a Retrieval-Augmented Generation (RAG) system using LangChain, MongoDB Atlas Vector Search, and Ollama models. The system is capable of processing various document types and answering questions based on the content.
The project consists of two main components:
script.py: A production-ready implementation using LangChain and MongoDB Atlasproject.ipynb: A Jupyter notebook implementation using Llama.cpp and FAISS
This script implements a RAG system with the following features:
-
Document Processing:
- Supports multiple document formats: PDF, CSV, DOCX, and HTML
- Uses LangChain's document loaders for parsing
- Implements text chunking with RecursiveCharacterTextSplitter
-
Vector Storage:
- Uses MongoDB Atlas Vector Search for document storage and retrieval
- Implements efficient vector search with configurable parameters
- Supports async operations for better performance
-
Language Models:
- Uses Ollama for both embeddings and text generation
- Implements a structured prompt template for consistent responses
- Supports temperature control for response generation
-
Set up environment variables in
.env:MONGO_URI=your_mongodb_connection_string pdf_path=path_to_pdf csv_path=path_to_csv docx_path=path_to_docx html_path=path_to_html
-
Run the script:
python script.py
This Jupyter notebook implements a simpler RAG system using different technologies:
-
Document Processing:
- Uses simple text chunking (500 characters per chunk)
- Implements basic text file reading
-
Vector Storage:
- Uses FAISS for efficient vector similarity search
- Implements a simple chunk mapping system
-
Language Models:
- Uses Llama.cpp for text generation
- Uses SentenceTransformer for embeddings
- Implements a simple question-answering system
-
Install required packages:
pip install llama-cpp-python faiss-cpu sentence-transformers
-
Prepare your model:
- Download the Llama 2 model (llama-2-7b-chat.Q4_K_M.gguf)
- Place it in the project directory
-
Run the notebook:
- Open
project.ipynbin Jupyter - Execute cells in sequence
- Use the interactive question-answering interface
- Open
-
script.pyis more production-ready with:- Support for multiple document types
- MongoDB Atlas integration
- Async operations
- More robust error handling
-
project.ipynbis more experimental with:- Simpler implementation
- Local vector storage (FAISS)
- Interactive notebook interface
- Easier to modify and experiment with
All required packages are listed in requirements.txt. You can install them using:
pip install -r requirements.txtThe main dependencies include:
- python-dotenv - For environment variable management
- pymongo - For MongoDB integration
- langchain - Core LangChain functionality
- langchain-ollama - Ollama integration
- langchain-mongodb - MongoDB vector store integration
- langchain-community - Community integrations
- PyPDF2 - PDF processing
- python-docx - DOCX processing
- unstructured - HTML and other document processing
- docx2txt - Additional DOCX support
- faiss-cpu - For local vector similarity search
- sentence-transformers - For text embeddings
- numpy - Required for numerical operations
- llama-cpp-python - For local LLM inference
- torch - Required for transformers
- transformers - For model loading and inference
- Clone the repository
- Install dependencies:
pip install -r requirements.txt
- Set up environment variables in
.env - Download required models:
- For
script.py: No additional model downloads needed (uses Ollama) - For
project.ipynb: Download llama-2-7b-chat.Q4_K_M.gguf
- For
- Run either the script or notebook based on your needs