Skip to main content

AI-powered SQL assistant for Postgres and Snowflake

Project description

sqlcopilot

AI-powered SQL assistant for Postgres and Snowflake. Convert natural language to SQL, explain queries, and optimize them — all from your terminal.


Features

Command Description
convert Natural language → SQL
explain Plain-English explanation of any SQL query
optimize Performance-optimized rewrite with change summary
configure Persist defaults (dialect, schema, API key)

Project Structure

sqlcopilot/
├── sqlcopilot/
│   ├── __init__.py        # version
│   ├── cli.py             # Typer CLI commands
│   ├── ai.py              # OpenAI prompt logic
│   ├── config.py          # Config load/save (~/.sqlcopilot/config.yaml)
│   └── schema_loader.py   # Parse schema YAML → SQL DDL for context
├── tests/
│   ├── test_ai.py
│   ├── test_cli.py
│   └── test_schema_loader.py
├── example_schema.yaml    # Sample schema to pass as --schema
├── Dockerfile
├── pyproject.toml
├── setup.py
└── README.md

Installation

From PyPI (once published)

pip install sqlcopilot

From source (development)

git clone https://github.com/your-org/sqlcopilot.git
cd sqlcopilot
pip install -e ".[dev]"

Configuration

Option 1 — Environment variable (quickest)

export OPENAI_API_KEY="sk-..."

Option 2 — Config file

sqlcopilot configure --api-key sk-... --dialect postgres

This writes ~/.sqlcopilot/config.yaml:

openai:
  api_key: sk-...
  model: gpt-4o
database:
  dialect: postgres
  schema_file: null

Usage

Convert natural language to SQL

sqlcopilot convert "show me the top 10 customers by total order value"

With a schema file (gives the AI your table definitions):

sqlcopilot convert "orders placed this month" --schema example_schema.yaml

Targeting Snowflake:

sqlcopilot convert "daily revenue for last 30 days" --dialect snowflake

Explain a query

sqlcopilot explain "SELECT u.name, COUNT(o.id) FROM users u LEFT JOIN orders o ON u.id = o.user_id GROUP BY u.name"

Pipe from a file:

sqlcopilot explain "$(cat my_query.sql)"

Optimize a query

sqlcopilot optimize "SELECT * FROM orders WHERE status = 'shipped'"

Override model per call

sqlcopilot convert "..." --model gpt-4-turbo

Schema File Format

Define your tables in YAML; sqlcopilot converts them to CREATE TABLE DDL before sending to the AI:

# example_schema.yaml
tables:
  users:
    columns:
      id: BIGSERIAL PRIMARY KEY
      email: VARCHAR(255) NOT NULL UNIQUE
      created_at: TIMESTAMPTZ NOT NULL DEFAULT NOW()

  orders:
    columns:
      id: BIGSERIAL PRIMARY KEY
      user_id: BIGINT NOT NULL REFERENCES users(id)
      total_amount: NUMERIC(12, 2) NOT NULL
      status: VARCHAR(50) NOT NULL

Docker

Build

docker build -t sqlcopilot .

Run

# Convert
docker run --rm \
  -e OPENAI_API_KEY="$OPENAI_API_KEY" \
  sqlcopilot convert "list all active users"

# With a local schema file mounted
docker run --rm \
  -e OPENAI_API_KEY="$OPENAI_API_KEY" \
  -v "$(pwd)/example_schema.yaml:/schemas/schema.yaml" \
  sqlcopilot convert "top products by revenue" --schema /schemas/schema.yaml --dialect snowflake

# Persist config across runs
docker run --rm \
  -v "$HOME/.sqlcopilot:/root/.sqlcopilot" \
  sqlcopilot configure --api-key sk-... --dialect postgres

Development

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Lint
ruff check .

Supported Dialects

Dialect Notes
postgres CTEs preferred, ILIKE for case-insensitive search
snowflake QUALIFY for window filtering, FLATTEN for semi-structured data

License

MIT

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

sqlcopilot-0.1.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

sqlcopilot-0.1.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file sqlcopilot-0.1.0.tar.gz.

File metadata

  • Download URL: sqlcopilot-0.1.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for sqlcopilot-0.1.0.tar.gz
Algorithm Hash digest
SHA256 97a67d629f299a267df26ffe9bab4a3fead827427dbabdab662a61efc96ad46a
MD5 9954a9f8f8dfb30557c01fe8db1dde87
BLAKE2b-256 8b39b3a506947c9b728d17f0dba4886d5f32673f4ba7c510cfae7d4185255b39

See more details on using hashes here.

File details

Details for the file sqlcopilot-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sqlcopilot-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for sqlcopilot-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 85ffa7f9e1311c59a110c2ab708214f91d3e70902e004055641a10ee98c05016
MD5 c3e0c23a49913899f806aa17b0264e75
BLAKE2b-256 49aeeea0222279ca034b3bb6cccde67679885b7176920d6a0e222b88da24e44e

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