How saadiaboussiar/ai-agent works
Purpose and Entry Points
This is a Python backend for an AI agent that answers sales and inventory questions by combining an LLM with database-backed tools. The HTTP entry point is src/api/main.py; chat handling is exposed through src/api/routes/chat.py. There is no implemented frontend beyond frontend/.gitkeep.
API Boundary
The API layer defines request/response schemas in src/api/schemas.py and routes requests from src/api/routes/chat.py into the agent. This is the external boundary for clients and should remain responsible for transport validation and response serialization, not business/tool logic.
Agent Orchestration
src/agent/orchestrator.py is the central control-flow component: it coordinates conversation state, model interaction, and tool invocation. src/agent/system_prompt.py supplies agent behavior/instructions, while src/agent/memory.py manages conversational context. The orchestrator is the boundary between natural-language reasoning and deterministic business-data operations.
LLM and Tool-Calling Runtime
src/agent/ollama_client.py integrates with an Ollama model runtime. src/agent/tool_executor.py executes tool calls selected by the agent, using the tool registry in src/tools/registry.py. Tool definitions share a base abstraction in src/tools/base.py, allowing the model-facing tool catalog to be assembled separately from API and database code.
Sales and Stock Tool Domains
Tools are partitioned by business domain and operation type:
• Sales: src/tools/sales/consultation_tools.py, src/tools/sales/traitement_tools.py, src/tools/sales/recommandation_tools.py
• Stock: src/tools/stock/consultation_tools.py, src/tools/stock/traitement_tools.py, src/tools/stock/recommandation_tools.py
This separation distinguishes read/consultation queries, analytical processing (“traitement”), and recommendation workflows. These modules are the application-facing contracts for SQL capabilities.
Database Layer and Analytics Logic
src/db/connection.py owns database connectivity. Database initialization is in src/db/setup.sql. Business analytics are implemented as SQL views and functions rather than embedded in the agent:
• Sales reporting: src/db/sales/consultation/v_sales_detail.sql, src/db/sales/traitement/fn_sales_statistics.sql, src/db/sales/recommandation/fn_sales_alert_decline.sql
• Stock reporting: src/db/stock/consultation/v_low_stock_products.sql, src/db/stock/traitement/fn_stock_report_core.sql, src/db/stock/recommandation/fn_reorder_suggestions.sql
The key data boundary is tool module → database connection → versioned SQL function/view. The LLM should request named capabilities through tools rather than compose arbitrary database access.
End-to-End Flow and Dependencies
A client sends a chat request to src/api/routes/chat.py; the route invokes src/agent/orchestrator.py; the orchestrator uses src/agent/ollama_client.py to obtain a response or tool call; src/agent/tool_executor.py dispatches through src/tools/registry.py; the selected sales/stock tool queries the database via src/db/connection.py; results return to the orchestrator for a final model response.
Material external services are an Ollama-compatible LLM runtime and the relational database provisioned from src/db/setup.sql. Python dependencies are declared in requirements.txt; environment-based runtime configuration is indicated by .env.example.
Main entry points: connection.py Database connection · setup.sql Database setup · orchestrator.py Agent orchestrator · chat.py Chat route
Components 21
API boundary
- HTTP entry point
src/api/main.py - Chat route
src/api/routes/chat.py - Request/response schemas · Validates transport payloads and serializes responses.
src/api/schemas.py
Agent runtime
- Agent orchestrator · Coordinates state, model responses, and tool invocation.
src/agent/orchestrator.py - System prompt
src/agent/system_prompt.py - Conversation memory
src/agent/memory.py - Ollama client
src/agent/ollama_client.py - Tool executor
src/agent/tool_executor.py
Business tools
- Tool registry · Assembles model-facing tools from the shared tool abstraction.
src/tools/registry.py - Sales tool suite · Consultation, analytics, and recommendation capabilities.
- Stock tool suite · Consultation, analytics, and recommendation capabilities.
Data layer
- Database connection
src/db/connection.py - Sales detail view
src/db/sales/consultation/v_sales_detail.sql - Sales statistics
src/db/sales/traitement/fn_sales_statistics.sql - Sales decline alerts
src/db/sales/recommandation/fn_sales_alert_decline.sql - Low-stock view
src/db/stock/consultation/v_low_stock_products.sql - Stock report core
src/db/stock/traitement/fn_stock_report_core.sql - Reorder suggestions
src/db/stock/recommandation/fn_reorder_suggestions.sql - Database setup
src/db/setup.sql - Relational database
Other components
- Ollama runtime
Connections 32
- HTTP entry point → Chat route: serves
- Chat route → Request/response schemas: validates with
- Chat route → Agent orchestrator: chat request
- Agent orchestrator → System prompt: uses
- Agent orchestrator → Conversation memory: reads and updates
- Agent orchestrator → Ollama client: model request
- Agent orchestrator → Tool executor: tool call
- Ollama client → Ollama runtime: inference
- Tool executor → Tool registry: resolves
- Tool registry → Sales tool suite: registers
- Tool registry → Stock tool suite: registers
- Sales tool suite → Database connection: queries via
- Stock tool suite → Database connection: queries via
- Database connection → Sales detail view: executes
- Database connection → Sales statistics: executes
- Database connection → Sales decline alerts: executes
- Database connection → Low-stock view: executes
- Database connection → Stock report core: executes
- Database connection → Reorder suggestions: executes
- Sales detail view → Relational database: reads
- Sales statistics → Relational database: reads
- Sales decline alerts → Relational database: analyzes
- Low-stock view → Relational database: reads
- Stock report core → Relational database: analyzes
- Reorder suggestions → Relational database: analyzes
- Database setup → Relational database: provisions
- Database setup → Sales detail view: installs
- Database setup → Sales statistics: installs
- Database setup → Sales decline alerts: installs
- Database setup → Low-stock view: installs
- Database setup → Stock report core: installs
- Database setup → Reorder suggestions: installs