feat(ai): support predicate criteria in decisions (#62819)

This commit is contained in:
Aiden Cline 2026-11-08 22:25:31 -06:00 • committed by GitHub
parent f29228b003
commit fee1ca5aa9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 144 additions and 4 deletions

View file

@ -322,6 +322,8 @@ const [refund, department] = response.answers
```
`input` is a JSON object or an ordered list of `text`, `json` and `media` parts. Answers come back in question order.
A `predicate` can say what counts as true and as false with `criteria: { true, false }`; hosts without a criteria field
read them appended to the instructions.
A question the host declines is `{ name, type: "error", reason, message? }`, with `reason` such as `"refusal"`; the
other answers still come back.
`response.usage` has `inputTokens`, `outputTokens`, `totalTokens` and the host's reported `cost` when it sends one;

View file

@ -51,6 +51,10 @@ export const PredicateQuestion = Schema.Struct({
name: Schema.String,
type: Schema.Literal("predicate"),
instructions: Schema.String,
/** What counts as true and as false, for statements the instructions alone leave ambiguous. */
criteria: Schema.optional(
Schema.Struct({ true: Schema.optional(Schema.String), false: Schema.optional(Schema.String) }),
),
})
export type PredicateQuestion = Schema.Schema.Type<typeof PredicateQuestion>

View file

@ -72,7 +72,20 @@ const fromRequest = Effect.fn("OpenAIDecisions.fromRequest")(function* (request:
),
model: request.model.id,
input: [{ role: "user", content: yield* Effect.forEach(request.input, (part) => inputPart(request, part)) }],
questions: request.questions,
questions: request.questions.map((question) => {
if (question.type !== "predicate") return question
// Predicates have no criteria field, so what true and false mean is appended to the instructions.
const meanings = [
question.criteria?.true === undefined ? [] : [`True means: ${question.criteria.true}`],
question.criteria?.false === undefined ? [] : [`False means: ${question.criteria.false}`],
].flat()
return {
name: question.name,
type: question.type,
instructions:
meanings.length === 0 ? question.instructions : `${question.instructions}\n\n${meanings.join("\n")}`,
}
}),
}
})

View file

@ -70,7 +70,14 @@ export const encodeBody = Effect.fn("SystemOne.encodeBody")(function* (request:
const encodeQuestions = (questions: ReadonlyArray<DecisionQuestion>) =>
Effect.forEach(questions, (question): Effect.Effect<readonly [string, unknown], AIError> => {
if (question.type === "predicate")
return Effect.succeed([question.name, { type: "noul", instructions: question.instructions }] as const)
return Effect.succeed([
question.name,
{
type: "noul",
instructions: question.instructions,
...(question.criteria === undefined ? {} : { criteria: question.criteria }),
},
] as const)
if (question.type === "score")
return Effect.succeed([
question.name,

View file

@ -35,6 +35,16 @@ const questions = [
{ name: "refund", type: "predicate", instructions: "Is the customer asking for a refund?" },
] as const
const defect = {
name: "defect",
type: "predicate",
instructions: "Is the customer reporting a product defect?",
criteria: {
true: "The product itself is broken or faulty.",
false: "Preference, sizing, shipping, or pricing complaint.",
},
} as const
const systemOneAnswers = {
department: { type: "choice", choice: "billing", probabilities: { technical: 0.1, billing: 0.9 }, confidence: 0.8 },
urgency: {
@ -516,6 +526,76 @@ describe("Decision", () => {
),
)
it.effect("sends predicate criteria as System One criteria", () =>
ai.decision
.generate({
model: TypeSafeAI.configure({ apiKey: "test" }).decision("jev-latest"),
input: [{ type: "text", text: "I returned the sweater because I prefer green." }],
questions: [defect],
})
.pipe(
Effect.tap((response) =>
Effect.sync(() =>
expect(response.answers).toEqual([{ name: "defect", type: "predicate", probability: 0.03 }]),
),
),
Effect.provide(
client((input) => {
expect(JSON.parse(input.text).questions).toEqual({
defect: {
type: "noul",
instructions: "Is the customer reporting a product defect?",
criteria: {
true: "The product itself is broken or faulty.",
false: "Preference, sizing, shipping, or pricing complaint.",
},
},
})
return Effect.succeed(
json(input, { model: "jev-latest", answers: { defect: { type: "noul", noul: 0.03 } } }),
)
}),
),
),
)
it.effect("appends predicate criteria to OpenAI Decisions instructions", () =>
ai.decision
.generate({
model: OpenAI.configure({ apiKey: "openai-key" }).decision("gpt-6-luna"),
input: [{ type: "text", text: "I returned the sweater because I prefer green." }],
questions: [defect, { ...defect, name: "only_true", criteria: { true: "The product is broken." } }],
})
.pipe(
Effect.provide(
client((input) => {
expect(JSON.parse(input.text).questions).toEqual([
{
name: "defect",
type: "predicate",
instructions:
"Is the customer reporting a product defect?\n\nTrue means: The product itself is broken or faulty.\nFalse means: Preference, sizing, shipping, or pricing complaint.",
},
{
name: "only_true",
type: "predicate",
instructions: "Is the customer reporting a product defect?\n\nTrue means: The product is broken.",
},
])
return Effect.succeed(
json(input, {
model: "gpt-6-luna",
answers: [
{ type: "predicate", name: "defect", probability: 0.03 },
{ type: "predicate", name: "only_true", probability: 0.04 },
],
}),
)
}),
),
),
)
it.effect("puts media in Clef's top-level lists on Cloudflare Run", () =>
ai.decision
.generate({

View file

@ -3953,7 +3953,12 @@ export type ExperimentalGenerateDecisionsInput = {
| { readonly type: "file"; readonly data: string; readonly mime: string; readonly name?: string }
>
readonly questions: ReadonlyArray<
| { readonly type: "predicate"; readonly name: string; readonly instructions: string }
| {
readonly type: "predicate"
readonly name: string
readonly instructions: string
readonly criteria?: { readonly true?: string; readonly false?: string }
}
| {
readonly type: "choice"
readonly name: string

View file

@ -502,12 +502,26 @@ describe("image and decision generation", () => {
{ label: "Actively blocking revenue" },
],
},
{ type: "predicate", name: "refund", instructions: "Is the customer asking for a refund?" },
{
type: "predicate",
name: "refund",
instructions: "Is the customer asking for a refund?",
criteria: { true: "They want money back.", false: "Anything else." },
},
],
})
expect(requests[0]).toMatchObject({
url: "https://openrouter.ai/api/v1/systemone",
headers: { authorization: "Bearer openrouter-key" },
body: {
questions: {
refund: {
type: "noul",
instructions: "Is the customer asking for a refund?",
criteria: { true: "They want money back.", false: "Anything else." },
},
},
},
})
expect(result.answers.map((answer) => [answer.name, answer.type])).toEqual([
["department", "choice"],

View file

@ -19036,6 +19036,18 @@
},
"instructions": {
"type": "string"
},
"criteria": {
"type": "object",
"properties": {
"true": {
"type": "string"
},
"false": {
"type": "string"
}
},
"additionalProperties": false
}
},
"required": ["type", "name", "instructions"],

View file

@ -115,6 +115,9 @@ export const PredicateQuestion = Schema.Struct({
type: Schema.Literal("predicate"),
name: QuestionName,
instructions: Schema.String,
criteria: Schema.Struct({ true: Schema.String.pipe(optional), false: Schema.String.pipe(optional) })
.pipe(optional)
.annotate(description("What counts as true and as false, for statements the instructions alone leave ambiguous")),
}).annotate({ identifier: "Generate.PredicateQuestion", description: "The probability that a statement is true" })
export interface ChoiceQuestion extends Schema.Schema.Type<typeof ChoiceQuestion> {}