A comprehensive multi-agent system for analyzing sales data using the OpenAI Agents SDK. Features AI-powered data review, cleaning, analysis, forecasting, and a conversational query interface.
-
7 Specialized Agents:
- DataReviewer: Profiles data quality and generates review reports
- DataCleaner: Cleans and transforms raw data (now with bulk fix tools)
- Visualizer: Dedicated agent for generating 22+ Matplotlib charts
- DataAnalyst (Strategist): Interprets charts and writes narrative reports
- Forecaster: Generates forecasts using ARIMA, Holt-Winters, and Moving Average
- ConversationalAgent: Natural language query interface
- Orchestrator: Coordinates the multi-agent pipeline
-
Multi-LLM Support: OpenAI, Anthropic (Claude), and DeepSeek via LiteLLM
-
Built-in Tracing: Full observability via OpenAI Traces Dashboard
-
Interactive Frontend: Gradio-based UI with 7 tabs
-
MongoDB Integration: Direct database connectivity for ~10,000 sales records
- Python 3.10+
- MongoDB running locally at
mongodb://localhost:27017 - Database:
superstorewithsalescollection - API keys for at least one LLM provider
-
Clone/navigate to the project:
cd /home/robert/Coding/sales -
Activate conda environment:
conda activate ai
-
Install dependencies (if not already installed):
pip install -r requirements.txt
-
Configure API keys:
cp .env.example .env # Edit .env and add your API keys
Create a .env file with your API keys:
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
DEEPSEEK_API_KEY=sk-...
MONGODB_URI=mongodb://localhost:27017
DEFAULT_LLM_PROVIDER=openai
DEFAULT_LLM_MODEL=gpt-4-turbo
OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA=falseVerify your configuration:
python main.py --check-setupExecute all agents in sequence (Data Review β Cleaning β Analysis β Forecasting). Note: Pipeline now runs each agent with an isolated context to prevent token overflow, while linking them via a shared Trace Group ID.
python main.py --run-pipelineWith DeepSeek (Cost-Effective & Optimized):
python main.py --run-pipeline --provider deepseek --model deepseek-chatpython main.py --run-agent data_reviewer
python main.py --run-agent data_analyst --provider openai --model gpt-4opython app.pyThen open http://localhost:7860 in your browser.
The web interface includes 7 tabs:
| Tab | Description |
|---|---|
| Dashboard | KPIs, stats, and pipeline runner |
| Data Review | Data quality report viewer |
| Data Cleaning | Interactive proposal workflow with Download CSV and Apply Changes buttons |
| Analysis | Sales analysis report with inline visualizations (supports split reports automatically) |
| Forecasting | Forecast report and model comparisons |
| Query | Conversational AI interface for data questions |
| Settings | Configuration and status information |
All agent executions are automatically traced. View traces at:
https://platform.openai.com/traces
The system uses Isolated Contexts with Grouped Traces:
- Each agent gets a fresh context window (preventing token overflow)
- All agents in a pipeline run share a
group_idfor unified viewing - Supports OpenAI, Anthropic, and DeepSeek via LiteLLM
sales/
βββ sales_agents/
β βββ __init__.py
β βββ tools.py # Shared function tools (optimized schemas)
β βββ data_reviewer.py # Agent 1
β βββ data_cleaner.py # Agent 2
β βββ visualizer.py # Agent 3A (New!)
β βββ data_analyst.py # Agent 3B (Strategist)
β βββ forecaster.py # Agent 4
β βββ conversational.py # Agent 5
β βββ orchestrator.py # Agent 6
βββ outputs/
β βββ reports/ # Generated markdown reports
β βββ visualizations/ # Generated PNG charts
βββ config.py # Configuration module
βββ main.py # CLI entry point
βββ app.py # Gradio frontend
βββ requirements.txt # Dependencies
βββ .env.example # Environment template
βββ README.md # This file
After running the pipeline:
Reports (outputs/reports/):
data_review_report.md- Data quality assessmentcleaning_proposal.csv- Record-level cleaning proposals (downloadable)cleaning_proposal.md- Cleaning proposal summaryanalysis.mdoranalysis_part*.md- Comprehensive sales analysis (auto-merged if split)forecast_report.md- Forecasting methodology and resultsexecutive_summary.md- High-level summary (when using orchestrator)
Visualizations (outputs/visualizations/):
sales_by_category.png- Category breakdownsales_by_region.png- Regional distributionsales_trend_monthly.png- Monthly trendstop_products.png- Top performing productsforecast_overall.png- Forecast with confidence intervalsmodel_comparison.png- Forecast model metrics
MongoDB Collections:
sales- Original raw data (9,800 records), updated in-place by Data Cleaneranalysis_results- Analysis metricssales_forecasts- Forecast predictionsquery_history- Conversation logs
We have moved beyond simple descriptive analytics ("What happened?") to Diagnostic Analytics ("Why it happened?") and added rigorous testing tools.
We now ingest external data to explain sales trends (scripts/seed_context_data.py):
- Marketing Campaigns: 180+ campaigns correlated with Sales Regions/Dates.
- Economic Indicators: Inflation, Unemployment, and Consumer Confidence data.
To verify the Data Cleaner, we use a tool that injects intended defects (scripts/inject_bad_data.py):
- Infected: 100 random records (1% of data).
- Defects: Typos ("Calfornia"), Nulls, Outliers ($1M sales), Duplicates, and Bad Dates.
- Audit: Generates
outputs/reports/chaos_log.csvfor transparency.
Profiles the raw sales collection and generates a quality report including:
- Record counts and schema validation
- Missing value analysis
- Duplicate detection
- Date range coverage
- Sales statistics
Refactored to support an Interactive Proposal Workflow:
- Analyze: Scans the
salescollection for specific quality issues (and Chaos defects!). - Bulk Fix Tools: Uses
fix_date_formatsto instantly correct thousands of date errors. - Propose: Generates a CSV proposal (
cleaning_proposal.csv) listing record-level changes. - Review: Users can download the CSV from the UI for external review.
- Apply: Dedicated "Apply Changes" button in the UI executes the updates.
A specialist agent dedicated purely to code generation for visualization:
- Focused Scope: Does not write reports or analyze text.
- Output: Generates 22 mandated visualizations in
outputs/visualizations/. - Chart Types: Complex overlays (Sales vs Marketing), Heatmaps, Dual-axis plots.
- Tools: Matplotlib/Seaborn optimization.
Now operates as a Lead Strategist, consuming the output of the Visualizer:
- Role: Pure diagnostic analysis and narrative writing.
- Inputs: Reads the visualizations generated by Agent 3A.
- Diagnostic: Cross-references Sales with Marketing & Economic data to explain the "Why".
- Report: Writes a 2,000-word analysis.md with executive summary and strategic recommendations.
- No Code: Focusing purely on business intelligence, not plotting code.
Generates sophisticated sales forecasts with Narrative Context:
- Multi-Model Approach: ARIMA, Holt-Winters, Moving Average.
- Diagnostic Narrative: Explicitly factors in external context (e.g., "Forecast risks high due to falling consumer confidence").
- Visualizations: Overlays forecast with context data where possible.
- Professional Charts: Clean formatting, clear legends, and confidence intervals.
Answers natural language questions by querying the raw sales collection (~9,800 records):
- "What were total sales in 2017?"
- "Top 5 products by revenue"
- "Compare sales between regions"
- API keys should be stored in
.env(not committed to git) - The
.envfile is ignored by default - Sensitive data can be excluded from traces via
trace_include_sensitive_data=False
MIT License