Skip to content

smshelar/northwind-crew-ai

Repository files navigation

πŸ€– Northwind Agentic AI

A multi-agent AI system that answers business questions in plain English β€” powered by CrewAI, Google Gemini, and Streamlit.

Python CrewAI Streamlit SQLite


πŸ“Œ Overview

Northwind Agentic AI is a multi-agent agentic AI system that lets users query the Northwind business database using plain English β€” no SQL knowledge required.

The system uses 3 specialised AI agents working in sequence:

  1. SQL Writer Agent β€” Translates a natural language question into a SQLite query
  2. SQL Executor Agent β€” Runs the query against the Northwind database
  3. Visualizer Agent β€” Generates a chart and business insight from the results

All agents are orchestrated by CrewAI and presented through a Streamlit web interface with a custom dark theme.


🎯 Problem Statement

Business data locked inside databases is inaccessible to non-technical users. Analysts spend time writing repetitive queries instead of generating insights. This project solves that by:

  • Allowing anyone to ask business questions in plain English
  • Automatically generating accurate SQL from natural language
  • Turning raw data into visualisations and insights instantly
  • Demonstrating a real-world agentic AI pipeline architecture

πŸ—οΈ Architecture

User (Plain English Question)
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Streamlit UI  β”‚  ← app.py
β”‚   (Dark Theme)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              CrewAI Orchestrator            β”‚
β”‚           crew/northwind_crew.py            β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚              β”‚               β”‚
       β–Ό              β–Ό               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Agent 1 β”‚   β”‚  Agent 2 β”‚   β”‚   Agent 3    β”‚
β”‚SQL Writer│──▢│Executor  │──▢│  Visualizer  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚              β”‚               β”‚
       β–Ό              β–Ό               β–Ό
  SQL Query      SQLite DB       Chart + Insight
                Northwind.db    (matplotlib)

πŸ“ Folder Structure

Northwind-Agentic-AI/
β”‚
β”œβ”€β”€ agents/                     # AI Agent definitions
β”‚   β”œβ”€β”€ sql_writer.py           # Agent 1: writes SQL queries
β”‚   β”œβ”€β”€ executer.py             # Agent 2: executes SQL
β”‚   └── visualizer.py           # Agent 3: generates charts & insights
β”‚
β”œβ”€β”€ tasks/                      # Task instructions for each agent
β”‚   β”œβ”€β”€ sql_task.py
β”‚   β”œβ”€β”€ execute_task.py
β”‚   └── visualizer_task.py
β”‚
β”œβ”€β”€ crew/                       # CrewAI pipeline orchestrator
β”‚   └── northwind_crew.py
β”‚
β”œβ”€β”€ tools/                      # Custom CrewAI tools
β”‚   └── db_tool.py              # ExecuteSQLTool β€” runs SQL on Northwind DB
β”‚
β”œβ”€β”€ utils/                      # Utilities
β”‚   └── llm_factory.py          # LLM switcher: Gemini / OpenAI / Groq
β”‚
β”œβ”€β”€ northwind_db/               # Database manager
β”‚   └── db_manager.py           # Auto-downloads northwind.db if missing
β”‚
β”œβ”€β”€ data/                       # Database file (auto-generated)
β”‚   └── northwind.db
β”‚
β”œβ”€β”€ assets/                     # Frontend styling
β”‚   └── styles.css              # Dark theme CSS for Streamlit
β”‚
β”œβ”€β”€ tests/                      # Offline tests (no API needed)
β”‚   └── test_db.py
β”‚
β”œβ”€β”€ app.py                      # Streamlit web application
β”œβ”€β”€ .env                        # API keys (not committed to Git)
β”œβ”€β”€ .gitignore
β”œβ”€β”€ requirements.txt
└── README.md

βš™οΈ Tech Stack

Layer Technology
AI Framework CrewAI
LLM β€” Primary Google Gemini 2.0 Flash Lite
LLM β€” Alternative Groq (Llama 3.1 8b Instant)
LLM β€” Alternative OpenAI GPT-4o Mini
LLM Routing LangChain + LiteLLM
Database SQLite (Northwind dataset)
Data Processing Pandas
Visualisation Matplotlib
Web UI Streamlit
Language Python 3.11

πŸš€ Getting Started

Prerequisites

  • Python 3.11 or higher
  • Node.js (optional β€” only if regenerating the PowerPoint)
  • A free API key from one of: Google AI Studio, Groq, or OpenAI

1. Clone the repository

git clone https://github.com/smshelar/northwind-agentic-ai.git
cd northwind-agentic-ai

2. Create a virtual environment

python3.11 -m venv venv
source venv/bin/activate        # Mac/Linux
venv\Scripts\activate           # Windows

3. Install dependencies

pip install --upgrade pip
pip install -r requirements.txt

4. Set up your API key

Create a .env file in the project root:

# Choose ONE provider and add its key
GOOGLE_API_KEY=your_gemini_key_here
LLM_PROVIDER=google

# Or use Groq (free, fast)
# GROQ_API_KEY=your_groq_key_here
# LLM_PROVIDER=groq

# Or use OpenAI
# OPENAI_API_KEY=your_openai_key_here
# LLM_PROVIDER=openai

πŸ’‘ Get a free Gemini API key at aistudio.google.com β€” no credit card required.

5. Run offline tests (no API needed)

python tests/test_db.py

All 7 tests should pass β€” this confirms your database and chart logic work before spending API quota.

6. Run the app

streamlit run app.py

Open http://localhost:8501 in your browser.


πŸ’¬ Example Questions

Try asking these in the app:

Sales Analysis

  • What were the top 5 products by total sales revenue?
  • Which product category generates the most revenue?
  • What is the total revenue generated per year?

Customer Analysis

  • Which customers have placed the most orders?
  • Who are the top 10 customers by total amount spent?
  • Which country has the most customers?

Employee Performance

  • Which employee has handled the most orders?
  • What is the total sales revenue generated by each employee?

Inventory

  • Which products are running low on stock?
  • Which products have never been ordered?

⚠️ The Northwind database contains data from 1996, 1997, and 1998 only.


πŸ”„ Switching LLM Providers

Change the LLM_PROVIDER in your .env file β€” no code changes needed:

Provider .env setting Model used Free?
Google Gemini LLM_PROVIDER=google gemini-2.0-flash-lite βœ… Yes
Groq LLM_PROVIDER=groq llama-3.1-8b-instant βœ… Yes
OpenAI LLM_PROVIDER=openai gpt-4o-mini ❌ Paid

πŸ§ͺ Running Tests Without API

The tests/test_db.py file validates everything that doesn't need an LLM:

python tests/test_db.py

This tests:

  • βœ… Database connection
  • βœ… Available years in the data
  • βœ… Table names (including [Order Details] with space)
  • βœ… Top products query
  • βœ… Top customers query
  • βœ… Monthly revenue query
  • βœ… Chart rendering (saves test_chart.png)

πŸ› Common Issues & Fixes

Error Fix
ModuleNotFoundError: crewai Run pip install -r requirements.txt with Python 3.11 venv activated
GOOGLE_API_KEY not found Check .env has no quotes or spaces around the =
Rate limit exceeded Wait 1 minute and retry, or switch LLM_PROVIDER in .env
No such table: OrderDetails Use [Order Details] with square brackets β€” the schema is fixed in sql_writer.py
Executor returned non-JSON Known issue with small LLMs β€” SQL now executes directly in Python
Python 3.9 venv Delete venv, recreate with python3.11 -m venv venv

πŸ—ΊοΈ Roadmap

Phase 1 β€” Short Term

  • Conversation memory β€” remember previous questions in the session
  • Query history β€” save and replay past questions
  • Export results to CSV / Excel
  • Better error messages with suggested rephrasing

Phase 2 β€” Medium Term

  • Support for multiple databases beyond Northwind
  • Voice input β€” ask questions by speaking
  • Dashboard mode β€” pin favourite charts
  • Scheduled email reports

Phase 3 β€” Long Term

  • Fine-tuned SQL model on company-specific schemas
  • Role-based access control
  • Predictive analytics and trend forecasting
  • Integration with Tableau / Power BI

πŸ“Š Northwind Database Schema

Customers      ──┐
                 β”œβ”€β”€β–Ά Orders ──▢ [Order Details] ──▢ Products ──▢ Categories
Employees      β”€β”€β”˜                                          └──▢ Suppliers
Shippers ──────────▢ Orders

Key tables:

  • Customers β€” company info, country, city
  • Orders β€” order dates, customer, employee, shipper
  • [Order Details] β€” products, quantities, prices per order
  • Products β€” product names, categories, stock levels
  • Employees β€” sales rep info

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/my-feature
  3. Commit your changes: git commit -m "Add my feature"
  4. Push to the branch: git push origin feature/my-feature
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License β€” see the LICENSE file for details.


πŸ™ Acknowledgements


Built with πŸ’š using CrewAI + Gemini + Streamlit
A practical demonstration of Agentic AI for business intelligence

About

A multi-agent AI system that answers business questions in plain English using CrewAI, Gemini and Streamlit

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors