A simple Retrieval-Augmented Generation (RAG) proof of concept.
This project takes a user question, retrieves the most relevant text chunks from a small document set using embeddings + FAISS, and builds a final prompt that can be sent to an LLM.
Current flow:
- Load documents
- Chunk documents
- Create embeddings and build FAISS index
- Retrieve top-k chunks for a query
- Build a context-aware prompt
pipeline.py→ Main orchestrator (entry point)docs.py→ Document sourcechunking.py→ Text chunking logicbuild_db.py→ Embedding model loading + FAISS index constructionquery.py→ Retrieval from vector indexllm_call.py→ Prompt construction for LLM call
Purpose: Coordinates the complete RAG pipeline from documents to prompt.
- Loads embedding model via
load_embedding_model() - Loads raw documents via
load_documents() - Chunks docs via
chunk_documents() - Builds vector DB and chunk map via
build_vector_store() - Runs retrieval with
query_rag() - Prints retrieved chunks
- Builds final LLM prompt with
build_prompt() - Prints final prompt
Execution starts from:
if __name__ == "__main__":
main()Purpose: Keeps all source documents in one place.
- Returns a Python list of document strings.
- Right now this is hardcoded sample content.
- In a real system, this can be replaced with file/database/API loading.
Purpose: Splits long text into smaller pieces for embedding and retrieval.
- Splits one document into word-based chunks.
- Uses fixed-size chunks (
chunk_sizewords each). - Returns a list of chunk strings.
- Applies
chunk_text()to every document. - Flattens all chunks into one list.
- Returns full corpus chunk list.
Purpose: Creates the vector retrieval backend (embeddings + FAISS index).
- Loads a SentenceTransformer model.
- Default model is lightweight and commonly used for RAG demos.
- Encodes chunks into embeddings using
model.encode(chunks). - Converts embeddings to NumPy array.
- Builds a FAISS
IndexFlatL2index. - Adds embeddings into index.
- Creates
chunk_storemapping:- key: FAISS vector id (int)
- value: original text chunk
- Returns
(index, chunk_store).
Purpose: Retrieves the most relevant chunks for a user question.
- Encodes query text into embedding.
- Searches FAISS index for top-k nearest vectors.
- Uses returned ids to fetch chunk text from
chunk_store. - Returns:
retrieved(list of best chunks)distances(FAISS distance scores)
Purpose: Builds the final prompt sent to an LLM.
- Joins retrieved chunks into one context string.
- Formats prompt as:
- instruction
- context
- user question
- Returns prompt string.
Note: This file currently only builds the prompt (no actual API call yet).
pipeline.pystartsmain().docs.load_documents()returns raw documents.chunking.chunk_documents()splits them into manageable chunks.build_db.load_embedding_model()loads embedding model.build_db.build_vector_store():- creates embeddings,
- builds FAISS index,
- prepares chunk id → text map.
- User question is sent to
query.query_rag(). - Query embedding is compared against all chunk embeddings in FAISS.
- Top-k closest chunks are returned as retrieval context.
llm_call.build_prompt()combines context + question into final prompt.- Prompt is ready to send to any LLM API.
pip install sentence-transformers faiss-cpu numpyIf you use a virtual environment:
python -m venv venv
source venv/bin/activate
pip install sentence-transformers faiss-cpu numpypython pipeline.pyExpected output:
- Retrieved chunks printed in terminal
- Final context-aware prompt printed in terminal