Thanks to visit codestin.com
Credit goes to github.com

Skip to content

peemblis/sql-bot

Repository files navigation

SQL Bot - Text to SQL Converter

A web application that converts natural language queries to SQL statements using AI (OpenAI GPT-4 or Anthropic Claude).

Features

  • 🤖 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

Quick Start

1. Install uv (if not already installed)

# 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 uv

2. Install Dependencies

uv sync

3. Set up Environment Variables

Copy .env.example to .env and add your API keys:

cp .env.example .env

Edit .env:

OPENAI_API_KEY=your_openai_api_key_here
ANTHROPIC_API_KEY=your_anthropic_api_key_here
DATABASE_URL=sqlite:///./sql_bot.db

4. Start the Application

Option A: Start both services with one command (Recommended)

uv run python run.py

Option B: Start services manually

Terminal 1 (Backend):

uv run python start_backend.py

Terminal 2 (Frontend):

uv run python start_frontend.py

5. Access the Application

Usage

  1. Enter a natural language query in the frontend
  2. Select AI provider (OpenAI or Anthropic)
  3. Click "Convert to SQL" to generate SQL
  4. Review and edit the generated SQL if needed
  5. Execute the SQL to see results
  6. Download results as CSV if needed

Sample Queries

  • "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"

Database Schema

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)

API Endpoints

  • POST /text-to-sql: Convert natural language to SQL
  • POST /execute-sql: Execute SQL queries
  • GET /schema: Get database schema
  • GET /docs: API documentation

Architecture

Frontend (Streamlit) → Backend (FastAPI) → AI Service (OpenAI/Anthropic)
                                      ↓
                               Database (SQLite)

Development

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published