This project is a hands-on implementation of a basic Retrieval-Augmented Generation (RAG) pipeline using the LangChain framework.
The primary goal was to understand the core components of RAG by building each module from scratch: document loading, text chunking (including semantic chunking), embedding generation, vector storage, retrieval, and final answer generation using a Large Language Model (LLM).
- Multi-Format Document Loading: Loads documents from various formats (.txt, .pdf, .docx) within a specified directory.
- Semantic Chunking: Implements semantic chunking using LangChain's SemanticChunker and HuggingFaceEmbeddings to create contextually coherent text chunks. Includes an option to cap chunk size for compatibility with smaller LLMs.
- Embedding Generation: Uses a sentence-transformer model (all-MiniLM-L6-v2) via HuggingFaceEmbeddings to create vector representations of text chunks and queries.
- Vector Storage: Utilizes ChromaDB as a local, persistent vector store to save and index document embeddings for efficient similarity search.
- Custom Retrieval: Implements a retriever class that queries the ChromaDB vector store based on semantic similarity to the user's question.
- LLM Answer Generation: Uses a local LLM (via Ollama) or is compatible with the Hugging Face Interface API for generation, connected through LangChain (ChatOllama) to generate a final answer based on the retrieved context and the user's query. Includes a prompt template to guide the LLM's response.
- Interactive Q&A: Provides a command-line interface to ask questions about the loaded documents.
- Core Framework: LangChain (langchain, langchain_community, langchain_core, langchain_experimental, langchain-huggingface)
- LLM: Ollama (running models like phi3:mini or tinyllama locally), Hugging Face Interface API
- Embedding Model: sentence-transformers/all-MiniLM-L6-v2 (via langchain_huggingface)
- Vector Database: ChromaDB (chromadb)
- Document Loaders: PyMuPDF (pymupdf), Docx2txt (docx2txt), PyPDF (pypdf)
- Language: Python 3.9+