Skip to main content

Interactive CLI DB AI Agent for natural language to SQL conversion

Project description

SQ3M - AI-Powered Database Query Assistant

A Python CLI tool that converts natural language queries into SQL using Large Language Models (LLM). Built with Clean Architecture principles.

๐Ÿš€ Features

  • ๐Ÿค– Natural language to SQL conversion using OpenAI completion models
  • ๐Ÿ—„๏ธ Multi-database support for MySQL and PostgreSQL
  • ๐Ÿง  Automatic table purpose inference using LLM
  • ๐ŸŽจ Beautiful CLI interface with Rich
  • โš™๏ธ Environment variable configuration
  • ๐Ÿ—๏ธ Clean Architecture design

๐Ÿ“ฆ Installation

Using pip

pip install sq3m

Using uv (recommended for development)

# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh

# Clone the repository and setup
git clone https://github.com/leegyurak/sq3m.git
cd sq3m
uv sync

โš™๏ธ Configuration

Set up your environment variables in a .env file or export them:

# OpenAI Configuration
export OPENAI_API_KEY=your_openai_api_key
export OPENAI_MODEL=gpt-3.5-turbo  # Optional, defaults to gpt-3.5-turbo

# Database Configuration (Optional - can be set interactively)
export DB_TYPE=mysql  # mysql or postgresql
export DB_HOST=localhost
export DB_PORT=3306
export DB_NAME=your_database
export DB_USERNAME=your_username
export DB_PASSWORD=your_password

๐Ÿ”ง How to Use

Quick Start

  1. Install sq3m:

    pip install sq3m
    
  2. Set up your OpenAI API key:

    export OPENAI_API_KEY=your_openai_api_key
    
  3. Run the tool:

    sq3m
    

Step-by-Step Usage

When you run sq3m, the tool will guide you through an interactive setup:

1. ๐Ÿค– LLM Configuration

  • If OPENAI_API_KEY is not set, you'll be prompted to enter it
  • Optionally configure the model (defaults to gpt-3.5-turbo)

2. ๐Ÿ—„๏ธ Database Connection

The tool will ask for your database details:

  • Database Type: Choose between MySQL, PostgreSQL, or SQLite
  • Host: Database server address (e.g., localhost)
  • Port: Database port (e.g., 3306 for MySQL, 5432 for PostgreSQL)
  • Database Name: Your database name
  • Username & Password: Your database credentials

Pro Tip: Set these as environment variables to skip the interactive setup:

export DB_TYPE=mysql
export DB_HOST=localhost
export DB_PORT=3306
export DB_NAME=your_database
export DB_USERNAME=your_username
export DB_PASSWORD=your_password

3. ๐Ÿ“Š Schema Analysis

  • sq3m automatically analyzes all tables in your database
  • Uses AI to infer the purpose of each table
  • Creates a comprehensive understanding of your database structure

4. ๐Ÿ’ฌ Interactive Query Mode

Now you can ask questions in natural language!

๐Ÿ’ก Example Conversations

๐Ÿค– sq3m > Show me all users
Generated SQL:
SELECT * FROM users;

Results:
โ”Œโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ id โ”‚   name   โ”‚       email         โ”‚    created_at      โ”‚
โ”œโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1  โ”‚ John Doe โ”‚ john@example.com    โ”‚ 2024-01-15         โ”‚
โ”‚ 2  โ”‚ Jane Doe โ”‚ jane@example.com    โ”‚ 2024-01-16         โ”‚
โ””โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿค– sq3m > How many orders were placed this month?
Generated SQL:
SELECT COUNT(*) as order_count
FROM orders
WHERE MONTH(created_at) = MONTH(CURRENT_DATE())
  AND YEAR(created_at) = YEAR(CURRENT_DATE());

Results:
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ order_count โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚     47      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿค– sq3m > What are the top 3 selling products?
Generated SQL:
SELECT p.name, SUM(oi.quantity) as total_sold
FROM products p
JOIN order_items oi ON p.id = oi.product_id
GROUP BY p.id, p.name
ORDER BY total_sold DESC
LIMIT 3;

Results: [showing results...]

๐ŸŽฏ Available Commands

While in the interactive mode, you can use these special commands:

Command Description
tables Show all database tables and their AI-inferred purposes
help or h Display available commands
quit, exit, or q Exit the application

๐Ÿ”ง Advanced Configuration

Create a .env file in your working directory:

# .env file
OPENAI_API_KEY=your_openai_api_key
OPENAI_MODEL=gpt-4  # Use GPT-4 for better results

DB_TYPE=postgresql
DB_HOST=localhost
DB_PORT=5432
DB_NAME=myapp_production
DB_USERNAME=myuser
DB_PASSWORD=mypassword

๐Ÿ’ก Tips for Better Results

  1. Be Specific: "Show users created this week" vs "Show users"
  2. Use Table Names: If you know them, mention specific table names
  3. Ask Follow-ups: "Can you also show their email addresses?"
  4. Use Business Terms: "Show revenue by month" instead of "sum sales"

๐Ÿ—๏ธ Architecture

The project follows Clean Architecture principles:

sq3m/
โ”œโ”€โ”€ domain/           # Business logic and entities
โ”‚   โ”œโ”€โ”€ entities/     # Core business objects
โ”‚   โ””โ”€โ”€ interfaces/   # Abstract interfaces
โ”œโ”€โ”€ application/      # Use cases and business rules
โ”‚   โ”œโ”€โ”€ services/     # Application services
โ”‚   โ””โ”€โ”€ use_cases/    # Specific business use cases
โ”œโ”€โ”€ infrastructure/   # External interfaces
โ”‚   โ”œโ”€โ”€ database/     # Database implementations
โ”‚   โ”œโ”€โ”€ llm/          # LLM service implementations
โ”‚   โ””โ”€โ”€ prompts/      # System prompts
โ”œโ”€โ”€ interface/        # User interface
โ”‚   โ””โ”€โ”€ cli/          # CLI implementation
โ””โ”€โ”€ config/           # Configuration management

๐Ÿ› ๏ธ Development

Prerequisites

  • Python 3.10+
  • uv package manager (recommended for fast dependency management)

UV Package Manager Setup

This project uses uv for fast Python package management.

Install uv:

# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh

# Or with pip
pip install uv

Setup Development Environment

# Clone the repository
git clone https://github.com/leegyurak/sq3m.git
cd sq3m

# Initialize Python environment and install dependencies
uv sync --all-extras --dev

# Install pre-commit hooks
uv run pre-commit install

Activate Virtual Environment (optional):

# Linux/macOS
source .venv/bin/activate

# Windows
.venv\Scripts\activate

Development Workflow

  1. Make changes to the code
  2. Run tests: uv run pytest
  3. Run linting: uv run ruff check --fix .
  4. Run formatting: uv run ruff format .
  5. Run type checking: uv run mypy sq3m/
  6. Commit changes (pre-commit hooks will run automatically)

Running Tests

# Run all tests
uv run pytest

# Run unit tests only
uv run pytest tests/unit

# Run integration tests
uv run pytest tests/integration

# Run with coverage
uv run pytest --cov=sq3m

# Run tests excluding slow ones
uv run pytest -m "not slow"

Code Quality

# Linting and formatting with ruff
uv run ruff check --fix .
uv run ruff format .

# Type checking
uv run mypy sq3m/

# Pre-commit hooks (run automatically on commit)
uv run pre-commit run --all-files

Running the Application

# Run directly with uv
uv run sq3m

# Or activate environment first
source .venv/bin/activate
sq3m

๐Ÿ“š Dependencies

Runtime Dependencies

  • click: CLI framework
  • rich: Beautiful terminal UI
  • openai: OpenAI API client
  • python-dotenv: Environment variable management
  • psycopg2-binary: PostgreSQL driver
  • pymysql: MySQL driver
  • sqlparse: SQL parsing utilities
  • pydantic: Data validation

Development Dependencies

  • pytest: Testing framework
  • pytest-cov: Coverage reporting
  • pytest-asyncio: Async testing support
  • ruff: Fast Python linter and formatter
  • pre-commit: Git hooks framework
  • mypy: Static type checker

๐Ÿ“‹ Requirements

  • Python: 3.10 or higher
  • uv: Package manager (recommended) or pip

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Thanks to OpenAI for providing the completion models
  • Built with modern Python tools: uv, ruff, pytest
  • Inspired by Clean Architecture principles

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sq3m-0.2.0.tar.gz (149.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sq3m-0.2.0-py3-none-any.whl (57.6 kB view details)

Uploaded Python 3

File details

Details for the file sq3m-0.2.0.tar.gz.

File metadata

  • Download URL: sq3m-0.2.0.tar.gz
  • Upload date:
  • Size: 149.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sq3m-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e5360731746a32bee87b3144ccaeaa9b9e3c42fecb9663a70957f7e96dd8c695
MD5 8ac99867910cdefb7bbce2d0c84f1a79
BLAKE2b-256 e245922522ad36748c2d37bcbf7dee96f2b382702a89d02f71149862ea5be2c1

See more details on using hashes here.

File details

Details for the file sq3m-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: sq3m-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 57.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sq3m-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed9059917b15a3a52cc7ad0c22739138dd7cb752006988002bb61375e3d91387
MD5 e84e3321dd3b6d23fe16573035c16b26
BLAKE2b-256 0f641203d354778ed276e52f49fe03bf2624b95f1560983976b4a7cf3ff71a3a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page