A lightweight Retrieval-Augmented Generation (RAG) pipeline that answers user questions from a local knowledge base with grounded answers and citations.
- Document ingestion from
.mdand.txtfiles - Chunking with configurable size and overlap
- FAISS vector index for k-nearest neighbor retrieval
- Google Gemini for answer generation
- Citation validation and grounding checks
- Machine-readable JSON outputs
docs/*.md,*.txt -> Document Loader -> Chunker -> FAISS Index
|
questions.json -> Retriever (k=3) -> Gemini Answer Gen -> Citation Validator -> Final Output
cd deriv-ragpython3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtCreate a .env file with your Google API key:
GOOGLE_API_KEY=your_api_key_here
Get a free API key from Google AI Studio.
python run.pyThis executes all stages:
- Load documents from
docs/ - Create chunks
- Build FAISS index with embeddings
- Load questions from
questions.json - Retrieve relevant chunks for each question
- Generate answers using Gemini
- Validate citations
- Export final results
python validate.pyChecks all generated artifacts for correctness.
Place your knowledge base documents here (.md or .txt files).
Questions to answer:
[
{ "id": 1, "question": "What authentication methods does the API support?" },
{ "id": 2, "question": "How long are webhook delivery logs retained?" }
]All outputs are saved to artifacts/:
| File | Description |
|---|---|
documents.json |
Loaded documents with text content |
chunks.json |
Document chunks with positions |
faiss_index.bin |
FAISS vector index |
chunk_mapping.json |
Chunk ID to index mapping |
retrieval_results.json |
Retrieved chunks per question |
answers.json |
Generated answers with citations |
citation_validation.json |
Validation results |
final_answers.json |
Combined final output |
llm_calls.jsonl |
LLM call logs |
Edit constants in run.py:
CHUNK_SIZE = 500 # characters per chunk
CHUNK_OVERLAP = 50 # overlap between chunks
TOP_K = 3 # chunks to retrieve
EMBEDDING_MODEL = "gemini-embedding-001"
GENERATION_MODEL = "gemini-2.5-flash-lite"INIT
-> DOCUMENTS_LOADED
-> CHUNKS_CREATED
-> INDEX_BUILT
-> QUESTIONS_LOADED
-> RETRIEVAL_COMPLETE
-> ANSWERS_GENERATED
-> CITATIONS_VALIDATED
-> RESULTS_EXPORTED
-> VALIDATION_COMPLETE
The pipeline validates:
- Cited chunk IDs exist in retrieved chunks
- Supported answers have at least one citation
- Unsupported answers have no citations
- Numeric claims in answers appear in cited chunks (grounding check)
MIT