A document-based chatbot using Retrieval-Augmented Generation (RAG) built with Python, LangChain, FAISS, and OpenAI. Ask questions about your uploaded documents and get precise answers with source citations.
- Document Processing: Upload and index .txt, .pdf, .docx, .md files
- Smart Retrieval: Uses FAISS for fast vector similarity search
- Embedding Cache: Optimizes performance by caching embeddings
- Web Search Fallback: Optional web search when documents don't contain answers
- Source Citations: Shows which documents were used to generate answers
- Modular Architecture: Easy to extend or integrate into APIs
rag_chatbot/
βββ src/
β βββ components/
β β βββ chatbot/ # Query handling and response generation
β β βββ config/ # Settings and logging configuration
β β βββ ingestion/ # Document loading and processing
β β βββ retrieval/ # Vector storage, embeddings, web search
β β βββ prompts/ # LLM prompt templates
β βββ utils/ # Helper functions and exceptions
β βββ main.py # Application entry point
βββ data/
β βββ raw_docs/ # Place your documents here
β βββ db/ # Vector index and metadata storage
β βββ prompts/ # YAML prompt files
βββ logs/ # Application logs (if file logging enabled)
- Clone the repository
git clone https://github.com/yourusername/rag_chatbot.git
cd rag_chatbot- Install dependencies
pip install -r requirements.txt- Setup environment variables
cp .env.example .env
# Edit .env and add your OpenAI API key- Add your documents
# Place your documents in data/raw_docs/
# Supported formats: .pdf, .docx, .txt, .mdStart the chatbot:
python src/main.pyThe application will:
- Process documents from
data/raw_docs/ - Create vector embeddings (cached for performance)
- Start an interactive chat session
Example interaction:
π Ask me: What are the main topics covered in the documents?
π Response: Based on the documents, the main topics include...
π Sources (documents):
1. ../data/raw_docs/document1.pdf
2. ../data/raw_docs/document2.txt
Key settings in your .env file:
| Variable | Description | Required |
|---|---|---|
OPENAI_API_KEY |
OpenAI API key for embeddings and LLM | β |
SEARCH_API_KEY |
Google Custom Search API key | β |
SEARCH_ENGINE_ID |
Google Custom Search Engine ID | β |
LOG_LEVEL |
Logging level (DEBUG, INFO, WARNING, ERROR) | β |
IS_FILE_LOGGING_ENABLED |
Enable file-based logging | β |
- Embedding Cache: Automatically caches embeddings to avoid recomputation
- Web Search: Falls back to web search when documents don't contain answers
- Configurable Chunking: Adjust chunk size and overlap for optimal retrieval
- Source Attribution: Always shows which documents were used for answers
- Type
quit,exit, orbyeto exit the chatbot - Ctrl+C for immediate shutdown
The codebase uses a modular architecture:
- Document Processing: Handles loading and chunking of various file formats
- Vector Storage: FAISS-based similarity search with metadata
- Embedding Cache: Redis-like caching for embeddings
- Query Processing: LLM-based response generation with retrieval
Logs are written to:
- Console (colored output for development)
- Files in
logs/directory (whenIS_FILE_LOGGING_ENABLED=True)
No documents processed?
- Check that documents are in
data/raw_docs/ - Verify file formats are supported (.pdf, .docx, .txt, .md)
API errors?
- Ensure
OPENAI_API_KEYis set in.env - Check API quota and billing
Web search not working?
- Set
SEARCH_API_KEYandSEARCH_ENGINE_IDfor Google Custom Search - Or leave unset to use fallback search methods