Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

RAG Chatbot

A functional Retrieval-Augmented Generation chatbot built with FastAPI and LangChain on the backend, and a React + Vite frontend. The app loads bootcamp documentation, creates three distinct chunking strategies, performs two retrieval methods, and returns all six answers together with labeled token usage metrics.

What this project does

  • Loads bootcamp docs from backend/data/aiseason-doc.txt
  • Uses three chunking strategies:
    • recursive
    • semantic
    • hierarchical
  • Uses two retrieval methods:
    • similarity (similarity score threshold)
    • mmr (maximum marginal relevance)
  • Executes all 6 combinations and returns every answer in one response
  • Labels each answer by chunking strategy and retrieval method
  • Compares each result by prompt_tokens, completion_tokens, and total_tokens

Core design

  • Embedding model: sentence-transformers/all-MiniLM-L6-v2
  • Embeddings created with HuggingFaceEmbeddings
  • Vector database: Chroma DB
  • Each chunking strategy is stored in its own Chroma collection:
    • recursive
    • semantic
    • hierarchical
  • No metadata filtering is required for retrieval; each collection contains only the chunks for its specific strategy. This makes retrieval cleaner, easier to debug, and avoids accidental cross-strategy issues.

Backend

The backend is built with FastAPI and LangChain.

  • backend/app/main.py
    • Defines the FastAPI app
    • Adds CORS middleware using FRONTEND_URL
    • Exposes /api/chat to execute all 6 RAG combinations
  • backend/app/api/chat.py
    • Runs retrieval and generation for each chunking/retrieval combination
    • Builds the prompt from retrieved context
    • Calls the LLM and records usage metadata
    • Returns results with chunking label, retrieval method, answer, and token counts
  • backend/app/api/models.py
    • Defines ChatRequest, Result, and ChatResponse

Chunking strategies

Chunking lives in backend/app/rag/chunking.py.

  • recursive_chunking
    • Splits text using RecursiveCharacterTextSplitter
    • Creates overlapping chunks for recursive splitting
  • semantic_chunking
    • Uses SemanticChunker with embeddings
    • Produces semantically coherent chunks
  • hierarchical_chunking
    • Builds a multi-level chunking structure with parent and child splits

Retrieval methods

Retrieval methods are implemented in backend/app/rag/retrieval_methods.py.

  • similarity_score_retriever
    • Uses Chroma similarity score threshold search
    • Falls back to standard top-k similarity search if needed
  • mmr_retrieval
    • Uses Maximum Marginal Relevance search

Both retrieval methods select the correct Chroma collection by chunking strategy.

Vector store

  • backend/app/rag/vectorstore.py
    • Creates a Chroma vector store for each collection
    • Uses collection_name to isolate each chunking strategy
  • backend/db/chroma_db/
    • Persists embeddings and collections locally

Ingestion pipeline

  • backend/app/rag/ingestion_pipeline.py
    • Loads documents from backend/data/
    • Converts and cleans text
    • Generates chunks for all three strategies
    • Saves each set of chunks into separate Chroma collections

Frontend

The frontend lives in frontend/.

  • Built with React and Vite
  • Designed for a modern single-page UI that queries /api/chat
  • Uses Axios to send the user question to the FastAPI backend
  • Displays all six answers together, clearly labeled by:
    • chunking strategy
    • retrieval method
  • Shows token metrics for each answer result

Folder structure

  • backend/
    • .env — environment configuration
    • requirements.txt — Python dependencies
    • app/
      • main.py — FastAPI app entrypoint
      • api/
        • chat.py — RAG execution logic
        • models.py — request/response schemas
      • rag/
        • chunking.py — three chunking strategies
        • retrieval_methods.py — similarity and MMR retrievers
        • vectorstore.py — Chroma collection helper
        • ingestion_pipeline.py — ingestion and collection creation
        • file_conversion.py — document cleaning utilities
    • data/ — bootcamp documentation source files
    • db/chroma_db/ — persisted Chroma collections
  • frontend/
    • package.json — frontend dependencies and scripts
    • src/ — React application source files
    • public/ — public static assets
    • vite.config.js — Vite configuration

Running the project

  1. Backend
cd backend
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
uvicorn app.main:app --reload
  1. Frontend
cd frontend
npm install
npm run dev
  1. Environment variables

Create backend/.env with at least:

FRONTEND_URL=http://localhost:5173
GROQ_API_KEY=your_groq_api_key_here

Result output

Each chat response returns all six combinations with labels and usage metrics:

  • chunking: recursive / semantic / hierarchical
  • retrieval: similarity / mmr
  • answer: generated response text
  • prompt_tokens, completion_tokens, total_tokens

This makes it easy to compare the performance and behavior of every chunking and retrieval combination side by side.

Notes

  • The project uses the embedding model sentence-transformers/all-MiniLM-L6-v2.
  • Three separate Chroma collections isolate strategy-specific chunks and avoid cross-strategy contamination.
  • The UI is built to show all six answers together so users can immediately compare outputs.

About

A RAG-based chatbot evaluation platform for analyzing multi-strategy document chunking and retrieval using performance metrics (tokens/accuracy) across six distinct pipeline configurations.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages