Repository navigation
feat(ai): support constrained sampling #6341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,325
−210
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f9a15fe
feat(ai): support constrained sampling
mitsuhiko 86b8183
Merge branch 'main' into grammar-constraints
mitsuhiko b381fe8
Merge branch 'main' into grammar-constraints
mitsuhiko 5c0c8cf
feat(ai): gate grammar tools via catalog supportsGrammarTools compat
mitsuhiko 8f8029c
Merge branch 'main' into grammar-constraints
mitsuhiko a8bbe8d
feat(ai): merge main into grammar-constraints
mitsuhiko 29b7e36
feat(ai): sync grammar constraints upstream
mitsuhiko 31925f0
feat(ai): merge main into grammar-constraints
mitsuhiko aaa6438
feat(ai): merge main into grammar-constraints
mitsuhiko 0578b9a
fix(ai): enforce constrained tool sampling
mitsuhiko dab145a
fix(ai): validate constrained sampling capabilities
mitsuhiko 91fb882
feat(ai): merge main into grammar-constraints
mitsuhiko 2053cfd
fix(ai): model constrained sampling capabilities explicitly
mitsuhiko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import type { Tool } from "../types.ts"; | ||
|
|
||
| interface JsonSchemaObject { | ||
| type?: unknown; | ||
| properties?: Record<string, JsonSchemaObject | undefined>; | ||
| required?: unknown; | ||
| } | ||
|
|
||
| export interface GrammarConstrainedSampling { | ||
| format: "lark" | "regex"; | ||
| definition: string; | ||
| inputProperty: string; | ||
| } | ||
|
|
||
| export interface GrammarToolInputJsonBuffer { | ||
| input: string; | ||
| started: boolean; | ||
| closed: boolean; | ||
| } | ||
|
|
||
| export function appendGrammarToolInputJsonDelta( | ||
| buffer: GrammarToolInputJsonBuffer, | ||
| inputProperty: string, | ||
| nextInput: string, | ||
| close: boolean, | ||
| ): string | undefined { | ||
| if (buffer.closed) { | ||
| if (close && nextInput === buffer.input) return undefined; | ||
| throw new Error(`grammar tool input for property "${inputProperty}" changed after it was closed`); | ||
| } | ||
| if (!nextInput.startsWith(buffer.input)) { | ||
| throw new Error(`grammar tool input for property "${inputProperty}" changed non-monotonically`); | ||
| } | ||
|
|
||
| const inputDelta = nextInput.slice(buffer.input.length); | ||
| if (!close && inputDelta.length === 0) return undefined; | ||
|
|
||
| let delta = ""; | ||
| if (!buffer.started) { | ||
| delta += `{${JSON.stringify(inputProperty)}:"`; | ||
| buffer.started = true; | ||
| } | ||
| delta += JSON.stringify(inputDelta).slice(1, -1); | ||
| buffer.input = nextInput; | ||
|
|
||
| if (close) { | ||
| delta += '"}'; | ||
| buffer.closed = true; | ||
| } | ||
| return delta; | ||
| } | ||
|
|
||
| function inferGrammarInputProperty(tool: Tool): string { | ||
| const schema = tool.parameters as JsonSchemaObject; | ||
| if (schema.type !== "object") { | ||
| throw new Error("grammar constrained sampling requires an object parameter schema"); | ||
| } | ||
| if (!Array.isArray(schema.required) || schema.required.length !== 1 || typeof schema.required[0] !== "string") { | ||
| throw new Error("grammar constrained sampling requires exactly one required string property"); | ||
| } | ||
|
|
||
| const inputProperty = schema.required[0]; | ||
| if (!schema.properties?.[inputProperty]) { | ||
| throw new Error(`grammar constrained sampling requires a properties entry for ${inputProperty}`); | ||
| } | ||
| if (schema.properties[inputProperty]?.type !== "string") { | ||
| throw new Error(`grammar constrained sampling property ${inputProperty} must have type string`); | ||
| } | ||
| return inputProperty; | ||
| } | ||
|
|
||
| export function resolveJsonSchemaStrictSampling(tool: Tool, supportsStrictMode: boolean): boolean | undefined { | ||
| const config = tool.constrainedSampling; | ||
| if (!config || config.type !== "json_schema") { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (supportsStrictMode) { | ||
| return true; | ||
| } | ||
| if (config.strict === "require") { | ||
| throw new Error( | ||
| `Tool "${tool.name}" requires JSON-schema constrained sampling, but strict tools are unsupported.`, | ||
| ); | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| export function resolveGrammarConstrainedSampling( | ||
| tool: Tool, | ||
| supportsGrammarTools: boolean, | ||
| ): GrammarConstrainedSampling | undefined { | ||
| const config = tool.constrainedSampling; | ||
| if (!config || config.type !== "grammar") { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (!supportsGrammarTools) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const definition = config.variants.openai_lark ?? config.variants.openai_regex; | ||
| if (definition === undefined) { | ||
| return undefined; | ||
| } | ||
|
|
||
| try { | ||
| return { | ||
| format: config.variants.openai_lark !== undefined ? "lark" : "regex", | ||
| definition, | ||
| inputProperty: inferGrammarInputProperty(tool), | ||
| }; | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| throw new Error(`Tool "${tool.name}" cannot use grammar constrained sampling: ${message}.`); | ||
| } | ||
| } | ||
|
|
||
| export function createGrammarToolInputProperties( | ||
| tools: Tool[] | undefined, | ||
| supportsGrammarTools: boolean, | ||
| ): ReadonlyMap<string, string> { | ||
| const properties = new Map<string, string>(); | ||
| for (const tool of tools ?? []) { | ||
| const grammar = resolveGrammarConstrainedSampling(tool, supportsGrammarTools); | ||
| if (grammar) { | ||
| properties.set(tool.name, grammar.inputProperty); | ||
| } | ||
| } | ||
| return properties; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.