A web application that converts natural language queries to SQL statements using AI (OpenAI GPT-4 or Anthropic Claude).
- 🤖 Natural language to SQL conversion
- 🔧 FastAPI backend with REST API
- 🎨 Streamlit frontend interface
- 🗃️ SQLite database with sample data
- 📊 SQL execution and result visualization
- 🔄 Support for OpenAI and Anthropic AI providers
# On macOS and Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# On Windows
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Or with pip
pip install uvuv syncCopy .env.example to .env and add your API keys:
cp .env.example .envEdit .env:
OPENAI_API_KEY=your_openai_api_key_here
ANTHROPIC_API_KEY=your_anthropic_api_key_here
DATABASE_URL=sqlite:///./sql_bot.db
Option A: Start both services with one command (Recommended)
uv run python run.pyOption B: Start services manually
Terminal 1 (Backend):
uv run python start_backend.pyTerminal 2 (Frontend):
uv run python start_frontend.py- Frontend: http://localhost:8501
- Backend API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- Enter a natural language query in the frontend
- Select AI provider (OpenAI or Anthropic)
- Click "Convert to SQL" to generate SQL
- Review and edit the generated SQL if needed
- Execute the SQL to see results
- Download results as CSV if needed
- "Show all employees in the Engineering department"
- "What is the average salary by department?"
- "List all projects with their budgets"
- "Who are the highest paid employees?"
- "Show departments with their total employee count"
The application includes sample data with these tables:
- employees: Employee information (name, department, salary, hire_date)
- departments: Department details (name, budget, manager_id)
- projects: Project information (name, department_id, dates, budget)
POST /text-to-sql: Convert natural language to SQLPOST /execute-sql: Execute SQL queriesGET /schema: Get database schemaGET /docs: API documentation
Frontend (Streamlit) → Backend (FastAPI) → AI Service (OpenAI/Anthropic)
↓
Database (SQLite)
The application is structured as:
sql-bot/
├── backend/
│ ├── main.py # FastAPI application
│ └── services/
│ ├── ai_service.py # AI integration
│ └── database_service.py # Database operations
├── frontend/
│ └── app.py # Streamlit interface
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
└── README.md # This file