Natural language-to-code AI agent for business analytics with automated visualizations
Challenge: Business analysts spend hours writing SQL queries and Python code to extract insights from sales data. Non-technical stakeholders struggle to get timely answers without developer support. Traditional BI dashboards are rigid, require upfront configuration, and don't adapt to ad-hoc questions.
Solution: Sales Dashboard Agent converts natural language business questions into executable Python analytics code. Ask questions in plain English like "What were our top 5 products by revenue in Q4?" and get instant answers with automated visualizations. The "code-as-plan" pattern provides full transparency - you can see and verify the exact analytics code generated by the AI.
Gemini 2.0 Flash analyzes business questions and understands intent: filtering, aggregation, grouping, visualization requests.
AI generates executable Python code with TinyDB queries dynamically based on your data schema and question context.
Code runs in sandboxed environment with read-only access. Only safe operations allowed (aggregations, filtering, visualization).
When you request charts, AI generates complete Matplotlib/Seaborn code with proper labels, titles, and formatting.
User Question → AI Code Generation → Safe Execution → Results
| Query Category | Example Questions |
|---|---|
| Revenue & Profit |
"What were total sales in November 2024?" "Which region generated the most profit?" "Show revenue trends by month in a line chart" |
| Product Analysis |
"Top 5 best-selling products by revenue" "Which products have profit margins above 30%?" "Show product performance in a bar chart" |
| Customer Insights |
"Which customer spent the most in electronics?" "Top 10 customers by quantity ordered" "Show customer distribution by region" |
| Category Performance |
"Average order value for each category" "Revenue breakdown by product category" "Which category has the lowest margin?" |
| Margin Analysis |
"What products have margins below 10%?" "Calculate profit margin by region" "Show margin distribution in table format" |
Prompt Engineering & Safe Execution:
answer_text with human-friendly summarySales Order Schema (Customizable):
How to use the agent: