AgenticSqlAgent
Showing how easy Agentic AI is made.
Install / Use
/learn @danielbeach/AgenticSqlAgentREADME
SQL Agent - Agentic AI Example
A comprehensive example application demonstrating Agentic AI concepts using LangChain, React, FastAPI, and SQLite. This project teaches how to build an intelligent SQL agent that can answer natural language questions about a sales database.
Full blog post here -> https://open.substack.com/pub/dataengineeringcentral/p/building-agentic-ai-fancy
Features
- 🤖 LangChain SQL Agent: Uses LangChain to create an intelligent agent that translates natural language to SQL queries
- 🎨 Modern React Frontend: Beautiful, responsive UI built with React and Vite
- 🚀 FastAPI Backend: High-performance Python API with async support
- 💾 Pre-filled SQLite Database: Sample sales data with sales people and daily sales records
- ⚙️ Fully Configurable: All components (database, LLM endpoint, models) are configurable via environment variables
- 🐳 Dockerized: Complete Docker setup with docker-compose for easy deployment
Architecture
┌─────────────┐
│ React │ Frontend (Port 3000)
│ Frontend │
└──────┬──────┘
│ HTTP
▼
┌─────────────┐
│ FastAPI │ Backend (Port 8000)
│ Backend │
└──────┬──────┘
│
├──► LangChain SQL Agent
│
└──► SQLite Database (sales.db)
Prerequisites
- Docker and Docker Compose
- OpenAI API key (or compatible endpoint)
- Python 3.12+ (for local development)
- Node.js 20+ (for local development)
Quick Start with Docker
-
Clone the repository (if applicable) or navigate to the project directory
-
Set up environment variables
Create a
.envfile in the root directory:OPENAI_API_KEY=your-api-key-here OPENAI_BASE_URL=https://api.openai.com/v1 # Optional, for custom endpoints OPENAI_MODEL=gpt-4o-mini # Optional, defaults to gpt-4o-mini -
Start the services
docker-compose up --build -
Access the application
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
- API Docs: http://localhost:8000/docs
Local Development
Backend Setup
-
Install dependencies
pip install uv uv pip install -e . -
Set environment variables
Create a
.envfile in thebackend/directory:DATABASE_URL=sqlite:///./sales.db OPENAI_API_KEY=your-api-key-here OPENAI_MODEL=gpt-4o-mini -
Run the backend
cd backend uvicorn main:app --reload
Frontend Setup
-
Install dependencies
cd frontend npm install -
Run the frontend
npm run dev
Configuration
All components are configurable via environment variables:
Database Configuration
DATABASE_URL: SQLite database URL (default:sqlite:///./sales.db)- Example:
sqlite:///./data/sales.db
- Example:
LLM Configuration
LLM_PROVIDER: LLM provider (default:openai)OPENAI_API_KEY: Your OpenAI API key (required)OPENAI_BASE_URL: Custom OpenAI-compatible endpoint (optional)- Example:
https://api.openai.com/v1orhttp://localhost:1234/v1
- Example:
OPENAI_MODEL: Model name (default:gpt-4o-mini)- Examples:
gpt-4o-mini,gpt-4,gpt-3.5-turbo
- Examples:
OPENAI_TEMPERATURE: Model temperature (default:0.0)
API Configuration
API_HOST: API host (default:0.0.0.0)API_PORT: API port (default:8000)CORS_ORIGINS: Allowed CORS origins (JSON array)
Database Schema
The application includes a pre-filled SQLite database with the following schema:
sales_people Table
| Column | Type | Description | |-------------|---------|--------------------------------| | id | INTEGER | Primary key | | name | TEXT | Sales person name | | email | TEXT | Email address (unique) | | region | TEXT | Sales region (North/South/East/West) | | hire_date | DATE | Date hired | | quota | REAL | Sales quota |
sales Table
| Column | Type | Description | |------------------|---------|--------------------------------| | id | INTEGER | Primary key | | sales_person_id | INTEGER | Foreign key to sales_people | | sale_date | DATE | Date of sale | | amount | REAL | Sale amount | | product_category | TEXT | Product category | | customer_name | TEXT | Customer name |
Example Queries
Try asking the agent questions like:
- "What are the total sales for each sales person?"
- "Who is the top performing sales person this month?"
- "Show me sales by region"
- "What is the average sale amount?"
- "Which product category has the most sales?"
- "Show me sales trends over the last 30 days"
- "How many sales did Alice Johnson make in the last week?"
API Endpoints
GET /
Health check endpoint.
GET /health
Detailed health check including agent initialization status.
GET /config
Get current configuration (database URL, LLM settings, etc.).
POST /query
Execute a natural language query.
Request:
{
"query": "What are the total sales for each sales person?"
}
Response:
{
"success": true,
"result": "The total sales for each sales person are:\n- Alice Johnson: $125,430.50\n- Bob Smith: $142,890.25\n...",
"intermediate_steps": [...]
}
Project Structure
SQLAgent/
├── backend/
│ ├── main.py # FastAPI application
│ ├── agent.py # LangChain SQL agent setup
│ ├── database.py # Database initialization
│ ├── config.py # Configuration management
│ └── Dockerfile # Backend Docker image
├── frontend/
│ ├── src/
│ │ ├── App.jsx # Main React component
│ │ ├── App.css # Styles
│ │ └── main.jsx # React entry point
│ ├── package.json # Frontend dependencies
│ └── Dockerfile # Frontend Docker image
├── data/ # Database storage (created at runtime)
├── docker-compose.yml # Docker orchestration
├── pyproject.toml # Python dependencies
└── README.md # This file
Teaching Points
This project demonstrates several key Agentic AI concepts:
- Natural Language to SQL Translation: The agent understands natural language and converts it to SQL queries
- Tool Use: The agent uses SQL tools to interact with the database
- Memory: The agent maintains conversation context across queries
- Error Handling: Graceful handling of parsing errors and invalid queries
- Modular Architecture: Separated concerns (frontend, backend, agent, database)
Troubleshooting
Agent not initializing
- Check that
OPENAI_API_KEYis set correctly - Verify the API endpoint is accessible
- Check backend logs for detailed error messages
Database not found
- The database is automatically created on first startup
- Ensure the
data/directory is writable - Check
DATABASE_URLconfiguration
CORS errors
- Verify
CORS_ORIGINSincludes your frontend URL - Check that both services are running
License
This project is provided as an educational example.
Contributing
This is an educational project. Feel free to fork and modify for your teaching needs!
Related Skills
feishu-drive
347.0k|
things-mac
347.0kManage Things 3 via the `things` CLI on macOS (add/update projects+todos via URL scheme; read/search/list from the local Things database)
clawhub
347.0kUse the ClawHub CLI to search, install, update, and publish agent skills from clawhub.com
codebase-memory-mcp
1.2kHigh-performance code intelligence MCP server. Indexes codebases into a persistent knowledge graph — average repo in milliseconds. 66 languages, sub-ms queries, 99% fewer tokens. Single static binary, zero dependencies.
