How arjun-bhattarai/rag-with-langchain works
Purpose and Entry Points
Arjunx is a modular Python Retrieval-Augmented Generation assistant: it transforms a question, selects local/web sources, evaluates evidence, and generates a grounded answer through a local Ollama model. The HTTP entry point is api/main.py; POST /chat/ is defined in api/routes/chat.py and delegates orchestration to api/services/rag_service.py. A lightweight browser client is rooted at frontend/index.html with behavior in frontend/script.js.
Application Boundary
FastAPI validates request/response payloads through api/schemas/chat.py, while the service layer isolates API concerns from the RAG implementation in api/services/rag_service.py. This boundary should remain the integration seam for alternate clients, streaming, authentication, or deployment concerns; pipeline modules should not require HTTP types.
Pipeline Orchestration and Generation
The integrated flow is centered on src/pipeline/rag_pipeline.py, with fundamental indexing, retrieval, and generation stages split across src/pipeline/indexing_rag.py, src/pipeline/retrieval_rag.py, and src/pipeline/generation_rag.py. Shared model/configuration abstractions reside in src/core/models.py and src/core/config.py. Generation ultimately targets Ollama/local Llama inference, mediated by LangChain-oriented RAG components.
Query Understanding and Source Selection
Before retrieval, query translation can create alternative retrieval formulations—multi-query, fusion, decomposition, HyDE, and step-back—in src/query_translation/query_processor.py, src/query_translation/multi_query.py, and src/query_translation/rag_fusion.py. Query structuring lives in src/query_structuring/query_structuring.py. Routing decides local, web, or hybrid retrieval via src/routing/source_routing.py, supported by logical and semantic policies in src/routing/logical_routing.py and src/routing/semantic_routing.py.
Retrieval and Indexing Boundary
Local retrieval implementations are separated from remote-source adapters: src/retrieval/chroma.py uses ChromaDB vector storage, while src/retrieval/web.py accesses web knowledge. External-source provider modules are data_source/web_search.py, data_source/wikipedia.py, and data_source/personal_website.py; Tavily and Wikipedia are material remote dependencies. Hybrid retrieval is composed in src/retrieval/hybrid.py. Advanced retrieval/indexing options include RAPTOR and ColBERT under src/retrieval/raptor.py, src/advanced_indexing/raptor.py, and src/advanced_indexing/colbert.py.
Evidence Quality and Adaptive Correction
Retrieved documents pass through evidence-quality stages: duplicate/overlap removal in src/utils/deduplication.py, relevance reranking in src/Reranking/reranking.py, and temporal prioritization in src/Reranking/freshness.py. Corrective and self-evaluating decisions are implemented in src/advanced_RAG/CRAG/crag.py and src/advanced_RAG/Self_RAG/self_rag.py; insufficient evidence can trigger more retrieval or yield NOT_ANSWERABLE. Context scaling is handled by src/advanced_RAG/Long_Context/long_context.py, and evidence representation/support utilities are in src/utils/evidence.py.
State, Evaluation, and Experimentation
Conversation/summarization/vector-memory capabilities are consolidated in src/memory/memory.py, providing state that can enrich later requests. Pipeline quality assessment is represented by src/evaluation/rag_evaluation.py. The production-oriented source tree is complemented by exploratory notebooks, especially integration/integration.py and integration/integrated_rag.ipynb, which document and exercise assembly of the techniques without being the application runtime.
Main entry points: retrieval_rag.py Retrieval stage · rag_pipeline.py RAG orchestrator · chat.py POST /chat · hybrid.py Hybrid retrieval
Components 21
Client
- Browser client
frontend/index.html - Chat UI behavior
frontend/script.js
HTTP boundary
- FastAPI app
api/main.py - POST /chat
api/routes/chat.py - Chat schemas
api/schemas/chat.py - RAG service · Keeps HTTP types outside the RAG implementation.
api/services/rag_service.py
RAG pipeline
- RAG orchestrator
src/pipeline/rag_pipeline.py - Query translation · Supports multi-query, fusion, decomposition, HyDE, and step-back formulations.
src/query_translation/query_processor.py - Query structuring
src/query_structuring/query_structuring.py - Source routing · Chooses local, web, or hybrid retrieval.
src/routing/source_routing.py - Indexing stage
src/pipeline/indexing_rag.py - Grounded generation · Generates through LangChain-oriented components and local Ollama/Llama inference.
src/pipeline/generation_rag.py - Model abstractions
src/core/models.py
Retrieval and evidence
- Retrieval stage
src/pipeline/retrieval_rag.py - Local vector retrieval
src/retrieval/chroma.py - Web retrieval · Uses web knowledge providers including Tavily and Wikipedia.
src/retrieval/web.py - Hybrid retrieval
src/retrieval/hybrid.py - Evidence quality · Removes overlap, reranks relevance, and prioritizes fresh evidence.
src/utils/deduplication.py - Corrective RAG · Can retry retrieval or return NOT_ANSWERABLE when evidence is insufficient.
src/advanced_RAG/CRAG/crag.py
State and quality
- Conversation memory · Conversation, summary, and vector memory enrich later requests.
src/memory/memory.py - RAG evaluation
src/evaluation/rag_evaluation.py
Connections 24
- Browser client → Chat UI behavior: loads
- Chat UI behavior → POST /chat: POST /chat
- FastAPI app → POST /chat: mounts
- POST /chat → Chat schemas: validates
- POST /chat → RAG service: delegates
- RAG service → RAG orchestrator: invokes
- RAG orchestrator → Query translation: expands query
- Query translation → Query structuring: structures
- Query structuring → Source routing: routes
- RAG orchestrator → Indexing stage: indexes corpus
- Indexing stage → Local vector retrieval: builds local index
- Source routing → Retrieval stage: selects retrieval
- Retrieval stage → Local vector retrieval: local
- Retrieval stage → Web retrieval: web
- Retrieval stage → Hybrid retrieval: hybrid
- Hybrid retrieval → Local vector retrieval: combines
- Hybrid retrieval → Web retrieval: combines
- Retrieval stage → Evidence quality: documents
- Evidence quality → Corrective RAG: assesses evidence
- Corrective RAG → Retrieval stage: retry if needed
- Corrective RAG → Grounded generation: supported context
- Grounded generation → Model abstractions: uses models
- RAG orchestrator → Conversation memory: reads and updates
- RAG orchestrator → RAG evaluation: assesses quality