How amr-abdellatif/rag-application-multiple-pdf-s-with-langchain works
Project type and purpose
• This is a small Python-based Retrieval-Augmented Generation (RAG) application.
• Purpose: Ingest multiple PDF documents, build a vector-embedding database, and answer user questions by retrieving relevant context and querying a local LLM (Ollama llama3:8b).
File structure
• Top-level scripts:
• get_embedding_function.py – wraps the LLM’s embedding API.
• populate_database.py – loads PDFs, computes embeddings, and populates a vector store.
• query_data.py – CLI entry point: takes a user question, retrieves nearest embeddings, and prompts the LLM.
• test_rag.py – pytest suite exercising end-to-end flow.
• Supporting files:
• requirements.txt – lists Python dependencies (e.g., langchain, chromadb, PyPDF).
• README.md – usage instructions.
• .gitignore
• Implied directory:
• data/ – user-provided PDFs reside here.
README for additional insights
• Installation: sets up Ollama, installs Python requirements.
• Workflow:
1. Drop PDFs into data/
2. Run populate_database.py → builds embedding DB
3. Run query_data.py "question" → returns an answer
4. Optionally run pytest on test_rag.py
Main components
a. PDF Ingestion & Parsing
• Component: PDF Loader
• Inputs: data/*.pdf
• Outputs: raw text chunks
b. Embedding Service
• Component: get_embedding_function
• Technology: Ollama llama3:8b via local CLI/API
• Function: Converts text chunks → high-dim vectors
c. Vector Store / Database
• Component: ChromaDB (or whichever vector DB is in requirements)
• Stores: {chunk_id, text_chunk, embedding_vector}
d. Retrieval & Query Pipeline
• Component: query_data.py
• Steps:
1. Accept CLI input “question”
2. Get question embedding via embedding_function
3. Perform similarity search on vector store → top-k text chunks
4. Assemble prompt: user question + retrieved context
5. Send prompt to LLM (Ollama) → generate answer
e. External Service
• Ollama local LLM server (llama3:8b)
Relationships & Data Flow
• data/*.pdf → PDF Loader → Text Chunks
• Text Chunks → Embedding Service → Vectors
• Vectors/Text → Vector Store
• User Question → Embedding Service → Query Embedding
• Query Embedding → Vector Store (Similarity Search) → Retrieved Chunks
• Retrieved Chunks + Question → LLM → Answer → CLI Output
Architectural patterns & design principles
• Separation of concerns: ingestion, embedding, storage, retrieval, LLM inference.
• Modularity: each major step lives in its own script/function.
• Reproducibility: test_rag.py ensures end-to-end correctness.