Expand description
sqlrite-ask — natural-language → SQL adapter for SQLRite.
Phase 7g.1 (foundational). One sync call:
use sqlrite::Connection;
use sqlrite_ask::{ask, AskConfig};
let conn = Connection::open("foo.sqlrite")?;
let config = AskConfig::from_env()?; // reads SQLRITE_LLM_API_KEY etc.
let response = ask(&conn, "How many users are over 30?", &config)?;
println!("Generated SQL: {}", response.sql);
println!("Why: {}", response.explanation);§What this crate is
- Reflects the schema of an open
Connectioninto CREATE TABLE text the LLM can ground on. - Wraps that schema in a stable system prompt with an
cache_control: ephemeralbreakpoint so the schema dump is served from Anthropic’s prompt cache after the first call. - Sends one HTTP POST to the LLM provider per
ask()call. - Parses the response into
AskResponse { sql, explanation }.
§What this crate is NOT
- Not an executor. The library deliberately does not run the
generated SQL — the caller decides whether to execute it. SDK
layers (
Python.Connection.ask_run,Node.db.askRun, etc.) add a one-shot generate-and-execute helper for the common case, but the default API is “generate, return, let me decide”. - Not multi-turn. Stateless — every call is a fresh prompt.
- Not multi-provider yet. Anthropic-first per Phase 7 plan
Q4. OpenAI + Ollama follow-ups slot into [
provider] without changing the public surface.
§Configuration
AskConfig resolves in this priority order:
- Explicit values you set on the struct (
AskConfig { api_key: Some(...), .. }) - Environment variables (
SQLRITE_LLM_*) - Built-in defaults (model =
claude-sonnet-4-6, max_tokens = 1024, cache TTL = 5 min)
Modules§
- schema
- Schema introspection — turn an open
Connectioninto a textual schema dump the LLM can ground its SQL generation on.
Structs§
- Anthropic
Provider - Anthropic Messages API client. Stateless — one struct, many
complete()calls. Theagent(ureq client) is reused across calls so connection-pool / TLS-session-cache benefits accrue when the sameAnthropicProvidermakes repeat calls. - AskConfig
- Knobs for an
ask()call. Construct directly, or viaAskConfig::from_envto pull defaults from the environment. - AskResponse
- Result returned from a successful
askcall. - Request
- One LLM call’s worth of input. Mirrors the Anthropic Messages
request shape because it’s the most expressive of the three
providers we’ll support; OpenAI and Ollama adapters convert to
their native shapes inside their own
completeimpls. - Response
- What every provider returns. We keep this minimal —
textis the raw string the model produced (the caller parses it),usagesurfaces token counts so callers can verify cache hits. - Usage
- Token-usage breakdown. Names match Anthropic’s API field names so
the mapping stays obvious; OpenAI’s
prompt_tokens/completion_tokenswill fan intoinput_tokens/output_tokenswhen that adapter lands.
Enums§
- AskError
- Errors
ask()can return. Includes every failure mode along the path: config / network / API / parsing. - Cache
Ttl - Cache-TTL knob exposed on
AskConfig. - Provider
Kind - Which LLM provider
asktalks to. Anthropic-only in 7g.1; the enum is here so adding OpenAI/Ollama later doesn’t break theAskConfigshape.
Constants§
- DEFAULT_
MAX_ TOKENS - Default
max_tokens. SQL generation rarely needs more than ~500 output tokens (single-statement queries + a one-sentence explanation). 1024 leaves headroom; under the SDK timeout cap so we don’t have to stream. - DEFAULT_
MODEL - Default model — Sonnet 4.6 hits the cost-quality sweet spot for
NL→SQL. Override via
AskConfig::modelor theSQLRITE_LLM_MODELenv var. Seedocs/phase-7-plan.mdfor the model-choice rationale.
Traits§
- Connection
AskExt - Extension trait that adds
ConnectionAskExt::asktosqlrite::Connection. Lives here (not on the engine) to keep the engine free of HTTP / TLS / serde deps. Bring it into scope withuse sqlrite_ask::ConnectionAskExt;. - Provider
- A single one-shot call. Sync because every supported provider has
a sync HTTPS entry point and
ask()itself is sync (matches the engine’s surface —Connection::executeetc. are all sync).
Functions§
- ask
- One-shot natural-language → SQL.
- ask_
with_ provider - Lower-level entry point — same flow, but you supply the provider.