Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

πŸ€– Agentic AI - Data Agent

A sophisticated multi-agent system for intelligent data processing and analysis using LangGraph. This project demonstrates a complete implementation of an agentic architecture with specialized sub-agents for SQL operations and ETL workflows.

πŸ“‹ Table of Contents


🎯 Overview

Agentic AI Data Agent is an intelligent system that processes natural language queries and routes them to specialized agents for execution. The main agent acts as an intelligent router that understands user intent and delegates tasks to either the SQL Analyst Agent (for database queries) or the ETL Analyst Agent (for data extraction and transformation operations).

This project showcases modern AI engineering practices including:

  • Multi-agent orchestration with LangGraph
  • Intelligent routing based on natural language understanding
  • Safety validation for SQL queries
  • Tool-based agent architecture
  • Dynamic LLM selection based on task complexity

πŸ—οΈ Architecture

The system follows a hierarchical agent architecture:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Data Agent (Router)                      β”‚
β”‚         Routes user queries to appropriate sub-agents       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚                       β”‚
         β–Ό                       β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ SQL Analyst  β”‚        β”‚ ETL Analyst  β”‚
    β”‚   Agent      β”‚        β”‚   Agent      β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚
         β”œβ”€β–Ί Query Curation      β”œβ”€β–Ί Extract Load
         β”œβ”€β–Ί Schema Context      β”œβ”€β–Ί Transform Load
         β”œβ”€β–Ί SQL Generation      └─► Code Execution
         β”œβ”€β–Ί Safety Validation   
         β”œβ”€β–Ί Query Execution     
         └─► Answer Generation   

State Flow

  1. User Input β†’ Natural language query
  2. Router Node β†’ Classifies query as SQL or ETL
  3. Agent Dispatch β†’ Routes to appropriate sub-agent
  4. Processing β†’ Each agent processes the task
  5. Output β†’ Returns structured result to user

✨ Features

Core Capabilities

  • Intelligent Query Routing: Automatically classifies user queries as SQL or ETL operations

  • SQL Analysis Agent:

    • Natural language to SQL query conversion
    • Automatic schema context gathering
    • SQL safety validation (prevents harmful operations)
    • Query execution on PostgreSQL database
    • Intelligent query refinement
  • ETL Agent:

    • API data extraction (JSON to structured formats)
    • Data transformation using Pandas
    • Multi-format support (CSV, JSON, Parquet)
    • Dynamic code generation based on user requirements
    • Safe code execution
  • Multi-LLM Support:

    • Low-complexity queries: Faster, cost-effective LLM
    • Medium-complexity queries: Balanced LLM
    • High-complexity queries: Premium LLM (Claude)
  • Safety & Validation:

    • SQL query safety checking
    • Protection against database modifications (INSERT, UPDATE, DELETE, DROP, etc.)
    • Input validation and sanitization
    • Structured output validation using Pydantic

πŸ“ Project Structure

Data_Agent/
β”œβ”€β”€ agents/                          # Agent implementations
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ data_agent.py               # Main router agent
β”‚   β”œβ”€β”€ sql_analyst.py              # SQL query agent
β”‚   └── etl_analyst.py              # ETL operations agent
β”‚
β”œβ”€β”€ Models/                          # Data models
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── schema.py                   # Pydantic schemas for state management
β”‚
β”œβ”€β”€ utils/                           # Utility modules
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ database.py                 # PostgreSQL utilities
β”‚   β”œβ”€β”€ etl_tools.py                # ETL operations toolkit
β”‚   β”œβ”€β”€ llm_pick.py                 # LLM selection logic
β”‚
β”œβ”€β”€ data/                            # Data directory
β”‚   β”œβ”€β”€ extract/                     # Extracted data storage
β”‚   β”œβ”€β”€ transform/                   # Transformed data storage
β”‚   β”œβ”€β”€ payments.csv                 # Sample dataset
β”‚   β”œβ”€β”€ ratings.csv                  # Sample dataset
β”‚   β”œβ”€β”€ rides.csv                    # Sample dataset
β”‚   β”œβ”€β”€ users.csv                    # Sample dataset
β”‚   └── vehicles.csv                 # Sample dataset
β”‚
β”œβ”€β”€ main.py                          # Entry point
β”œβ”€β”€ app.py                           # streamlit code
β”œβ”€β”€ feed_db.py                       # Database initialization script
β”œβ”€β”€ pyproject.toml                   # Project metadata and dependencies
└── README.md                         # This file

βš™οΈ Configuration

LLM Selection (utils/llm_pick.py)

The pick_llm() function intelligently selects the appropriate LLM based on complexity:

from utils.llm_pick import pick_llm

# Select based on complexity
llm_fast = pick_llm("low")        # Cost-effective for simple queries
llm_balanced = pick_llm("medium") # Balanced performance and cost
llm_powerful = pick_llm("claude") # Premium model for complex tasks

πŸ’» Usage

Running the Data Agent

Basic Usage:

from agents.data_agent import data_agent
from langchain_core.messages import HumanMessage

# Example: Extract data from API
response = data_agent.invoke({
    "messages": [
        HumanMessage(content="""
            I want to extract the data from the API endpoint 
            'https://pokeapi.co/api/v2/pokemon' and save it to 
            data/extract folder in CSV format
        """)
    ],
    "route_response": ""
})

print(response)

SQL Query Example:

response = data_agent.invoke({
    "messages": [
        HumanMessage(content="""
            Show me the top 5 users with the highest ratings
        """)
    ],
    "route_response": ""
})

ETL Example:

response = data_agent.invoke({
    "messages": [
        HumanMessage(content="""
            Transform the rides.csv data by filtering only rides 
            with rating > 4.5 and save to data/transform
        """)
    ],
    "route_response": ""
})

Running from Command Line

# Run the main data agent
python main.py

# Run individual agents
python agents/sql_analyst.py
python agents/etl_analyst.py

πŸ€– Agent Descriptions

1. Data Agent (Main Router)

File: agents/data_agent.py

Responsibility:

  • Receives natural language user queries
  • Classifies queries as either SQL or ETL operations
  • Routes queries to appropriate sub-agents
  • Aggregates results and returns to user

Components:

  • Router Node: Uses structured output to classify query intent
  • Conditional Routing: Routes to SQL or ETL based on classification
  • Graph Orchestration: Manages workflow using LangGraph

Workflow image


2. SQL Analyst Agent

File: agents/sql_analyst.py

Responsibility:

  • Converts natural language queries to SQL
  • Handles all database query operations
  • Validates query safety
  • Executes queries and returns results

Workflow:

  1. Query Curation - Refines user question for clarity
  2. Context Gathering - Fetches database schema details
  3. Prompt Construction - Creates detailed context for LLM
  4. SQL Generation - Generates SQL query using LLM
  5. Safety Check - Validates query safety
  6. Query Execution - Executes validated query on database
  7. Answer Generation - Formats and returns results
image

Safety Features:

  • Prevents execution of dangerous commands (INSERT, UPDATE, DELETE, DROP, ALTER)
  • Validates query before execution
  • Automatic result limiting to 10 rows (unless specified)
  • Schema validation against database

3. ETL Analyst Agent

File: agents/etl_analyst.py

Responsibility:

  • Handles data extraction from APIs
  • Performs data transformation using Pandas
  • Manages data loading to various formats
  • Executes code safely in controlled environment

Workflow:

  1. Tool Binding - Attaches ETL tools to LLM
  2. User Intent Understanding - Analyzes transformation requirements
  3. Tool Selection - Chooses appropriate ETL operation
  4. Code Generation - Generates Pandas code for transformation
  5. Safe Execution - Executes generated code in sandboxed environment
  6. Result Reporting - Returns execution status and generated code
image

Supported Tools:

  • extract_load_tool: Extract from API β†’ Load to storage
  • transform_load_tool: Transform data using Pandas β†’ Load result

Supported Formats:

  • CSV (default)
  • JSON (Lines or Records)
  • Parquet

πŸ“Š Data Models

AgentSchema (SQL Agent State)

class AgentSchema(BaseModel):
    messages: List                    # Conversation messages
    user_question: str                # Original user query
    curated_ques: str                 # Refined question
    prompt_query_context: str         # Database context + prompt
    generated_sql_query: str          # Generated SQL
    is_safe: Literal["Yes", "No"]     # Safety validation result
    comments: str                     # Safety check comments
    sql_query_execution_result: str   # Query result
    final_answer: str                 # Final formatted answer

ETLAgentSchema (ETL Agent State)

class ETLAgentSchema(BaseModel):
    messages: List                    # Conversation messages

RouterSchema (Query Classification)

class RouterSchema(BaseModel):
    answer: Literal["sql", "etl"]     # Query classification
    comments: str                     # Reasoning for classification

DataAgentSchema (Main Agent State)

class DataAgentSchema(BaseModel):
    messages: List                    # All conversation messages
    route_response: str               # Router decision (sql/etl)

πŸ“š Examples

Example 1: Database Query

User Query:

"Show me the average rating for each vehicle type"

Processing:

  1. Router classifies as SQL query
  2. SQL Agent fetches schema
  3. Generates: SELECT vehicle_type, AVG(rating) FROM rides GROUP BY vehicle_type LIMIT 10
  4. Validates safety βœ“
  5. Executes and returns results

Example 2: Data Extraction

User Query:

"Extract the data from 'https://pokeapi.co/api/v2/pokemon' and save it as CSV"

Processing:

  1. Router classifies as ETL operation
  2. ETL Agent selects extract_load_tool
  3. Makes API request to endpoint
  4. Normalizes JSON response
  5. Saves to data/extract/extracted_data.csv

Example 3: Data Transformation

User Query:

"Transform rides.csv to filter only rides with rating > 4.0 and save as JSON"

Processing:

  1. Router classifies as ETL operation
  2. ETL Agent analyzes requirement
  3. Generates Pandas code to filter and transform
  4. Executes code safely
  5. Saves result to data/transform/ in JSON format

πŸ“š Streamlit Screenshots

Overview Tab

image

Workflow Tab image

Ask Tab image

Question 1

"Delete data from users table"
image

Workflow changes based on the Questions image

Question 2

"Show the first 10 payment records."
image

Question 3:

"Transform the data and show squirtle pokemon only"
image

Workflow changes based on the Questions

image

πŸ” Security Features

βœ… SQL Safety Validation

  • Query inspection before execution
  • Blocks destructive operations
  • Database structure protection

βœ… Safe Code Execution

  • Sandboxed Python code execution
  • Input validation
  • Error handling and reporting

βœ… Environment Security

  • Credentials stored in .env (not in code)
  • Sensitive data protection
  • Proper exception handling

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages