A standalone RAG (Retrieval-Augmented Generation) chatbot service built with FastAPI and LangChain. Answers questions from a shared knowledge base built from exam notes — currently integrated with the AI Exam Notes Generator project, but designed to support any project via isolated knowledge bases.
Live URL: https://rag-chatbot-service-1poi.onrender.com
- Notes are ingested (from MongoDB backfill or direct API call) → chunked → embedded → stored in Qdrant Cloud
- On a chat request, the question is embedded → most relevant chunks retrieved from Qdrant → passed as context to Google Generative AI → answer returned with source attribution
- Each project gets its own isolated Qdrant collection via API key →
project_idmapping — data never crosses between projects
| Layer | Choice |
|---|---|
| API | FastAPI |
| RAG | LangChain (plain LCEL) |
| Embeddings | gemini-embedding-001 via Google Generative AI |
| LLM | gemini-2.5-flash via Google Generative AI |
| Vector store | Qdrant Cloud (free tier, AWS Oregon) |
| Source DB | MongoDB (read-only, for backfill) |
| Hosting | Render (free tier, Oregon) |
Every request requires an X-API-Key header. Keys are mapped to a project_id server-side — the caller never needs to know or specify the project ID directly.
X-API-Key: your-secret-key-here
Missing or invalid key returns 401 Unauthorized. Contact the repo owner to get a key issued for your project.
Liveness check. No auth required.
Response
{
"status": "ok",
"environment": "production"
}Ingest a single note into the knowledge base. Called automatically by the Express backend after a note is generated.
Headers
X-API-Key: your-secret-key-here
Content-Type: application/json
Request body
{
"note_id": "mongo-document-id",
"owner_id": "user-id",
"title": "Newton's Laws of Motion",
"text": "full plain text content of the note"
}Response
{
"status": "ok",
"chunks_indexed": 4
}Ingest a user-uploaded PDF into the knowledge base.
Headers
X-API-Key: your-secret-key-here
Request body
multipart/form-data with a single field file containing the PDF.
Response
{
"status": "ok",
"chunks_indexed": 7
}Ask a question against the knowledge base.
Headers
X-API-Key: your-secret-key-here
Content-Type: application/json
Request body
{
"question": "What are Newton's laws of motion?"
}Response
{
"answer": "Newton's Laws of Motion are fundamental principles...",
"sources": [
"Netwons Laws of motion",
"Newton laws motion",
"Newtons Laws of motion"
]
}The sources array lists the note titles whose chunks were retrieved to generate the answer. If the knowledge base doesn't contain relevant information, the model says so rather than hallucinating.
The primary integration. The knowledge base (exam_notes_generator collection in Qdrant) is seeded from all notes stored in the project's MongoDB database via a one-time backfill script (scripts/backfill_from_mongo.py). Going forward, new notes are auto-ingested by the Express backend calling POST /ingest/note after each generation.
Currently indexed: 30 notes, 168 chunks
This service is built to be reusable. One deployed instance can serve multiple completely isolated knowledge bases — each project's data lives in its own Qdrant collection and is never mixed with others.
To add a new project:
- Generate a new random API key:
python -c "import secrets; print(secrets.token_hex(32))"- Add it to
API_KEYS_JSONin Render's environment variables:
{
"existing-key": "exam_notes_generator",
"new-project-key": "your_new_project_id"
}-
The new project's Qdrant collection is created automatically on the first
/ingest/noteor/ingest/uploadcall — no manual setup needed. -
If the new project has a different MongoDB schema, flatten the content into plain text in the calling backend before sending to
/ingest/note. The service itself is schema-agnostic.
git clone https://github.com/Shivansh0047/rag-chatbot-service
cd rag-chatbot-service
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env # fill in real values
uvicorn app.main:app --reloadVisit http://localhost:8000/docs for interactive API docs.
| Variable | Description |
|---|---|
QDRANT_URL |
Qdrant Cloud cluster URL |
QDRANT_API_KEY |
Qdrant Cloud API key |
GOOGLE_API_TOKEN |
Google API token (for embeddings + LLM) |
MONGO_URI |
MongoDB connection string (for backfill) |
MONGO_DB_NAME |
MongoDB database name |
MONGO_NOTES_COLLECTION |
MongoDB collection name |
API_KEYS_JSON |
JSON map of {"api_key": "project_id"} |
python -m scripts.backfill_from_mongoapp/
├── main.py # FastAPI app entrypoint
├── config.py # all env vars in one place
├── auth.py # API key → project_id resolution
├── models.py # request/response schemas
├── routes/
│ ├── ingest.py # POST /ingest/note, POST /ingest/upload
│ └── chat.py # POST /chat
├── rag/
│ ├── embeddings.py # Google Generative AI
│ ├── llm.py # Gemini via Google
│ ├── vectorstore.py # Qdrant client, per-project collections
│ ├── splitter.py # text chunking
│ └── chain.py # retrieve → prompt → generate
└── ingestion/
├── mongo_reader.py # reads notes from MongoDB
└── pdf_extract.py # extracts text from uploaded PDFs
scripts/
└── backfill_from_mongo.py # one-time knowledge base seeding