How aditya-karande/chatbot_with_langgraph works
Purpose and Entry Point
This is a Streamlit-based agentic chatbot that combines LangGraph orchestration, LLM tool calling, document RAG, approval-gated code execution, and two memory scopes. Runtime starts at app.py, which hosts chat, thread interaction, file upload, and human approval/resume controls.
Application Boundary and State
The backend models a stateful LangGraph conversation rather than a request/response web service. backend/state.py defines chatbot state and prompting context; backend/config.py centralizes model, embedding, database, and memory/checkpoint setup. Conversation identity and user context must flow consistently through graph invocations and resumes.
Main Orchestration Graph
backend/graph.py composes the primary LangGraph workflow and routes among agent generation, tools, RAG, memory persistence, and summarization. backend/agent.py supplies the main agent decision logic and conditional paths. The graph is the control-plane boundary: individual capabilities are graph nodes or subgraphs, while Streamlit invokes/resumes the compiled workflow.
Agent and Tool Execution
The agent uses an external Groq-hosted openai/gpt-oss-120b model to decide whether to answer directly or request capabilities. backend/tools.py exposes weather, stock, currency, date, web search, and Python execution tools. Safe tool calls return into the agent loop; Python execution crosses a human-approval boundary through hitl_gate in backend/agent.py, using LangGraph interruption until the UI approves or rejects.
Retrieval-Augmented Generation
backend/rag.py owns document upload/ingestion and a dedicated LangGraph RAG subgraph. PDF, TXT, and Markdown content is chunked, embedded with Gemini embeddings, and persisted in PostgreSQL pgvector. At query time, retrieval is followed by relevance grading/filtering before selected chunks are supplied for answer generation. This separates document knowledge from general agent/tool reasoning.
Memory and Conversation Lifecycle
Short-term conversation continuity is checkpoint-backed and managed with history compression in backend/summarization.py; summaries replace older messages when context grows. Long-term, cross-conversation facts are retrieved before response generation and selectively deduplicated/stored afterward by backend/memory.py. backend/threads.py manages user-facing chat histories, titles, and renaming, distinct from graph checkpoint state.
Runtime Infrastructure
The application runs as Python/Streamlit (app.py) with LangGraph as the workflow runtime. It depends on Groq for LLM inference, Google/Gemini for embeddings, PostgreSQL plus pgvector for vector persistence, and external APIs for tools such as weather and search. Configuration and connection initialization are concentrated in backend/config.py; deployment requires database connectivity, pgvector enabled, and API credentials.
Main entry points: graph.py Compiled Workflow · config.py Runtime Configuration · agent.py Agent Decision & HITL · app.py Streamlit Chat App
Components 16
User Interface
- Streamlit Chat App · Chat, uploads, thread controls, and approval/resume actions.
app.py - Thread Manager · Manages user-facing histories, titles, and renames.
backend/threads.py - User
LangGraph Control Plane
- Conversation State · Defines conversation state and prompting context.
backend/state.py - Runtime Configuration · Initializes models, embeddings, databases, memory, and checkpoints.
backend/config.py - Compiled Workflow · Routes agent, tools, RAG, memory, and summarization stages.
backend/graph.py - Agent Decision & HITL · Chooses response paths and gates Python execution with interrupts.
backend/agent.py - Tool Executor · Weather, stock, currency, date, search, and Python tools.
backend/tools.py
Knowledge & Memory
- RAG Subgraph · Ingests documents and retrieves graded chunks for answers.
backend/rag.py - History Compression · Replaces older messages with summaries as context grows.
backend/summarization.py - Long-Term Memory · Retrieves facts before responses and deduplicates facts afterward.
backend/memory.py - Checkpoint Store · Stores short-term graph conversation continuity.
External Services
- PostgreSQL + pgvector · Persists embedded document chunks.
- Groq LLM · Hosts openai/gpt-oss-120b for agent decisions and generation.
- Gemini Embeddings
- External Tool APIs · Supports weather and web-search style tools.
Connections 21
- User → Streamlit Chat App: chat, uploads, approval
- Streamlit Chat App → Thread Manager: manages chats
- Streamlit Chat App → Compiled Workflow: invoke / resume
- Compiled Workflow → Conversation State: reads and updates
- Compiled Workflow → Agent Decision & HITL: agent step
- Compiled Workflow → RAG Subgraph: document retrieval path
- Compiled Workflow → History Compression: compresses history
- Compiled Workflow → Long-Term Memory: retrieve and persist facts
- Agent Decision & HITL → Tool Executor: tool calls
- Tool Executor → Agent Decision & HITL: safe results loop
- Agent Decision & HITL → Streamlit Chat App: Python interrupt for approval
- Tool Executor → External Tool APIs: weather and search requests
- RAG Subgraph → Gemini Embeddings: embed chunks and queries
- RAG Subgraph → PostgreSQL + pgvector: ingest and retrieve chunks
- Agent Decision & HITL → Groq LLM: decisions and answers
- Runtime Configuration → Compiled Workflow: configures runtime
- Runtime Configuration → Checkpoint Store: initializes
- Runtime Configuration → PostgreSQL + pgvector: connects
- Runtime Configuration → Groq LLM: configures
- Runtime Configuration → Gemini Embeddings: configures
- Compiled Workflow → Checkpoint Store: checkpoint state