How aianytime/agentic-rag-using-langgraph works
Project Type & Purpose
• This is a backend service—a conversational Retrieval-Augmented Generation (RAG) pipeline.
• Purpose: Accept multi-turn user queries, preserve context, filter off-topic requests, retrieve and evaluate documents from a vector store, refine failed searches, and generate grounded responses via an LLM.
File Structure Analysis
• Top-level files:
• langgraph_rag_system.py – contains the entire workflow definition (graph nodes, edges, LLM & vector store setup, state model).
• pyproject.toml, uv.lock – project metadata and dependency lock for the UV build tool.
• README.md – detailed architecture and usage guide.
• No separate frontend or microservices—everything lives in one Python process orchestrated by LangGraph.
Key README Insights
• The “System Architecture” section spells out a node-based flow chart (rephrase → classify → fetch → evaluate → tweak/retry → respond/fallback).
• External services: OpenAI API for embeddings & chat, Chroma vector DB for retrieval, LangGraph for workflow orchestration.
Main Components
a. Ingress: User (via CLI or API)
b. Workflow Engine: LangGraph StateGraph
c. State Store / Memory: MemorySaver or SqliteSaver checkpoint
d. Vector Database: Chroma or other vectorstore
e. LLM Service: OpenAI ChatOpenAI & OpenAIEmbeddings
f. Document Management: Document loader component
g. Control Logic Nodes:
‑ Rephrase Query
‑ Classify Topic
‑ Fetch Documents
‑ Evaluate Documents
‑ Tweak Question (Retry)
‑ Create Response / Fallback
Relationships & Data Flow
1. User question → StateGraph entry node (“Rephrase Query”)
2. “Rephrase Query” → “Classify Topic”
3. “Classify Topic” → if off-topic: “Reject Off-Topic” → END
else → “Fetch Documents”
4. “Fetch Documents” → Vector DB (Chroma) → returns docs → “Evaluate Documents”
5. “Evaluate Documents” → if relevant docs found: “Create Response” → END
else if attempts < max: “Tweak Question” → back to “Fetch Documents”
else → “Fallback Response” → END
• Each node may invoke the LLM service for structured output or generation.
Architectural Patterns & Principles
• Graph-based workflow (StateGraph) for modular, reusable nodes
• Stateful design: DialogState TypedDict holds conversation history, retrieved_docs, query refinements, flags
• Retry logic & self-correction: conditional branching based on evaluation results
• Clear separation of concerns: query rewriting, classification, retrieval, evaluation, response generation
Technology Stack Labels
• Python 3.11 runtime
• UV (uv) build & execution tool
• LangGraph (StateGraph, nodes, edges)
• LangChain Core / langchain_openai / langchain_community
• Chroma vector database
• OpenAI API (ChatOpenAI, OpenAIEmbeddings)
• pydantic for structured outputs
• dotenv for configuration
Final Advice
• Be exhaustive: even though the code is in one file, break it into logical components.
• Iterative retry loop explicitly.
• How state flows from node to node via the TypedDict.
• Keep labels concise but precise—derive them from function names and state keys.