Module: batteries/constrained-output
This module defines affordances for constraining the output of the model into specific formats, such as JSON, YAML, or Markdown.
Classes
Type Aliases
ObjectCompletion
Ƭ ObjectCompletion: ModelPropsWithChildren
& { example?
: string
; schema?
: z.ZodObject
<any
> ; validators?
: (obj
: object
) => void
[] }
Defined in
packages/ai-jsx/src/batteries/constrained-output.tsx:23
TypedObjectCompletion
Ƭ TypedObjectCompletion: ObjectCompletion
& { parser
: (str
: string
) => object
; typeName
: string
; partialResultCleaner?
: (str
: string
) => string
}
Defined in
packages/ai-jsx/src/batteries/constrained-output.tsx:45
TypedObjectCompletionWithRetry
Ƭ TypedObjectCompletionWithRetry: TypedObjectCompletion
& { retries?
: number
}
Defined in
packages/ai-jsx/src/batteries/constrained-output.tsx:61
Functions
JsonChatCompletion
▸ JsonChatCompletion(«destructured»
, «destructured»
): AsyncGenerator
<string
, string
, unknown
>
A ChatCompletion component that constrains the output to be a valid JSON string. It uses a combination of prompt engineering and validation with retries to ensure that the output is valid.
Though not required, you can provide a Zod schema to validate the output against. This is useful for ensuring that the output is of the expected type.
Example
const FamilyTree: z.Schema = z.array(
z.object({
name: z.string(),
children: z.lazy(() => FamilyTree).optional(),
})
);
const RootFamilyTree: z.ZodObject<any> = z.object({
tree: FamilyTree,
});
return (
<JsonChatCompletion schema={RootFamilyTree}>
<UserMessage>
Create a nested family tree with names and ages.
It should include a total of 5 people.
</UserMessage>
</JsonChatCompletion>
);
Parameters
Name | Type |
---|---|
«destructured» | Omit <TypedObjectCompletionWithRetry , "typeName" | "parser" | "partialResultCleaner" > |
«destructured» | ComponentContext |
Returns
AsyncGenerator
<string
, string
, unknown
>
A string that is a valid JSON or throws an error after retries
attempts.
Intermediate results that are valid, are also yielded.
Defined in
packages/ai-jsx/src/batteries/constrained-output.tsx:95
YamlChatCompletion
▸ YamlChatCompletion(props
, «destructured»
): AsyncGenerator
<string
, string
, unknown
>
A ChatCompletion component that constrains the output to be a valid YAML string. It uses a combination of prompt engineering and validation with retries to ensure that the output is valid.
Though not required, you can provide a Zod schema to validate the output against. This is useful for ensuring that the output is of the expected type.
Example
const FamilyTree: z.Schema = z.array(
z.object({
name: z.string(),
children: z.lazy(() => FamilyTree).optional(),
})
);
const RootFamilyTree: z.ZodObject<any> = z.object({
tree: FamilyTree,
});
return (
<YamlChatCompletion schema={RootFamilyTree}>
<UserMessage>
Create a nested family tree with names and ages.
It should include a total of 5 people.
</UserMessage>
</YamlChatCompletion>
);
Parameters
Name | Type |
---|---|
props | Omit <TypedObjectCompletionWithRetry , "typeName" | "parser" > |
«destructured» | ComponentContext |
Returns
AsyncGenerator
<string
, string
, unknown
>
A string that is a valid YAML or throws an error after retries
attempts.
Intermediate results that are valid, are also yielded.