Skip to content

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 ​

typescript
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 it

parse 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.

OptionDescription
depth, functions, maxEnumSize, types, includeDocsShape the schema (below).
maxPast this many types (default 5) the argument is a prose string instead.
name, descriptionThe tool's.
runtimeRun options such as params and rls.
errorFormat'summary', 'list', 'markers', 'full' (default), or an options object.

Schemas ​

typescript
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 Prompt

Results 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.

AxisLevels (loose to tight)Constrains
refsopen, types, fields, both, pairedfield references and joins
typeNamesopen, enumfrom, into and other type names
functionsopen, names, typedfunction names and arguments
filtersopen, pairedfilters clauses
writesopen, names, typedinsert rows and update set
typescript
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 these

Independently 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.

typescript
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.

typescript
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:

typescript
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);

Released under the GPL-3.0 License.