A production-ready Retrieval-Augmented Generation (RAG) Chatbot built with:
- π Backend β FastAPI + LangChain + Pinecone (vector search) + SQLite (chat history) + OpenAI
- βοΈ Frontend β React 19 + Vite + Tailwind CSS
RAG_Chatbot/
βββ rag_chatbot/ # Python FastAPI backend
β βββ routes/ # API route handlers (chat, history, session, ingest)
β βββ middleware/ # Auth middleware
β βββ docs/ # Source documents for RAG ingestion
β βββ main.py # FastAPI app entry point
β βββ config.py # Configuration (reads .env)
β βββ vectorstore.py # Pinecone vector store logic + local BM25 fallback
β βββ embeddings.py # OpenAI embedding setup
β βββ memory.py # SQLite chat session management
β βββ ingest_docs.py # One-shot script to load docs into Pinecone
β βββ requirements.txt # Python dependencies
β βββ .env.example # Backend env template
β
βββ rag_frontend/ # React + Vite frontend
β βββ src/
β β βββ components/ # ChatWindow, Sidebar, MessageBubble
β β βββ App.jsx # Root component
β β βββ api.js # Axios API calls to backend
β β βββ index.css # Global styles
β βββ package.json
β βββ .env.example # Frontend env template
β
βββ .gitignore
βββ README.md
Make sure you have the following installed:
| Tool | Version | Download |
|---|---|---|
| Python | β₯ 3.10 | python.org |
| Node.js | β₯ 18.x | nodejs.org |
| Git | any | git-scm.com |
You will also need accounts / API keys for:
- Pinecone β pinecone.io (free tier works)
- OpenAI β platform.openai.com (needs an API Key with credits)
git clone https://github.com/Priyap1038/ChatBot.git
cd ChatBotcd rag_chatbot
# Windows
python -m venv venv
venv\Scripts\activate
# macOS / Linux
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txt# Copy the example file
copy .env.example .env # Windows
cp .env.example .env # macOS / LinuxNow open .env and fill in your values:
OPENAI_API_KEY=sk-... # Your OpenAI API key
PINECONE_API_KEY=pcsk_... # Your Pinecone API key
PINECONE_INDEX_NAME=priya-rag-index # Your Pinecone index name
CORS_ORIGINS=* # Use * for local dev
RATE_LIMIT=30/minute
LOG_LEVEL=INFO
β οΈ CRITICAL: Create your index in Pinecone with Dimensions = 1536 and Metric = cosine. This matches the default OpenAItext-embedding-3-smallmodel output.
This step uploads your documents in
docs/into the Pinecone vector store and builds the local BM25 search corpus (bm25_corpus.json). Run this once before starting the server (or whenever you physically add new files todocs/). The local search state survives restarts!
python ingest_docs.pyuvicorn main:app --reload --port 8000The API will be live at: http://localhost:8000
- Swagger UI: http://localhost:8000/docs
- Health check: http://localhost:8000/api/health
Open a new terminal and run:
cd rag_frontend
npm install# Windows
copy .env.example .env
# macOS / Linux
cp .env.example .envFor local development, the default .env values work out of the box (Vite proxies /api/* β localhost:8000):
VITE_API_URL= # Leave empty for local dev
VITE_API_KEY= # Leave empty unless backend API_KEY is setnpm run devThe app will be live at: http://localhost:5173
Open two terminals side-by-side:
| Terminal 1 β Backend | Terminal 2 β Frontend |
|---|---|
cd rag_chatbot |
cd rag_frontend |
venv\Scripts\activate |
npm install |
uvicorn main:app --reload |
npm run dev |
Then open http://localhost:5173 in your browser. π
- Place your
.md,.txt, or.pdffiles insiderag_chatbot/docs/ - Re-run the ingestion script:
cd rag_chatbot python ingest_docs.py - Restart the backend server.
| Method | Endpoint | Description |
|---|---|---|
GET |
/api/health |
Health check |
GET |
/api/sessions |
List all chat sessions |
POST |
/api/chat |
Send a message |
GET |
/api/history/{session_id} |
Get chat history for a session |
POST |
/api/session |
Create a new session |
POST |
/api/ingest |
Ingest a document via API |
Full interactive docs: http://localhost:8000/docs
cd rag_frontend
npm run buildOutput will be in rag_frontend/dist/. Serve it with any static host (Vercel, Netlify, etc.).
For the backend, update .env:
CORS_ORIGINS=https://yourfrontend.com
API_KEY=your-strong-secret-key| Problem | Fix |
|---|---|
ModuleNotFoundError |
Make sure your venv is activated and pip install -r requirements.txt was run |
Pinecone Dimension mismatch error |
Delete the index and recreate it with dimension = 1536 |
OpenAI 401 / Quota Exceeded |
Check your OpenAI billing page to ensure your key has credits |
| Empty responses / no context | Run python ingest_docs.py to populate Pinecone and the local database |
| CORS errors in browser | Ensure CORS_ORIGINS=* is set in backend .env during development |
| Frontend can't reach backend | Make sure backend is running on port 8000 and frontend on 5173 |
This project is for educational / internal use. Feel free to fork and adapt!