How sruthisree11/ncert-q-a-bot works
Purpose and Entry Points
This is a local, CLI-based RAG assistant for answering questions over NCERT textbook PDFs. It has two operational entry points:
• Offline corpus ingestion/index construction: src/build_vector_store.py
• Interactive retrieval and answer generation: src/qa_cli.py
Source Corpus and Persistent Knowledge Store
PDFs are the authoritative document corpus under data/pdfs/, currently including NCERT files such as data/pdfs/hesc106.pdf. The build stage converts this corpus into two coupled local artifacts:
• Serialized chunk metadata/text: data/chunks.pkl
• FAISS nearest-neighbor index: data/chunks_index.faiss
The index and pickle must remain aligned: FAISS retrieves vector positions, while the pickle provides the corresponding textual context.
Ingestion and Index-Build Pipeline
src/build_vector_store.py is the batch pipeline boundary between raw documents and retrievable knowledge. It:
1. Reads PDFs from data/pdfs/.
2. Extracts document text via a PDF library.
3. Splits text into semantic chunks.
4. Creates embeddings.
5. Persists chunks and the FAISS index to data/chunks.pkl and data/chunks_index.faiss.
Rebuilding the store is required when PDFs or chunking/embedding behavior changes.
Retrieval and Answering Pipeline
src/qa_cli.py is the runtime application. It accepts a natural-language question from the terminal, loads the local FAISS index and chunk collection, embeds the question, and retrieves the most relevant chunks. Retrieved passages become the grounding context for Gemini rather than being answered solely from the model’s general knowledge.
The control flow is: user question → query embedding → FAISS similarity search → retrieved chunk context → prompt assembly → Gemini response → terminal output.
Prompt Boundary
prompts/qa_prompt.txt defines the answer-generation prompt template. It is the principal policy/context boundary between deterministic local retrieval and the external generative model. Changes here can alter how retrieved textbook excerpts are presented, how strictly answers follow context, and the final response format without rebuilding the vector store.
External Services and Runtime Dependencies
The system runs on Python 3 with local FAISS CPU search and local persisted artifacts. Google Gemini is the external generation service, authenticated through GEMINI_API_KEY loaded from a root .env file. Relevant dependency expectations are declared in requirements.txt, including FAISS, PDF extraction, dotenv, and Google’s generative AI SDK.
Architectural Constraints
The repository is intentionally single-machine and file-backed: no web server, database, queue, or remote vector database is present. Index construction and question answering are separate commands, allowing query-time execution to avoid PDF parsing and index generation. Data quality depends on readable PDFs and successful extraction from data/pdfs/.
Main entry points: build_vector_store.py Build vector store · qa_cli.py QA CLI · chunks.pkl Chunk collection · requirements.txt Python dependencies
Components 16
Offline index build
Batch conversion of PDFs into aligned retrieval artifacts.
- Build vector store · Offline ingestion entry point.
src/build_vector_store.py - PDF text extraction
- Semantic chunking
- Document embeddings
Interactive QA
CLI retrieval and answer-generation flow.
- QA CLI · Runtime question-answering entry point.
src/qa_cli.py - Terminal user
- Query embedding
- FAISS similarity search
- QA prompt template · Controls grounding and answer format without rebuilding the index.
prompts/qa_prompt.txt - Grounded prompt assembly
Local files
Single-machine corpus and persistent knowledge store.
- NCERT PDFs · Authoritative textbook documents.
- Chunk collection · Text and metadata aligned to FAISS vector positions.
data/chunks.pkl - FAISS index
data/chunks_index.faiss
External service
- Google Gemini · External grounded answer generation.
- Gemini API key
Other components
- Python dependencies · Declares FAISS, PDF, dotenv, and Gemini SDK dependencies.
requirements.txt
Connections 18
- NCERT PDFs → Build vector store: reads
- Build vector store → PDF text extraction: extracts
- PDF text extraction → Semantic chunking: text
- Semantic chunking → Document embeddings: chunks
- Semantic chunking → Chunk collection: persists text and metadata
- Document embeddings → FAISS index: builds vectors
- Terminal user → QA CLI: question
- QA CLI → Query embedding: embeds query
- Query embedding → FAISS similarity search: query vector
- FAISS index → FAISS similarity search: loads and searches
- FAISS similarity search → Chunk collection: retrieved positions
- Chunk collection → Grounded prompt assembly: retrieved passages
- QA prompt template → Grounded prompt assembly: template
- Grounded prompt assembly → Google Gemini: grounded prompt
- Gemini API key → Google Gemini: authenticates
- Google Gemini → Terminal user: answer
- Python dependencies → Build vector store: runtime packages
- Python dependencies → QA CLI: runtime packages