Visitar URL original
starting-ragchatbot-codebase/query_flow_diagram.md at main · dan6684/starting-ragchatbot-codebase · GitHub
Skip to content

Latest commit

 

History

History
120 lines (87 loc) · 3.61 KB

query_flow_diagram.md

File metadata and controls

120 lines (87 loc) · 3.61 KB

RAG Chatbot Query Flow Visualization

sequenceDiagram
    participant User
    participant Frontend as Frontend<br/>(script.js)
    participant API as FastAPI<br/>(app.py)
    participant RAG as RAGSystem<br/>(rag_system.py)
    participant Session as SessionManager<br/>(session_manager.py)
    participant AI as AIGenerator<br/>(ai_generator.py)
    participant Tools as SearchTools<br/>(search_tools.py)
    participant Vector as VectorStore<br/>(vector_store.py)

    User->>Frontend: Types query & clicks send
    Note over Frontend: sendMessage() at line 45
    
    Frontend->>Frontend: Disable input & show loading
    Note over Frontend: Lines 49-60
    
    Frontend->>API: POST /api/query
    Note over Frontend: Fetch request at lines 63-72
    
    API->>API: Validate QueryRequest
    Note over API: Pydantic model at lines 38-41
    
    API->>RAG: query(query, session_id)
    Note over API: Line 66
    
    RAG->>Session: get_conversation_history()
    Note over RAG: Lines 118-119
    Session-->>RAG: Return chat history
    
    RAG->>AI: generate_response(query, history, tools)
    Note over RAG: Lines 122-127
    
    AI->>AI: Call Claude API with tools
    Note over AI: Line 80
    
    alt Claude decides to use search tool
        AI->>Tools: execute_tool("search_course_content")
        Note over AI: Lines 83-84
        
        Tools->>Vector: search(query, filters)
        Note over Tools: Lines 66-70
        Vector-->>Tools: Return search results
        
        Tools->>Tools: Format results & track sources
        Note over Tools: Lines 88-114
        Tools-->>AI: Formatted search results
        
        AI->>AI: Generate final response with context
        Note over AI: Lines 134-135
    end
    
    AI-->>RAG: Generated response
    
    RAG->>Tools: get_last_sources()
    Note over RAG: Line 130
    Tools-->>RAG: Source list
    
    RAG->>Session: add_exchange(session_id, query, response)
    Note over RAG: Line 137
    
    RAG-->>API: (response, sources)
    
    API->>API: Build QueryResponse
    Note over API: Lines 68-72
    
    API-->>Frontend: JSON response
    
    Frontend->>Frontend: Remove loading message
    Note over Frontend: Line 84
    
    Frontend->>Frontend: Convert markdown & display
    Note over Frontend: Lines 120, 133-135
    
    Frontend->>Frontend: Show sources in collapsible
    Note over Frontend: Lines 124-131
    
    Frontend->>Frontend: Re-enable input
    Note over Frontend: Lines 92-94
    
    Frontend-->>User: Display response with sources
Loading

Key Components & Flow Points

1. User Input Capture

  • Location: frontend/script.js:45-96
  • Function: sendMessage()
  • Triggers: Enter key or send button click

2. API Request

  • Endpoint: POST /api/query
  • Location: backend/app.py:56-74
  • Validation: Pydantic QueryRequest model

3. RAG Orchestration

  • Location: backend/rag_system.py:102-140
  • Manages: Session history, AI generation, tool execution

4. AI Tool Integration

  • Location: backend/ai_generator.py:43-135
  • Uses: Claude API with function calling
  • Tool: Course search with semantic matching

5. Vector Search

  • Location: backend/search_tools.py:52-114
  • Features: Course filtering, lesson filtering, source tracking

6. Response Assembly

  • Components: Generated text + source citations
  • Display: Markdown rendering + collapsible sources

Data Flow Summary

  1. Input: User query string
  2. Processing: Semantic search + AI generation
  3. Output: Formatted response + source citations
  4. Storage: Conversation history in session manager