-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add llguidance-based constrained generation (@huggingface/transformers-llguidance)
#1733
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
Open
nico-martin
wants to merge
15
commits into
main
Choose a base branch
from
feat/transformers-llguidance-js
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,925
−3
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b179ef0
first draft for #1328 using llguidance
nico-martin a1c66f8
removed plan
nico-martin b64ba30
replaced response_schema with logits_processor
nico-martin 56a8114
clean up
nico-martin 00deb59
first POC
nico-martin 8b7dc30
added LlguidanceConstraint
nico-martin 097dd65
added regex example
nico-martin e695be3
clean up and added more unit tests
nico-martin a7ab7f3
clean up
nico-martin 1eb0175
performance improvements
nico-martin c7d0400
updated llguidamce to 0.2.0
nico-martin 4d399a7
clean up
nico-martin 6331dd4
copilot review
nico-martin b9881af
removed llguidance dependency and created a much faster variant of re…
nico-martin 7c4593c
clean up
nico-martin 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # @huggingface/transformers-response-constraint | ||
|
|
||
| Experimental constrained-generation helpers for Transformers.js. | ||
|
|
||
| This dependency-free package exports `ResponseConstraint`, which turns a JSON-schema, JSON-object, or regex response format into the `logits_processor` and `stopping_criteria` objects accepted by Transformers.js generation. | ||
|
|
||
| The constraint engine is implemented specifically for Transformers.js and has no runtime dependencies. | ||
|
|
||
| ```js | ||
| import { ResponseConstraint } from "@huggingface/transformers-response-constraint"; | ||
|
|
||
| const constraint = ResponseConstraint.fromResponseFormat(tokenizer, { | ||
| type: "json_schema", | ||
| json_schema: { | ||
| type: "object", | ||
| properties: { | ||
| answer: { type: "string" }, | ||
| }, | ||
| required: ["answer"], | ||
| additionalProperties: false, | ||
| }, | ||
| }); | ||
|
|
||
| await model.generate({ | ||
| ...inputs, | ||
| logits_processor: constraint.logits_processor, | ||
| stopping_criteria: constraint.stopping_criteria, | ||
| }); | ||
| ``` | ||
|
|
||
| Constraints currently support a single generated sequence at a time. Generation throws when the logical batch size is not `1` rather than sharing mutable grammar state across sequences. | ||
|
|
||
| ## Supported constraints | ||
|
|
||
| The JSON engine implements a practical JSON Schema 2020-12 profile. This includes deep `const` and `enum`, exact decimal bounds and `multipleOf`, recognized string formats, tuple and homogeneous arrays, `contains`, deep `uniqueItems`, object property and dependency assertions, `allOf`/`anyOf`/`oneOf`/`not`, conditionals, local `$ref`, recursive `$defs`, draft-07 compatibility, and root-level `x-guidance` separators. External and dynamic references and unevaluated-property/item assertions remain unsupported. | ||
|
|
||
| The regex engine performs full-string matching and supports literals, UTF-8 literals, alternation, groups, character classes, `.`, `\\d`, `\\s`, `\\w`, anchors, and greedy `*`, `+`, `?`, and `{m,n}` quantifiers. Lookarounds, backreferences, lazy quantifiers, and Unicode character classes are intentionally unsupported. | ||
|
|
||
| When the generated bytes satisfy the constraint, the logits processor exposes only the tokenizer's EOS token as a valid completion. Sampling EOS updates the shared stopping criterion. | ||
|
|
||
| ## Regex constraints | ||
|
|
||
| Use `type: "regex"` to constrain generation to a regular expression. For example, this only allows ISO-like dates in `YYYY-MM-DD` format: | ||
|
|
||
| ```js | ||
| import { ResponseConstraint } from "@huggingface/transformers-response-constraint"; | ||
|
|
||
| const constraint = ResponseConstraint.fromResponseFormat(tokenizer, { | ||
| type: "regex", | ||
| regex: "\\d{4}-\\d{2}-\\d{2}", | ||
| }); | ||
|
|
||
| const output = await model.generate({ | ||
| ...inputs, | ||
| logits_processor: constraint.logits_processor, | ||
| stopping_criteria: constraint.stopping_criteria, | ||
| }); | ||
| ``` |
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,11 @@ | ||
| /** @type {import('jest').Config} */ | ||
| export default { | ||
| clearMocks: true, | ||
| collectCoverage: true, | ||
| coverageDirectory: "coverage", | ||
| coveragePathIgnorePatterns: ["node_modules", "tests"], | ||
| coverageProvider: "v8", | ||
| roots: ["./tests/"], | ||
| testTimeout: 32000, | ||
| transform: {}, | ||
| }; |
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,70 @@ | ||
| { | ||
| "name": "@huggingface/transformers-response-constraint", | ||
| "version": "0.0.0", | ||
| "description": "Dependency-free constrained generation for Transformers.js", | ||
| "main": "./dist/index.cjs", | ||
| "types": "./types/index.d.ts", | ||
| "type": "module", | ||
| "exports": { | ||
| "import": { | ||
| "types": "./types/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| }, | ||
| "require": { | ||
| "types": "./types/index.d.ts", | ||
| "default": "./dist/index.cjs" | ||
| } | ||
| }, | ||
| "scripts": { | ||
| "format": "prettier --write . --ignore-path ../../.prettierignore", | ||
| "format:check": "prettier --check . --ignore-path ../../.prettierignore", | ||
| "typegen": "tsc --build --force", | ||
| "dev": "node scripts/dev.mjs", | ||
| "build": "node scripts/build.mjs && pnpm typegen", | ||
| "performance": "node performance/run.mjs", | ||
| "pretest": "pnpm build", | ||
| "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose", | ||
| "test:json-corpus": "node tests/corpus/run.mjs", | ||
| "test:json-corpus:original": "node tests/corpus/run.mjs --original" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/huggingface/transformers.js.git" | ||
| }, | ||
| "keywords": [ | ||
| "transformers", | ||
| "transformers.js", | ||
| "huggingface", | ||
| "constrained-generation", | ||
| "structured-output", | ||
| "json-schema" | ||
| ], | ||
| "author": "Hugging Face", | ||
| "license": "Apache-2.0", | ||
| "bugs": { | ||
| "url": "https://github.com/huggingface/transformers.js/issues" | ||
| }, | ||
| "homepage": "https://github.com/huggingface/transformers.js#readme", | ||
| "peerDependencies": { | ||
| "@huggingface/transformers": "^4.2.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@huggingface/transformers": "workspace:*", | ||
| "@types/jest": "^30.0.0", | ||
| "@types/node": "^24.1.0", | ||
| "esbuild": "^0.27.2", | ||
| "jest": "^30.2.0", | ||
| "typescript": "5.9.3" | ||
| }, | ||
| "files": [ | ||
| "src", | ||
| "dist", | ||
| "types", | ||
| "README.md", | ||
| "LICENSE", | ||
| "!**/*.tsbuildinfo" | ||
| ], | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
packages/transformers-response-constraint/scripts/build.mjs
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,27 @@ | ||
| import { build } from "esbuild"; | ||
| import { rmSync } from "node:fs"; | ||
|
|
||
| rmSync("dist", { recursive: true, force: true }); | ||
| rmSync("types", { recursive: true, force: true }); | ||
|
|
||
| const common = { | ||
| entryPoints: ["src/index.ts"], | ||
| bundle: true, | ||
| platform: "neutral", | ||
| target: "es2022", | ||
| sourcemap: true, | ||
| external: ["@huggingface/transformers"], | ||
| }; | ||
|
|
||
| await Promise.all([ | ||
| build({ | ||
| ...common, | ||
| format: "esm", | ||
| outfile: "dist/index.js", | ||
| }), | ||
| build({ | ||
| ...common, | ||
| format: "cjs", | ||
| outfile: "dist/index.cjs", | ||
| }), | ||
| ]); |
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,65 @@ | ||
| import { context } from "esbuild"; | ||
| import { spawn } from "node:child_process"; | ||
| import { rmSync } from "node:fs"; | ||
|
|
||
| rmSync("dist", { recursive: true, force: true }); | ||
| rmSync("types", { recursive: true, force: true }); | ||
|
|
||
| const watchLogger = { | ||
| name: "watch-logger", | ||
| setup(build) { | ||
| let startTime = 0; | ||
|
|
||
| build.onStart(() => { | ||
| startTime = performance.now(); | ||
| console.log(`[transformers-response-constraint] rebuilding ${build.initialOptions.outfile}...`); | ||
| }); | ||
|
|
||
| build.onEnd((result) => { | ||
| const duration = (performance.now() - startTime).toFixed(2); | ||
| if (result.errors.length > 0) { | ||
| console.log(`[transformers-response-constraint] rebuild failed in ${duration}ms`); | ||
| } else { | ||
| console.log(`[transformers-response-constraint] rebuilt ${build.initialOptions.outfile} in ${duration}ms`); | ||
| } | ||
| }); | ||
| }, | ||
| }; | ||
|
|
||
| const common = { | ||
| entryPoints: ["src/index.ts"], | ||
| bundle: true, | ||
| platform: "neutral", | ||
| target: "es2022", | ||
| sourcemap: true, | ||
| external: ["@huggingface/transformers"], | ||
| plugins: [watchLogger], | ||
| }; | ||
|
|
||
| const contexts = await Promise.all([ | ||
| context({ | ||
| ...common, | ||
| format: "esm", | ||
| outfile: "dist/index.js", | ||
| }), | ||
| context({ | ||
| ...common, | ||
| format: "cjs", | ||
| outfile: "dist/index.cjs", | ||
| }), | ||
| ]); | ||
|
|
||
| await Promise.all(contexts.map((ctx) => ctx.watch())); | ||
|
|
||
| const tscWatch = spawn("tsc", ["--build", "--watch", "--preserveWatchOutput"], { | ||
| stdio: "inherit", | ||
| shell: true, | ||
| }); | ||
|
|
||
| console.log("Watching @huggingface/transformers-response-constraint..."); | ||
|
|
||
| process.on("SIGINT", async () => { | ||
| tscWatch.kill(); | ||
| await Promise.all(contexts.map((ctx) => ctx.dispose())); | ||
| process.exit(0); | ||
| }); |
92 changes: 92 additions & 0 deletions
92
packages/transformers-response-constraint/src/ResponseConstraint.ts
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,92 @@ | ||
| import { LogitsProcessor, LogitsProcessorList, StoppingCriteria, type Tensor } from '@huggingface/transformers'; | ||
|
|
||
| import { | ||
| createTokenConstraint, | ||
| prepareTokenizer, | ||
| type JSONSchema, | ||
| type TokenConstraint, | ||
| type TokenizerSource, | ||
| } from './engine'; | ||
| import { applyMask } from './utils/mask'; | ||
|
|
||
| export type ResponseFormat = | ||
| | { type: 'json_object' } | ||
| | { type: 'json_schema'; json_schema: JSONSchema } | ||
| | { type: 'regex'; regex: string }; | ||
|
|
||
| type GenerationState = { | ||
| completed: boolean; | ||
| constraint: TokenConstraint; | ||
| mask?: Uint32Array; | ||
| }; | ||
|
|
||
| export class ResponseConstraint { | ||
| /** | ||
| * Precomputes the tokenizer-derived data structures used by every | ||
| * constraint. The first constraint per tokenizer otherwise pays this cost | ||
| * (hundreds of milliseconds for large vocabularies) inside | ||
| * `fromResponseFormat`; call this once after loading the model to pay it | ||
| * early instead. | ||
| */ | ||
| static warmup(tokenizer: TokenizerSource): void { | ||
| prepareTokenizer(tokenizer); | ||
| } | ||
|
|
||
| static fromResponseFormat(tokenizer: TokenizerSource, responseFormat: ResponseFormat) { | ||
| const state: GenerationState = { | ||
| completed: false, | ||
| constraint: createTokenConstraint(tokenizer, responseFormat), | ||
| }; | ||
| const logits_processor = new LogitsProcessorList(); | ||
| logits_processor.push(new ConstraintLogitsProcessor(state)); | ||
| return { | ||
| logits_processor, | ||
| stopping_criteria: new ConstraintStoppingCriteria(state), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| class ConstraintLogitsProcessor extends LogitsProcessor { | ||
| constructor(private readonly state: GenerationState) { | ||
| super(); | ||
| } | ||
|
|
||
| _call(inputIds: bigint[][], logits: Tensor) { | ||
| assertSingleSequence(inputIds.length); | ||
| if (this.state.completed) return logits; | ||
| const logitsVocabSize = logits.dims.at(-1); | ||
| if (logitsVocabSize === undefined || !Number.isInteger(logitsVocabSize) || logitsVocabSize <= 0) { | ||
| throw new Error('ResponseConstraint requires logits with a vocabulary dimension.'); | ||
| } | ||
| const words = Math.ceil(logitsVocabSize / 32); | ||
| if (this.state.mask?.length !== words) this.state.mask = new Uint32Array(words); | ||
| if (!this.state.constraint.fillMask(this.state.mask)) { | ||
| throw new Error('The constraint reached a dead end before producing a valid output.'); | ||
| } | ||
| applyMask(logits, this.state.mask, this.state.constraint.vocabSize); | ||
| return logits; | ||
| } | ||
|
|
||
| onTokensSampled(tokenIds: number[], inputIds: bigint[][]) { | ||
| assertSingleSequence(tokenIds.length); | ||
| assertSingleSequence(inputIds.length); | ||
| if (!this.state.completed) this.state.completed = this.state.constraint.commit(tokenIds[0]); | ||
| } | ||
| } | ||
|
|
||
| class ConstraintStoppingCriteria extends StoppingCriteria { | ||
| constructor(private readonly state: GenerationState) { | ||
| super(); | ||
| } | ||
|
|
||
| _call(inputIds: ArrayLike<unknown>[]) { | ||
| assertSingleSequence(inputIds.length); | ||
| return [this.state.completed]; | ||
| } | ||
| } | ||
|
|
||
| function assertSingleSequence(batchSize: number): void { | ||
| if (batchSize !== 1) { | ||
| throw new Error(`ResponseConstraint currently supports batch size 1; received ${batchSize}.`); | ||
| } | ||
| } |
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.