LLM Tooling
The grammar a model writes queries in is built with @aeye/schema from your registry. It is rendered in each model's own dialect and decodes the answer back into a QueryDef in one pass.
The query tool
import { buildQueryTool } from '@aeye/query';
const queryTool = buildQueryTool(engine, {
depth: 'paired',
errorFormat: 'full',
});
const query = await queryTool.parse(ctx, JSON.stringify({ query: queryDef })); // a runnable Query
const result = await queryTool.run(query, ctx); // runs itparse decodes, parses and validates. A mistake throws SchemaParseError, whose message is what the model is told: its own JSON with a marker under each problem and "did you mean" suggestions.
| Option | Description |
|---|---|
depth, functions, maxEnumSize, types, includeDocs | Shape the schema (below). |
max | Past this many types (default 5) the argument is a prose string instead. |
name, description | The tool's. |
runtime | Run options such as params and rls. |
errorFormat | 'summary', 'list', 'markers', 'full' (default), or an options object. |
Schemas
import { buildSchemas, querySchema } from '@aeye/query';
import { OPENAI_STRICT } from '@aeye/schema';
const { Query, Expr, Select } = buildSchemas(engine, { depth: 'paired' });
Query.render(OPENAI_STRICT).jsonSchema; // what the model is sent
const parsed = Query.render(OPENAI_STRICT).parse(answer); // { ok, value } or { ok: false, message }
const toolArgs = querySchema(engine, { depth: 'paired' }); // `{ query }` for your own Tool or PromptResults are cached per registry and options, so building them every turn is cheap. queryToolSchema is the same arguments wrapped so they decode into a runnable Query, and QueryGrammar is the module for composing your own results.
Schema depth
depth sets how tightly the grammar is locked to your data, on five axes.
| Axis | Levels (loose to tight) | Constrains |
|---|---|---|
refs | open, types, fields, both, paired | field references and joins |
typeNames | open, enum | from, into and other type names |
functions | open, names, typed | function names and arguments |
filters | open, paired | filters clauses |
writes | open, names, typed | insert rows and update set |
buildSchemas(engine, { depth: 'open' });
buildSchemas(engine, { depth: { refs: 'paired', functions: 'typed' } });
buildSchemas(engine, { depth: 'paired', maxEnumSize: 50 }); // loosen axes that overflow
buildSchemas(engine, { functions: { scalar: ['upper', 'lower'] } }); // offer only theseIndependently of depth, expression kinds that nothing could use are left out: semantic without an eligible type, array-op without an array field, joins without relations, and so on.
Choosing types
With a large model, narrow the schema to what a request needs. This ranks types semantically with an embedder.
import { selectTypes } from '@aeye/query';
const types = await selectTypes(engine, 'revenue by customer last month', { topN: 5, embedder });
buildQueryTool(engine, { depth: 'paired', types });Describing the engine
describeEngine renders one compact block a model can read: the types, the expression kinds in use, the functions, operators, worked examples and dialects.
import { describeEngine } from '@aeye/query';
const guide = describeEngine(engine, { types, maxExamples: 1 });The parts are available as describeType, describeTypes, describeExprs, describeFunctions, describeOperators, describeQueryExamples and describeDialects. instructions and examples options replace the shipped text per function or kind.
Without a tool
For a query from a file, a CLI or your own harness:
import { parseQueryTool, QueryToolError } from '@aeye/query';
const outcome = parseQueryTool(engine, { query: queryDef });
if (outcome instanceof QueryToolError) console.log(outcome.report);
else await engine.run(outcome);