How yash2010/ai_rag works
Purpose and Entry Points
This is a local, CLI-driven retrieval-augmented-generation agent. It ingests local documents and optional URLs into a persistent vector store, then routes each chat query to document retrieval, web search, both, or direct local-model generation. Primary entry point: main.py; core workflow implementation: agent.py.
Runtime and Configuration
Python 3.10+ runs the application. Settings are centralized in config.yaml and loaded through config.py, including model names, retrieval chunking, storage locations, search provider, and routing defaults. The process depends on a locally running Ollama service for Llama3 generation and nomic-embed-text embeddings.
Ingestion and Knowledge Boundary
Ingestion is invoked through python main.py --ingest. Source material resides under docs/, currently including docs/test.txt; supported sources also include PDFs, DOCX, and URL lists. Content is parsed, chunked, embedded, and persisted in Chroma at data/chroma/. This creates the local knowledge boundary: documents become searchable semantic chunks rather than being sent to a cloud LLM.
Stateful Agent Graph
agent.py defines the LangGraph-based workflow around a shared AgentState. Its architecture is a router followed by conditional stages:
• router classifies a query as rag, web, both, or direct;
• retrieval queries Chroma for relevant local chunks;
• web search fetches current external evidence;
• generation synthesizes an answer and citations.
The graph owns transitions; individual nodes read and update shared state.
Retrieval and Persistence
Chroma is the durable document-index backend, represented by data/chroma/chroma.sqlite3 plus its vector index files under data/chroma/52dc6bd8-1df5-4af8-9357-daadb2c3258a/. Retrieval returns the configured top-k chunks to the generation stage. Conversation history is intended to persist in the configured SQLite memory database under data/, enabling continuity across CLI sessions.
External Services and Trust Boundaries
Ollama is a local inference dependency, keeping LLM prompts and embeddings on the machine. Web retrieval crosses the external boundary through the configured search provider: Tavily, SerpAPI, or DuckDuckGo; paid/keyed providers use environment-supplied API credentials. Web results and local retrieved chunks converge only at answer generation, where source citations are produced.
User Interaction
Without --ingest, main.py starts an interactive terminal session. It accepts natural-language questions and session commands such as reset, help, and quit. The CLI exposes the selected strategy, retrieval status, cited sources, and final generated response while delegating all decision logic to the agent graph in agent.py.
Main entry points: main.py Interactive CLI · agent.py Answer generation · config.py Settings loader · config.yaml Application settings
Components 14
Local Runtime
CLI process and local inference dependencies.
- Interactive CLI · Starts chat or ingestion from command-line flags.
main.py - Settings loader · Loads model, storage, routing, and search settings.
config.py - Application settings
config.yaml - Ollama · Provides Llama3 generation and nomic-embed-text embeddings locally.
Agent Workflow
LangGraph stages operating on shared state.
- LangGraph workflow · Owns conditional transitions over AgentState.
agent.py - AgentState · Carries query, strategy, evidence, and answer between stages.
agent.py - Query router · Selects rag, web, both, or direct generation.
agent.py - Local retrieval · Returns configured top-k semantic chunks.
agent.py - Web search · Fetches current external evidence when routed.
agent.py - Answer generation · Synthesizes response and source citations.
agent.py
Knowledge & Memory
Persistent local retrieval and session storage.
- Local source documents · Text, PDF, DOCX, and URL-list sources.
- Chroma vector store · Persists embedded document chunks.
- Conversation memory · Configured persistence for continuity across CLI sessions.
External Retrieval
Optional internet-facing search boundary.
- Search provider · Tavily, SerpAPI, or DuckDuckGo; keyed providers use environment credentials.
Connections 18
- Application settings → Settings loader: loaded by
- Settings loader → Interactive CLI: configures
- Interactive CLI → Local source documents: --ingest reads
- Local source documents → Chroma vector store: parse, chunk, embed, persist
- Chroma vector store → Ollama: embeddings generated by
- Interactive CLI → LangGraph workflow: submits query
- Interactive CLI → Conversation memory: loads and persists sessions
- LangGraph workflow → AgentState: maintains
- LangGraph workflow → Query router: starts with
- Query router → Local retrieval: rag or both
- Query router → Web search: web or both
- Query router → Answer generation: direct
- Local retrieval → Chroma vector store: queries chunks
- Local retrieval → Answer generation: local evidence
- Web search → Search provider: requests results
- Web search → Answer generation: web evidence
- Answer generation → Ollama: generates with
- Answer generation → Interactive CLI: answer and citations