How pawan300/enterprise_bot works
Purpose and Entry Points
This is a local, enterprise-style multi-agent Q&A prototype for synthetic consumer-goods data. It answers structured KPI questions, document-based questions, web-search questions, and bounded computations, producing grounded citations and debug metadata. Interactive entry points are scripts/chat_cli.py and app/streamlit_app.py; the latter is an inspection UI rather than a production frontend.
Orchestration and Conversation State
QnaAgent coordinates each turn: it preserves multi-turn context, resolves follow-ups, performs intent/routing decisions, invokes selected agents, and returns formatted answers plus diagnostics. Conversation state and follow-up generation are separate concerns, enabling UI/CLI clients to remain thin.
Paths: src/qna_agent/orchestrator/agent.py, src/qna_agent/orchestrator/conversation.py, src/qna_agent/orchestrator/followups.py.
Deterministic Understanding and Metadata Resolution
Before model-driven decisions, deterministic NLU extracts entities, corrects aliases, interprets temporal language, and detects language. The metadata store supplies KPI/dimension definitions, hierarchy relationships, and document metadata; this supports explicit parent-level fallback (for example, an unavailable sub-region resolving to its parent) and overrides conflicting LLM classifications when entity/KPI resolution is decisive.
Paths: src/qna_agent/nlu/entities.py, src/qna_agent/nlu/temporal.py, src/qna_agent/catalog/metadata_store.py.
Intent Routing and Specialized Execution
The router selects up to four specialized agents based on question intent and evidence requirements. Structured queries target SQLite; unstructured queries retrieve from generated Markdown documents; web search uses live search or an offline corpus; coding handles calculations. Multi-evidence “why” questions can run structured and unstructured paths together.
Paths: src/qna_agent/orchestrator/intent.py, src/qna_agent/orchestrator/router.py, src/qna_agent/subagents/structured_agent.py, src/qna_agent/subagents/unstructured_agent.py, src/qna_agent/subagents/websearch_agent.py, src/qna_agent/subagents/coding_agent.py.
Evidence Stores and Data Boundary
The system’s business corpus is reproducibly generated as a matched structured/unstructured dataset: SQLite warehouse records, Markdown documents, catalog/alias metadata, and an offline web corpus. The shared generator makes document narratives and warehouse events overlap by construction. Agent boundaries should treat retrieved rows, snippets, search results, and computation outputs as evidence passed upstream rather than direct final answers.
Paths: data/generate_data.py, data/warehouse.db, data/documents/, data/catalog.json, data/websearch_corpus.json.
Synthesis, Validation, and Presentation
The synthesizer receives only gathered evidence, drafts a cited response, then deterministically checks that answer numbers are supported. A failed validation triggers retry and ultimately a guaranteed-correct templated fallback. Formatting adds citations, assumptions/fallback notes, and debug fields such as routed agents, SQL, validation retry, latency, and cost.
Paths: src/qna_agent/orchestrator/synthesizer.py, src/qna_agent/orchestrator/formatting.py, src/qna_agent/orchestrator/agent.py.
Runtime, Model, and Security Boundaries
Python 3.11+ runs the application. Ollama hosting local llama3.1:8b is the sole LLM dependency, isolated behind one client wrapper; no cloud model/API key is required. SQL generated for the warehouse is constrained by an introspected allow-list, read-only execution, and timeout. Generated computation code is AST-restricted and runs with minimal builtins.
Paths: src/qna_agent/llm/ollama_client.py, src/qna_agent/security/sql_guard.py, src/qna_agent/security/sandbox.py, requirements.txt.
Main entry points: agent.py Q&A coordinator · router.py Intent router · synthesizer.py Cited synthesis · generate_data.py Corpus generator
Components 21
Interactive clients
- Chat CLI
scripts/chat_cli.py - Inspection UI · Local UI for inspecting answers and diagnostics.
app/streamlit_app.py
Orchestration
- Q&A coordinator · Coordinates context, routing, evidence collection, and response delivery.
src/qna_agent/orchestrator/agent.py - Conversation state
src/qna_agent/orchestrator/conversation.py - Follow-up resolution
src/qna_agent/orchestrator/followups.py - Deterministic NLU · Extracts entities, aliases, temporal terms, and language before model decisions.
src/qna_agent/nlu/entities.py - Metadata store · Supports definitions, aliases, document metadata, and parent-level fallback.
src/qna_agent/catalog/metadata_store.py - Intent router · Selects up to four evidence-gathering agents.
src/qna_agent/orchestrator/router.py - Cited synthesis · Drafts answers from gathered evidence and citations.
src/qna_agent/orchestrator/synthesizer.py - Answer formatting · Adds citations, fallback notes, and debug metadata.
src/qna_agent/orchestrator/formatting.py
Specialized execution
- Structured agent
src/qna_agent/subagents/structured_agent.py - Document agent
src/qna_agent/subagents/unstructured_agent.py - Web-search agent
src/qna_agent/subagents/websearch_agent.py - Coding agent
src/qna_agent/subagents/coding_agent.py
Generated evidence corpus
Matched structured and document data are generated together.
- SQLite warehouse
data/warehouse.db - Markdown documents
- Offline web corpus
data/websearch_corpus.json - Corpus generator · Generates overlapping warehouse and document narratives.
data/generate_data.py
Runtime safeguards
- Ollama client · Isolates the local llama3.1:8b model dependency.
src/qna_agent/llm/ollama_client.py - SQL guard · Allow-lists introspected SQL and enforces timeout.
src/qna_agent/security/sql_guard.py - Code sandbox · Runs generated computations with minimal builtins.
src/qna_agent/security/sandbox.py
Connections 27
- Chat CLI → Q&A coordinator: submits turns
- Inspection UI → Q&A coordinator: submits turns
- Q&A coordinator → Conversation state: loads and saves context
- Q&A coordinator → Follow-up resolution: resolves references
- Q&A coordinator → Deterministic NLU: normalizes question
- Deterministic NLU → Metadata store: resolves entities and hierarchy
- Q&A coordinator → Intent router: routes intent
- Metadata store → Intent router: overrides decisive classification
- Intent router → Structured agent: structured intent
- Intent router → Document agent: document intent
- Intent router → Web-search agent: web intent
- Intent router → Coding agent: calculation intent
- Structured agent → SQL guard: constrains SQL
- SQL guard → SQLite warehouse: read-only queries
- Document agent → Markdown documents: retrieves snippets
- Web-search agent → Offline web corpus: offline search
- Coding agent → Code sandbox: executes bounded code
- Structured agent → Cited synthesis: rows as evidence
- Document agent → Cited synthesis: snippets as evidence
- Web-search agent → Cited synthesis: results as evidence
- Coding agent → Cited synthesis: outputs as evidence
- Cited synthesis → Ollama client: drafts cited answer
- Cited synthesis → Answer formatting: validated answer or fallback
- Answer formatting → Q&A coordinator: formatted response
- Corpus generator → SQLite warehouse: generates records
- Corpus generator → Markdown documents: generates narratives
- Corpus generator → Offline web corpus: generates offline corpus