Skip to main content

GroundedQL — Intent-driven, deterministic natural language to SQL for Postgres.

Project description

GroundedQL

GroundedQL - Reliable natural-language analytics through deterministic SQL compilation

Intent-driven, deterministic natural language to SQL for Postgres.
Instead of letting an LLM generate free-form SQL, the LLM extracts a lightweight QueryIntent, and GroundedQL deterministically compiles it into parameterized SQL and executes it safely.

Install

pip install groundedql

To use the OpenAI SDK adapter shown below:

pip install "groundedql[openai]"

With optional few-shot memory (recommended for production):

pip install "groundedql[memory]"
Install from source
git clone https://github.com/Certifore/groundedql
cd groundedql
pip install -e ".[dev]"

Quick Start

1. Generate your schema from the database

groundedql init --db "postgresql://user:pass@host/db"
# → config/schema.yaml  (tables, columns, types, PKs, links — all auto-detected)

2. Enrich with LLM-generated descriptions (optional, recommended)

export LLM_API_KEY=sk-...   # works with any OpenAI-compatible provider
groundedql describe --schema config/schema.yaml --db "postgresql://user:pass@host/db"
# → Adds table + column descriptions using sample data for context

3. Ask questions

from sqlalchemy import create_engine
from openai import OpenAI
from groundedql.agent import QueryAgent

engine = create_engine("postgresql+psycopg2://user:pass@host/db")

agent = QueryAgent(
    engine=engine,
    schema_path="config/schema.yaml",
    llm=OpenAI(api_key="sk-..."),
)

result = agent.ask("how many plumbing issues last year?")
print(result["rows"])
print(result["sql"])

Use Mistral instead of OpenAI

export MISTRAL_AI=...
# optional
export MISTRAL_MODEL=mistral-small-latest
agent = QueryAgent(
    engine=engine,
    schema_path="config/schema.yaml",
    llm="mistral",  # or "mistral:mistral-small-latest"
)

Use a local Ollama model

export OLLAMA_MODEL=groundedql-gemma4
# optional
export OLLAMA_BASE_URL=http://127.0.0.1:11434
export OLLAMA_NUM_CTX=8192
agent = QueryAgent(
    engine=engine,
    schema_path="config/schema.yaml",
    llm="ollama",  # or "ollama:groundedql-gemma4"
)

CLI Reference

Command Description
groundedql init --db URL Introspect Postgres and generate schema.yaml
groundedql describe --schema PATH --db URL Enrich schema with LLM-generated descriptions

Run groundedql --help for full options.

Documentation

Full documentation, benchmarks, and guides are at certifore.github.io/groundedql_docs (source).

See ROADMAP.md for planned capabilities (lookup, trends, ratios, multi-step NL, and expressiveness goals).

Existing users of the previous package name should read MIGRATION.md.

Contributing

Contributions are welcome through pull requests. Please read CONTRIBUTING.md before submitting changes. The lead maintainer reviews and merges all changes into the official repository and is the only person who publishes official releases.

License

GroundedQL is licensed under the Apache License 2.0.

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

groundedql-0.3.0.tar.gz (147.5 kB view details)

Uploaded Source

Built Distribution

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

groundedql-0.3.0-py3-none-any.whl (154.5 kB view details)

Uploaded Python 3

File details

Details for the file groundedql-0.3.0.tar.gz.

File metadata

  • Download URL: groundedql-0.3.0.tar.gz
  • Upload date:
  • Size: 147.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for groundedql-0.3.0.tar.gz
Algorithm Hash digest
SHA256 31a18a25e40caed28db2b4cd40b790176b45796f820d248c4aac1f6417fea16f
MD5 57fddc3d2cea5eed6117bba020f7e615
BLAKE2b-256 846c83be03d82a2a36e8acc01cb84a98329327de64147f60fa6443ed384b990f

See more details on using hashes here.

File details

Details for the file groundedql-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: groundedql-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 154.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for groundedql-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd6f92cd90e393934a443e4e00c1ab84cce13de8e54d9e9b690b71c786cabfcd
MD5 d96213eed423a7a1393c906120701f70
BLAKE2b-256 3648914a5531becc49593c686efc1c459a788343f5850a330de6911aeb3dc613

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