# farzad-r/advanced-qa-and-rag-series architecture

> Architecture diagram of the GitHub repository farzad-r/advanced-qa-and-rag-series, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/farzad-r/advanced-qa-and-rag-series
- Repository: https://github.com/farzad-r/advanced-qa-and-rag-series

## Overview

##### Project Type and Purpose
- Type: Full-stack conversational AI application (no browser-based frontend; uses a Python-based UI via Gradio).
- Purpose: End-to-end chatbot that can perform Q&A and RAG over unstructured documents and SQL tables, automatically selecting the appropriate tool.

##### File Structure (AgentGraph-Intelligent-Q&A-and-RAG-System)
- Top-level directories:
- notebooks/ – exploration, prototyping, teaching material
- configs/ – YAML config files (project_config.yml, tools_config.yml)
- data/ – sample databases (Chinook.db), unstructured docs for RAG
- memory/ – CSV files storing conversational memory
- src/
– agent_graph/ – core orchestration classes and tool definitions
– chatbot/ – backend logic for Gradio UI and session management
– prepare_vector_db.py – ingestion script to build vector DB from docs
– app.py – entry point that spins up the Gradio interface
– utils/ – shared utilities (UI settings, common helpers)
- Configuration & deployment artifacts: requirements.txt, .env template in env_example

##### README for Insights
- Core frameworks: OpenAI (or AzureOpenAI), LangChain, LangGraph, LangSmith (monitoring), Gradio
- Features: multi-tool agent, SQL agents for structured data, RAG on vector store, automatic tool selection, web search fallback
- Data flows: ingestion → vector store, DB queries via SQL agent, tool chain orchestration

##### Out Main Components
a. Frontend / UI
- Gradio application (src/app.py)
- Chat interface, memory display, tool selection controls
b. Application / Backend
- Chatbot backend (src/chatbot/chatbot_backend.py)
- Agent orchestrator (src/agent_graph/agent_backend.py) using LangGraph
- Tool modules (e.g., tool_chinook_sqlagent.py, tool_lookup_policy_rag.py, tool_stories_rag.py, tool_tavily_search.py)
- Config loader (src/agent_graph/load_tools_config.py, src/chatbot/load_config.py)
- Vector-DB preparation (src/prepare_vector_db.py)
c. Data Storage
- Vector store (ChromaDB) populated from unstructured_docs
- SQL database (Chinook.db, travel.db, etc.)
- Memory CSV logs
d. External Services
- OpenAI / AzureOpenAI API calls for LLM completions and embeddings
- Optional web-search API (as configured in tools_config.yml)
e. Monitoring & Logging
- LangSmith for agent performance tracking
- Local CSV/per-session logs

##### Relationships & Data Flows
- User → Gradio UI → Chatbot Backend
- Chatbot Backend → Agent Orchestrator → Config Loader + Memory Retriever
- Orchestrator → selects appropriate Tool(s):
- SQL Agent → interacts with SQL DB
- RAG Tool → queries VectorDB → embeddings via OpenAI → similarity search
- Web Search Tool → calls external search API
- Tools → return structured data (text, table, JSON) → Agent → composes LLM prompt → LLM API
- LLM Response → Agent → Chatbot Backend → UI
- Monitoring: each LLM call and agent step logged to LangSmith

##### Architectural Patterns
- Modular “Tool” plugin architecture (separation of tool definitions from orchestrator)
- Single entrypoint (app.py) adhering to “adapter” pattern bridging UI and agent core
- Config-driven behavior (YAML files define tools, databases, limits)
- Layered approach:
Presentation (Gradio UI)
Application (chatbot, orchestrator)
Domain (tools, data ingestion)
Infrastructure (databases, external APIs)

##### Checklist
[ ] Frontend (Gradio) clearly separated from backend
[ ] Config files feeding into orchestrator and ingestion
[ ] Monitoring component (LangSmith) linked to orchestrator
[ ] Offline ingestion path (prepare_vector_db) displayed as a side process

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        UI["Gradio UI"]:::presentation
        UI_settings["UI Settings"]:::presentation
    end

    %% Application Layer
    subgraph "Application Layer"
        ChatBackend["Chatbot Backend"]:::logic
        ConfigLoaderChat["Chat Config Loader"]:::logic
        Memory["Memory Manager"]:::logic
        Orchestrator["Agent Orchestrator"]:::logic
        ToolsConfig["Tools Config Loader"]:::logic
        GraphBuilder["Full Graph Builder"]:::logic
    end

    %% Tools (Domain)
    subgraph "Tools (Domain)"
        SQLChinook["Chinook SQL Agent"]:::tools
        SQLTravel["Travel SQL Agent"]:::tools
        PolicyRAG["Policy RAG Tool"]:::tools
        StoriesRAG["Stories RAG Tool"]:::tools
        WebSearch["Web Search Tool"]:::tools
    end

    %% Infrastructure & Data Stores
    subgraph "Infrastructure & Data Stores"
        VectorDB["Chroma Vector Store"]:::datastore
        SQLDB["SQL Databases"]:::datastore
        UnstructuredDocs["Unstructured Docs"]:::datastore
        MemoryCSV["Memory CSV Logs"]:::datastore
        ConfigProject["project_config.yml"]:::datastore
        ConfigTools["tools_config.yml"]:::datastore
    end

    %% Offline Ingestion
    subgraph "Offline Ingestion"
        Ingestion["Vector DB Prep Script"]:::logic
    end

    %% External Services
    subgraph "External Services"
        OpenAI["OpenAI/AzureOpenAI API"]:::external
        WebSearchAPI["Web Search API"]:::external
        LangSmith["LangSmith Monitoring"]:::external
    end

    %% Data Flows - Online Chat
    UI -->|"user input"| ChatBackend
    UI -->|"display memory"| Memory
    ChatBackend -->|"load chat config"| ConfigLoaderChat
    ChatBackend -->|"invoke orchestrator"| Orchestrator
    ChatBackend -->|"manage memory"| Memory
    ChatBackend -->|"response"| UI
    ConfigLoaderChat --> ConfigProject
    Orchestrator -->|"load tools config"| ToolsConfig
    ToolsConfig --> ConfigTools
    Orchestrator --> GraphBuilder
    Orchestrator --> SQLChinook
    Orchestrator --> SQLTravel
    Orchestrator --> PolicyRAG
    Orchestrator --> StoriesRAG
    Orchestrator --> WebSearch
    SQLChinook -->|"DB query"| SQLDB
    SQLTravel -->|"DB query"| SQLDB
    PolicyRAG -->|"similarity search"| VectorDB
    StoriesRAG -->|"similarity search"| VectorDB
    WebSearch -->|"web query"| WebSearchAPI
    Orchestrator -->|"LLM prompt"| OpenAI
    OpenAI -->|"LLM response"| Orchestrator
    Orchestrator -->|"log events"| LangSmith
    Orchestrator --> ChatBackend

    %% Offline Ingestion Flow
    Ingestion -.->|"reads docs"| UnstructuredDocs
    Ingestion -.->|"embeddings"| OpenAI
    Ingestion -.->|"populates"| VectorDB
    Ingestion --> ConfigTools

    %% Click Events
    click UI "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/app.py"
    click UI_settings "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/utils/ui_settings.py"
    click ChatBackend "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/chatbot/chatbot_backend.py"
    click ConfigLoaderChat "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/chatbot/load_config.py"
    click Memory "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/chatbot/memory.py"
    click Orchestrator "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/agent_graph/agent_backend.py"
    click ToolsConfig "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/agent_graph/load_tools_config.py"
    click GraphBuilder "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/agent_graph/build_full_graph.py"
    click SQLChinook "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/agent_graph/tool_chinook_sqlagent.py"
    click SQLTravel "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/agent_graph/tool_travel_sqlagent.py"
    click PolicyRAG "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/agent_graph/tool_lookup_policy_rag.py"
    click StoriesRAG "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/agent_graph/tool_stories_rag.py"
    click WebSearch "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/agent_graph/tool_tavily_search.py"
    click Ingestion "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/src/prepare_vector_db.py"
    click ConfigProject "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/configs/project_config.yml"
    click ConfigTools "https://github.com/farzad-r/advanced-qa-and-rag-series/blob/main/configs/tools_config.yml"

    %% Styles
    classDef presentation fill:#d4f8d4,stroke:#2d7a2d,color:#2d7a2d;
    classDef logic fill:#d0e1f9,stroke:#1a4e8a,color:#1a4e8a;
    classDef tools fill:#fde4b4,stroke:#c46d00,color:#c46d00;
    classDef datastore fill:#e0e0e0,stroke:#7a7a7a,color:#333333;
    classDef external fill:#e8d0f9,stroke:#6a1a8a,color:#6a1a8a;
```
