Skip to content

An AI agent that answers natural language questions about e-commerce data by converting queries to SQL, fetching results from a sales database, and delivering real-time responses via API.

Notifications You must be signed in to change notification settings

nitishb-dev/ANARIX-AI-Agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

43 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 ANARIX AI Agent (FastAPI + Gemini + MySQL)


πŸ“˜ Project Overview

This project focuses on building an AI agent designed to answer e-commerce data questions. The agent is capable of interpreting natural language questions, generating and executing SQL queries on provided datasets, and responding with accurate answers.


πŸš€ Features

This ANARIX AI Agent provides a powerful and intuitive way to interact with your e-commerce data:

  • Natural Language Querying: The AI agent can understand and answer any question related to the provided e-commerce data.

  • API-Driven Interaction: Questions are received via API endpoints, and the agent processes them to return accurate responses.

  • SQL Conversion & Execution: The agent includes logic to convert natural language questions into SQL queries, fetch answers from the database, and return them in a human-readable format.


🧠 How It Works

  1. User Input β€” Ask a question at the /ask API endpoint.
  2. Gemini Processing β€” Generates a SQL query or chart instruction.
  3. SQL Execution β€” FastAPI runs the SQL on the connected MySQL database.
  4. Chart Generation β€” If requested, Matplotlib generates a chart and saves it as an image file in assets/screenshots/.
    The API response includes the path to the chart image (e.g., "chart_path": "assets/screenshots/chart.png"), which can be viewed or served via your web server.
  5. Response β€” The agent returns the answer, SQL query, query result, and chart path (if applicable).

πŸ“¦ Project Structure

πŸ“‚ANARIX-AI-AGENT
β”œβ”€β”€ πŸ“‚pycache
β”œβ”€β”€ πŸ“‚assets
β”‚ └── πŸ“‚screenshots (has imgs)
β”œβ”€β”€ πŸ“‚datasets
β”‚ └── πŸ“„(Excel files or raw datasets)
β”œβ”€β”€ πŸ“‚db
β”‚ └── πŸ“„(MySQL config or helpers)
β”œβ”€β”€ πŸ“‚env (excluded by .gitignore)
β”œβ”€β”€ πŸ“‚llm
β”‚ └── πŸ“„agent.py (Gemini + SQL + chart logic)
β”œβ”€β”€ πŸ“„.env (holds GEMINI_API_KEY)
β”œβ”€β”€ πŸ“„.gitignore (ignores env(virtual environment)/, pycache/, .env, api_check.py, cmds.txt.)
β”œβ”€β”€ πŸ“„main.py (FastAPI entry point with /ask endpoint)
β”œβ”€β”€ πŸ“„requirements.txt (all required pip packages)
β”œβ”€β”€ πŸ“„TASK DESCRIPTION.md (provided use-case or brief)
└── πŸ“„README.md _(project documentation you're reading)_


1. Create a Virtual Environment

python -m venv env

source env/Scripts/activate   

2. Install Dependencies

pip install -r requirements.txt

3. Add Your Gemini API Key

Create a .env file:

GEMINI_API_KEY=your_gemini_api_key_here

Get your key from: https://makersuite.google.com/app/apikey

4. Configure MySQL DB

Ensure MySQL is running, and update the DB connection in db/session.py if needed:

DATABASE_URL = f"mysql+mysqlconnector://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"

Make sure your database and tables are already created and populated.

5. Run the API Server

uvicorn main:app --reload

Access Swagger UI at: http://127.0.0.1:8000/docs

πŸ’¬ Example Questions

πŸ“Š Data Queries "What is the total ad spend?"

"How many clicks did item 101 get?"

"Show me all metrics for item 21"

πŸ“ˆ Business Metrics "What is the Return on Ad Spend (RoAS)?"

"Which item had the highest CPC?"

πŸ“Š Chart Requests "Generate a pie chart of sales by item"

"Show a bar chart of revenue per product"

πŸ› οΈ Data Updates "Delete entry where item_id is 99"


πŸ“¦ Requirements

Python 3.8+

FastAPI

SQLAlchemy

pandas

matplotlib

python-dotenv

sse-starlette

MySQL (local or hosted)

Google Generative AI client (google-generativeai)


🧠 How It Works

User Input β€” Ask a question at the /ask API endpoint.

Gemini Processing β€” Generates a SQL query or chart instruction.

SQL Execution β€” FastAPI runs the SQL on the connected MySQL database.

Chart (Optional) β€” If requested, Matplotlib opens a popup with a chart.


πŸ“Œ Notes

.env is excluded by .gitignore for security

Matplotlib is used to pop up chart windows (no frontend)

Error-handling and Gemini fallback are supported


πŸ“Š Chart Output

  • Chart images are saved automatically in assets/screenshots/ whenever a chart is requested.
  • The API response includes the chart image path for easy access and integration.
  • Example response:
    {
      "question": "Show me a bar chart of total sales by product",
      "sql_generated": "SELECT product, SUM(total_sales) FROM total_sales_metrics GROUP BY product",
      "query_result": [...],
      "answer": "Query executed successfully with chart",
      "chart_path": "assets/screenshots/chart.png",
      "type": "select"
    }

πŸ“· Screenshots

DB & Tables in MySQL

Screenshot 2025-07-22 195118

Swagger UI(/docs)

SQL Output Screenshot Chart Screenshot

Chart Output Example (Saved Image)

image

Terminal Output (Generated by Gemini API)

Postman Screenshot

MySQL Output

image

πŸ™Œ Credits

FastAPI

Google Gemini

SQLAlchemy

Matplotlib

Pandas


πŸ™‹β€β™‚οΈ Author

Nitish B
Final Year Student | Software Developer & AI Enthusiast
πŸ“« GitHub β€’ LinkedIn


⭐️ Star the Repository

If you found this project helpful, consider starring ⭐ it on GitHub to support future development!

About

An AI agent that answers natural language questions about e-commerce data by converting queries to SQL, fetching results from a sales database, and delivering real-time responses via API.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages