How prakashwebdevx/ai-business-research-agent-backend works
Purpose and entry points
AI Business Research Agent answers business questions using internal SQLite data, live web research, or both. CLI entry is app.py; dashboard entry is FastAPI backend/main.py plus React frontend/src/main.jsx. The orchestrator is shared by both interfaces.
Agent orchestration and routing
agent.py defines BusinessAgent, a LangChain/Groq agent that uses centralized instructions to decide whether to invoke SQL, web search, both, or answer directly. prompts.py owns agent prompt templates; tools.py registers the callable SQL and web adapters. The primary control boundary is orchestrator versus specialized sub-agents.
Paths: agent.py, prompts.py, tools.py
Internal-data research pipeline
The SQL tool delegates natural-language database questions to sql_agent.py, which uses LangChain SQL tooling to generate and execute read-only queries. Persistent business entities and first-run sample-data initialization are owned by SQLAlchemy in database.py; the runtime database is SQLite (database/employee.db, generated rather than present in this tree). SQL safety explicitly excludes mutation and schema-changing commands.
Paths: sql_agent.py, database.py, schemas.py
External research pipeline
The web tool routes to web_agent.py, which calls Tavily Search and uses Groq to summarize results rather than return raw search payloads. This is a service boundary: Tavily provides external retrieval, while Groq powers web synthesis and the central routing/final response process.
Paths: web_agent.py, tools.py, config.py
Structured results and API layer
Agent/tool execution is represented by shared result and chart schemas, enabling rich dashboard output beyond plain text. FastAPI exposes chat, session, and export operations; it adapts requests/responses and maintains in-memory conversational sessions. A key concurrency limitation is the shared agent instance with per-session memory resynchronization rather than isolated multi-user execution.
Paths: schemas.py, backend/main.py, backend/session_store.py
Dashboard rendering and exports
React/Vite consumes the REST API through frontend/src/lib/api.js. App.jsx owns routing/layout; ChatView.jsx renders interaction flow, while message-level components render SQL tables, charts, and sourced web research. Backend export handling serializes SQL results as CSV, Excel, or JSON.
Paths: frontend/src/App.jsx, frontend/src/components/ChatMessage.jsx, frontend/src/lib/api.js; backend/export_utils.py
Material runtime and infrastructure
Backend runtime is Python 3.10+ with LangChain, Groq, Tavily, SQLAlchemy/SQLite, FastAPI/Uvicorn, pandas, and openpyxl. Frontend runtime is React 19/Vite, with Tailwind and Recharts. Required external credentials are Groq and Tavily API keys loaded from environment configuration; the Vite development server proxies /api/* to FastAPI.
Paths: requirements.txt, frontend/package.json, frontend/vite.config.js
Main entry points: agent.py BusinessAgent · main.py FastAPI backend · tools.py Tool registry · web_agent.py Web sub-agent
Components 18
Interfaces
- CLI entry
app.py
Agent Core
- BusinessAgent · Routes questions to SQL, web, both, or direct answers.
agent.py - Agent prompts
prompts.py - Tool registry
tools.py - Result schemas · Defines structured results and chart output.
schemas.py
Internal Research
- SQL sub-agent · Generates and executes read-only SQL queries.
sql_agent.py - Business database · Owns entities and first-run sample-data initialization.
database.py - Employee SQLite DB · Generated runtime file at database/employee.db.
External Research
- Web sub-agent · Summarizes search findings with Groq.
web_agent.py - Tavily Search
- Groq · Powers routing, final answers, and web synthesis.
API & Sessions
- FastAPI backend · Exposes chat, session, and export operations.
backend/main.py - Session store · Resynchronizes shared agent memory per session.
backend/session_store.py - Export serializer
backend/export_utils.py
Dashboard
- React entry
frontend/src/main.jsx - App layout
frontend/src/App.jsx - Chat rendering · Renders tables, charts, and sourced web research.
frontend/src/components/ChatMessage.jsx - REST API client
frontend/src/lib/api.js
Connections 19
- CLI entry → BusinessAgent: invokes
- FastAPI backend → BusinessAgent: shared agent
- FastAPI backend → Session store: maintains
- FastAPI backend → Export serializer: exports results
- BusinessAgent → Agent prompts: uses
- BusinessAgent → Tool registry: selects tools
- BusinessAgent → Result schemas: returns
- BusinessAgent → Groq: LLM inference
- Tool registry → SQL sub-agent: SQL adapter
- Tool registry → Web sub-agent: web adapter
- SQL sub-agent → Business database: SQLAlchemy tooling
- Business database → Employee SQLite DB: persists to
- Web sub-agent → Tavily Search: searches
- Web sub-agent → Groq: summarizes
- React entry → App layout: mounts
- App layout → Chat rendering: hosts
- Chat rendering → REST API client: uses
- REST API client → FastAPI backend: /api requests · Vite proxies API requests to FastAPI during development.
- FastAPI backend → Result schemas: adapts structured output