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
# Language Configuration (Optional)
export LANGUAGE=en # en (English) or ko (Korean), defaults to en
๐ Multi-language Support
sq3m supports multiple system prompt languages through the LANGUAGE environment variable:
-
Supported Languages:
en: English (default)ko: Korean
-
Priority Order for System Prompts:
- Custom path parameter (directly specified)
SYSTEM_PROMPT_PATHenvironment variable (absolute path)SYSTEM_PROMPT_FILEenvironment variable (filename in config directory)- Language-specific default prompt (based on LANGUAGE environment variable)
- Default fallback prompt
Example Usage:
# Use Korean system prompts
export LANGUAGE=ko
sq3m
# Or set in .env file
echo "LANGUAGE=ko" >> .env
๐ง Usage
Run the CLI tool:
sq3m
The tool will guide you through:
- ๐ค LLM Setup: Configure OpenAI API key (if not in environment)
- ๐๏ธ Database Connection: Set up database connection (interactive if not in environment)
- ๐ Schema Analysis: Automatically analyze all tables and infer their purposes
- ๐ฌ Interactive Queries: Ask questions in natural language
๐ก Example Queries
- "Show all users"
- "Find orders from last month"
- "Count products by category"
- "Show user details for user ID 123"
- "What are the top 5 selling products?"
๐ฏ CLI Commands
tables- Show all database tables and their purposeshelporh- Show available commandsquit,exit, orq- Exit the application
๐๏ธ 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
- Make changes to the code
- Run tests:
uv run pytest - Run linting:
uv run ruff check --fix . - Run formatting:
uv run ruff format . - Run type checking:
uv run mypy sq3m/ - 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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sq3m-0.1.1.tar.gz.
File metadata
- Download URL: sq3m-0.1.1.tar.gz
- Upload date:
- Size: 140.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db4f011e985ce57728ff9d0eee945096b569d189905c3c08682ebb58ae04b44f
|
|
| MD5 |
a447f74efa78efa10d32f669e3bf11d8
|
|
| BLAKE2b-256 |
e189e042740408d5a80cd9e8e94b6afdef3029cdc65e6d7aaded58a6e0f9b979
|
File details
Details for the file sq3m-0.1.1-py3-none-any.whl.
File metadata
- Download URL: sq3m-0.1.1-py3-none-any.whl
- Upload date:
- Size: 55.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e88106ac42f95fcc6f74b548c406df2908ea7292f2297ebbe1bbf36bce320f95
|
|
| MD5 |
1d645c9f8fd488e6b87749fbaec88994
|
|
| BLAKE2b-256 |
b8a6a9fb9b0fc9559918cd0233a680b0f7745a871fed71574691f0b4c793c2e8
|