How iiiiiiiinseong/langgraph_rag_agent works
Project type and purpose
• This is a full-stack AI chatbot system specialized for personalized financial advice.
• It implements an Adaptive Self-Retrieval-Augmented-Generation (Self-RAG) pipeline on top of a Flask web server (with Gradio UI), backed by document preprocessing, hybrid search (BM25 + vector), ChromaDB embedding store, and OpenAI’s GPT-4o-mini for answer generation and self-evaluation.
File structure
• Top-level directories:
• db_preprocessing: raw CSV → Python scripts → cleaned data
• flask_server: Flask app (app.py, routes.py, config.py), static PDF store, Jinja2 templates
• src: core RAG pipeline scripts (adaptive_self_rag.py, entity_extraction.py, qd_mapping.py, evaluate_retrieval.py)
• results: evaluation JSON/CSV outputs
README for additional insights
• Describes the Adaptive Self-RAG chain built using LangChain & LangGraph
• Highlights hybrid search (bm25 + vector via HuggingFace Embeddings + ChromaDB)
• Shows roles of question routing, document retrieval, answer generation, hallucination check, and personalization
Main components
a. Data Preparation
• Raw CSV files (db_preprocessing/rawdata)
• Data processing scripts → cleaned data
b. Document Store & Indexing
• Static PDF folder (flask_server/static/pdf_storage) – raw documents
• ChromaDB (vector index) + BM25 index (hybrid search engine)
c. Adaptive Self-RAG Pipeline
• Question Router (classifies financial vs. general)
• Parallel document retrievers (deposit, fixed, loan, savings)
• Relevance evaluator (self-assessment of retrieved docs)
• Self-corrector (re-formulates queries if needed)
• Generator (GPT-4o-mini) → draft answer
• Answer Evaluator (checks hallucination, relevance)
d. Web Server & UI
• Flask server routes → calls pipeline
• Gradio interface embedded in Flask templates
e. Evaluation & Metrics
• Q-D mapping modules (qd_mapping.py, qd_mapping_multi.py)
• evaluate_retrieval.py → produces JSON/CSV in results/
Relationships and interactions
• User → Flask/Gradio UI → HTTP POST “ask” → Pipeline entry
• Pipeline → Query Router → (if general → direct GPT call; if financial → RAG chain)
• RAG chain → Hybrid Search → ChromaDB + bm25 → filtered docs → Generator
• Generated answer → Self-evaluation & possible re-query → Final answer
• All data flows (documents, embeddings, queries, answers) mediated by pipeline modules
• Evaluation scripts operate offline on results directory
Architectural patterns and design principles
• Modular pipeline orchestrated by LangGraph → clearly separated stages
• Hybrid design: retrieval (vector + BM25) + generative model
• Self-feedback loop: generator → evaluator → optional re-query
• Separation of concerns: data prep, document store, pipeline logic, web server, evaluation
Technologies and frameworks to annotate
• Python 3.x, Flask, Jinja2, Gradio
• LangChain & LangGraph for RAG orchestration
• OpenAI GPT-4o-mini (LLM)
• HuggingFace Embeddings, ChromaDB, rank-BM25
• CSV / JSON for Q-D mapping, evaluation outputs