A highly-extensible, production-ready Retrieval-Augmented Generation (RAG) API & Chatbot for your Markdown knowledge base.
MD ChatBot is a modern, full-stack chatbot application designed to instantly retrieve and synthesize information from your local Markdown (.md) files.
Built with enterprise-grade architecture, it seamlessly blends a lightweight frontend interface with a robust, highly-optimized FastAPI backend. Under the hood, it leverages LangChain and ChromaDB to execute advanced RAG (Retrieval-Augmented Generation) patterns—ensuring accurate, context-aware, and hallucination-free responses based strictly on your uploaded data.
🚀 High-Performance API
- Built on FastAPI for blazing-fast response times, automatic Swagger documentation (
/docs), and native async support. - Fully configured CORS middleware for effortless frontend integration.
🧠 Advanced RAG Pipeline
- Pluggable LLM Providers: Effortlessly switch between OpenAI (GPT-4o-mini), Google Gemini (2.5 Flash), or entirely Local Open-Source Models (via HuggingFace).
- Intelligent Chunking: Uses
RecursiveCharacterTextSplitterto optimally segment Markdown files while preserving context. - MMR Search: Employs Maximum Marginal Relevance (MMR) retrieval to ensure diverse and relevant document fetching.
- LLM-Based Reranking: Dynamically reranks retrieved context using the LLM to surface the most critical information to the top.
- Context Compression: Eliminates duplicate context to maximize prompt efficiency and reduce token costs.
📂 Seamless Document Management
- Instant Ingestion: Upload
.mdfiles directly via the UI or API endpoint (/upload) and trigger real-time vectorization. - Smart Idempotency: Hashes document chunks to prevent duplicate vector store entries, ensuring a clean and efficient
Chromadatabase.
| Layer | Technology | Description |
|---|---|---|
| Frontend | HTML / Vanilla CSS / JS | Lightweight, static frontend served directly by FastAPI. |
| Backend API | FastAPI | Async Python framework routing requests and managing file uploads. |
| Orchestration | LangChain | Manages prompt building, document loaders, splitting, and LLM chains. |
| Vector Database | ChromaDB | Local, persistent vector store for fast embedding retrieval. |
| Embeddings/LLM | OpenAI / Gemini / HF | Flexible integration for state-of-the-art generative AI models. |
- Python 3.10 or higher
- An API key for your chosen provider (OpenAI or Google Gemini)
Clone the repository and install the required dependencies:
git clone https://github.com/your-org/md-chatbot.git
cd md-chatbot
pip install -r requirements.txtCreate a .env file in the root directory and add your API keys:
# Example .env file
OPENAI_API_KEY="your-openai-api-key"
GEMINI_API_KEY="your-google-gemini-api-key"Configure your provider preference directly in rag.py:
# rag.py configuration block
USE_PROVIDER = "gemini" # Options: "openai", "gemini", "local"Start the FastAPI server using Uvicorn:
uvicorn main:app --reloadThe application will now be available at:
- Web UI:
http://localhost:8000/ - API Documentation (Swagger):
http://localhost:8000/docs
| Method | Endpoint | Description |
|---|---|---|
GET |
/ |
Serves the interactive Web UI. |
POST |
/upload |
Accepts a .md file, saves it to the data/ directory, and triggers the LangChain ingestion pipeline. |
POST |
/ask |
Accepts a JSON payload { "query": "string" }, runs the RAG pipeline, and returns the LLM-generated answer. |
- Ingestion (
/upload): When a user uploads a Markdown file,rag.pyloads it usingDirectoryLoader, splits it into 800-character chunks (with 150-character overlap), generates an MD5 hash for idempotency, embeds the text, and stores it locally in ChromaDB. - Retrieval (
/ask): Upon receiving a query, the system vectorizes the question, performs an MMR search in ChromaDB to fetch the top 12 chunks, and then uses a custom LLM prompt to rerank those chunks down to the top 5 most relevant pieces of context. - Generation: The compressed, highly-relevant context is injected into a strict prompt template instructing the LLM to answer only based on the provided text, mitigating hallucination risks.