Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions laya-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,36 @@ class MetricsHook extends BaseHook {
setDefaultHooks([new MetricsHook()]); // addDefaultHook(...) appends; clearDefaultHooks() resets
```

## Structured decisions (`decide`)

Turn a JSON schema into typed values in one call — the port of Python's `laya.structured`
(#280). Enum properties become choice questions, booleans become noul, bounded integers
become scores; anything the fixed-option model cannot answer (free strings, arrays, nested
objects, `$ref`) is rejected with a `SchemaError` naming the path:

```ts
import { Agent, decide } from "laya-ts";

const agent = await Agent.load("./dist/laya");
const values = await agent.decide(ticketText, {
type: "object",
properties: {
department: { type: "string", enum: ["billing", "support", "sales"] },
urgency: { type: "integer", minimum: 0, maximum: 2 },
needs_human: { type: "boolean" },
},
});
// { department: "billing", urgency: 2, needs_human: false }
```

`router.decide(...)` works the same way (routing options are forwarded to `predict`), and the
free `decide(runner, state, schema, opts)` accepts anything with a `predict` method. Pass
`{ returnDetails: true }` for per-field confidence and probabilities, or `{ questions }`
instead of a schema to get raw answers. Zod/TypeBox users can pass `z.toJSONSchema(Model)` —
any object with a `toJSONSchema()` method is accepted. `planFromJsonSchema`,
`questionsFromJsonSchema` and `answersToJson` expose the planning and projection steps.

## Shortlist (many labels)
## Shortlist (many labels)

```ts
Expand Down
24 changes: 24 additions & 0 deletions laya-ts/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "./common.js";
import type { Batch, SessionProvider } from "./providers.js";
import { encodeWithData, parseTokenizerJson, type TokenizerLike } from "./tokenizer.js";
import { decide, type DecideOptions, type DecisionResult } from "./structured.js";
import {
HookRegistry,
PredictContext,
Expand Down Expand Up @@ -441,6 +442,29 @@ export class Agent extends HookRegistry {
return this.systemOne(state, questions, opts);
}

/**
* Answer `state` against a JSON schema (or explicit `opts.questions`) and return typed
* values — see `structured.ts`. Pass exactly one of `schema` or `opts.questions`; other
* options are forwarded to `predict`.
*/
async decide(
state: unknown,
schema: unknown,
opts: DecideOptions & PredictOptions & { returnDetails: true },
): Promise<DecisionResult>;
async decide(
state: unknown,
schema?: unknown,
opts?: DecideOptions & PredictOptions,
): Promise<Record<string, unknown>>;
async decide(
state: unknown,
schema?: unknown,
opts: DecideOptions & PredictOptions = {},
): Promise<Record<string, unknown> | DecisionResult> {
return decide(this, state, schema, opts);
}

static async load(
modelDirOrRepo: string,
opts?: {
Expand Down
11 changes: 11 additions & 0 deletions laya-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ export {
} from "./hooks.js";
export type { Hook, HookEvent, HookArg, PredictHook, PredictHookArg } from "./hooks.js";
export { triageQuestions, emailQuestions, guardQuestions, moderationQuestions, routerQuestions } from "./presets.js";
export {
decide,
planFromJsonSchema,
questionsFromJsonSchema,
answersToJson,
SchemaError,
MAX_PROPERTIES,
MAX_OPTIONS,
MAX_SCORE_LEVELS,
} from "./structured.js";
export type { DecisionResult, DecideOptions, DecideRunner, PlannedField } from "./structured.js";
export {
renderOptions,
serializeState,
Expand Down
24 changes: 24 additions & 0 deletions laya-ts/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { analyse, type AnalyseResult } from "./lang.js";
import type { PredictOptions, QuestionDef, SystemOneResult } from "./agent.js";
import { decide, type DecideOptions, type DecisionResult } from "./structured.js";
import {
HookRegistry,
PredictContext,
Expand Down Expand Up @@ -488,6 +489,29 @@ export class Router extends HookRegistry {
return (ctx.results as unknown as RoutedResult[])[0];
}

/**
* Answer `state` against a JSON schema (or explicit `opts.questions`) and return typed
* values — see `structured.ts`. Routing options (`model`, `task`, ...) are forwarded to
* `predict`.
*/
async decide(
state: unknown,
schema: unknown,
opts: DecideOptions & RouteOptions & PredictOptions & { returnDetails: true },
): Promise<DecisionResult>;
async decide(
state: unknown,
schema?: unknown,
opts?: DecideOptions & RouteOptions & PredictOptions,
): Promise<Record<string, unknown>>;
async decide(
state: unknown,
schema?: unknown,
opts: DecideOptions & RouteOptions & PredictOptions = {},
): Promise<Record<string, unknown> | DecisionResult> {
return decide(this, state, schema, opts);
}

async systemOne(
state: unknown,
questions: Record<string, QuestionDef>,
Expand Down
Loading
Loading