@huggingface/transformers-structured-output 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +56 -0
- package/dist/index.cjs +2425 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.js +2402 -0
- package/dist/index.js.map +7 -0
- package/package.json +65 -0
- package/src/StructuredOutputProcessor.ts +103 -0
- package/src/engine/constraint.ts +331 -0
- package/src/engine/index.ts +2 -0
- package/src/engine/json.ts +1853 -0
- package/src/engine/regex.ts +419 -0
- package/src/engine/tokenizer.ts +205 -0
- package/src/engine/types.ts +12 -0
- package/src/index.ts +2 -0
- package/src/utils/mask.ts +30 -0
- package/types/StructuredOutputProcessor.d.ts +22 -0
- package/types/StructuredOutputProcessor.d.ts.map +1 -0
- package/types/engine/constraint.d.ts +30 -0
- package/types/engine/constraint.d.ts.map +1 -0
- package/types/engine/index.d.ts +3 -0
- package/types/engine/index.d.ts.map +1 -0
- package/types/engine/json.d.ts +70 -0
- package/types/engine/json.d.ts.map +1 -0
- package/types/engine/regex.d.ts +3 -0
- package/types/engine/regex.d.ts.map +1 -0
- package/types/engine/tokenizer.d.ts +8 -0
- package/types/engine/tokenizer.d.ts.map +1 -0
- package/types/engine/types.d.ts +16 -0
- package/types/engine/types.d.ts.map +1 -0
- package/types/index.d.ts +3 -0
- package/types/index.d.ts.map +1 -0
- package/types/utils/mask.d.ts +3 -0
- package/types/utils/mask.d.ts.map +1 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { LogitsProcessorList } from '@huggingface/transformers';
|
|
2
|
+
import { type JSONSchema, type TokenizerSource } from './engine';
|
|
3
|
+
export type ResponseFormat = {
|
|
4
|
+
type: 'json_object';
|
|
5
|
+
} | {
|
|
6
|
+
type: 'json_schema';
|
|
7
|
+
json_schema: JSONSchema;
|
|
8
|
+
} | {
|
|
9
|
+
type: 'regex';
|
|
10
|
+
regex: string;
|
|
11
|
+
};
|
|
12
|
+
export declare class StructuredOutputProcessor extends LogitsProcessorList {
|
|
13
|
+
/**
|
|
14
|
+
* Precomputes the tokenizer-derived data structures used by every
|
|
15
|
+
* constraint. The first processor per tokenizer otherwise pays this cost
|
|
16
|
+
* (hundreds of milliseconds for large vocabularies) in its constructor;
|
|
17
|
+
* call this once after loading the model to pay it early instead.
|
|
18
|
+
*/
|
|
19
|
+
static warmup(tokenizer: TokenizerSource): void;
|
|
20
|
+
constructor(tokenizer: TokenizerSource, responseFormat: ResponseFormat);
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=StructuredOutputProcessor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StructuredOutputProcessor.d.ts","sourceRoot":"","sources":["../src/StructuredOutputProcessor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,mBAAmB,EAAe,MAAM,2BAA2B,CAAC;AAE9F,OAAO,EAGH,KAAK,UAAU,EAEf,KAAK,eAAe,EACvB,MAAM,UAAU,CAAC;AAGlB,MAAM,MAAM,cAAc,GACpB;IAAE,IAAI,EAAE,aAAa,CAAA;CAAE,GACvB;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,WAAW,EAAE,UAAU,CAAA;CAAE,GAChD;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAWvC,qBAAa,yBAA0B,SAAQ,mBAAmB;IAC9D;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,eAAe,GAAG,IAAI;gBAInC,SAAS,EAAE,eAAe,EAAE,cAAc,EAAE,cAAc;CAOzE"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { JSONSchema, TokenizerSource } from './types';
|
|
2
|
+
type ResponseFormat = {
|
|
3
|
+
type: 'json_object';
|
|
4
|
+
} | {
|
|
5
|
+
type: 'json_schema';
|
|
6
|
+
json_schema: JSONSchema;
|
|
7
|
+
} | {
|
|
8
|
+
type: 'regex';
|
|
9
|
+
regex: string;
|
|
10
|
+
};
|
|
11
|
+
export type TokenConstraint = {
|
|
12
|
+
vocabSize: number;
|
|
13
|
+
fillMask(target: Uint32Array): boolean;
|
|
14
|
+
commit(tokenId: number): boolean;
|
|
15
|
+
repeatedWhitespace(): {
|
|
16
|
+
tokenIds: readonly number[];
|
|
17
|
+
count: number;
|
|
18
|
+
} | undefined;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Builds and caches the tokenizer-derived data structures (token tries, string
|
|
22
|
+
* masks) ahead of time. This is the expensive part of creating the first
|
|
23
|
+
* constraint for a tokenizer (hundreds of milliseconds for a 256k vocabulary),
|
|
24
|
+
* so calling this right after loading a model moves that cost off the first
|
|
25
|
+
* generation. Subsequent calls with the same tokenizer are free.
|
|
26
|
+
*/
|
|
27
|
+
export declare function prepareTokenizer(tokenizerSource: TokenizerSource): void;
|
|
28
|
+
export declare function createTokenConstraint(tokenizerSource: TokenizerSource, responseFormat: ResponseFormat): TokenConstraint;
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=constraint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constraint.d.ts","sourceRoot":"","sources":["../../src/engine/constraint.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAmB,UAAU,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAmB5E,KAAK,cAAc,GACb;IAAE,IAAI,EAAE,aAAa,CAAA;CAAE,GACvB;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,WAAW,EAAE,UAAU,CAAA;CAAE,GAChD;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvC,MAAM,MAAM,eAAe,GAAG;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC;IACvC,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;IACjC,kBAAkB,IAAI;QAAE,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC;CACpF,CAAC;AAKF;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,eAAe,EAAE,eAAe,GAAG,IAAI,CAEvE;AAED,wBAAgB,qBAAqB,CACjC,eAAe,EAAE,eAAe,EAChC,cAAc,EAAE,cAAc,GAC/B,eAAe,CA+FjB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/engine/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,KAAK,eAAe,EAAE,MAAM,cAAc,CAAC;AAC7F,YAAY,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { ConstraintState, JSONSchema } from './types';
|
|
2
|
+
type Schema = boolean | Record<string, unknown>;
|
|
3
|
+
type JsonNode = {
|
|
4
|
+
kind: 'null';
|
|
5
|
+
value: null;
|
|
6
|
+
} | {
|
|
7
|
+
kind: 'boolean';
|
|
8
|
+
value: boolean;
|
|
9
|
+
} | {
|
|
10
|
+
kind: 'number';
|
|
11
|
+
value: number;
|
|
12
|
+
raw: string;
|
|
13
|
+
} | {
|
|
14
|
+
kind: 'string';
|
|
15
|
+
value: string;
|
|
16
|
+
} | {
|
|
17
|
+
kind: 'array';
|
|
18
|
+
value: unknown[];
|
|
19
|
+
items: JsonNode[];
|
|
20
|
+
} | {
|
|
21
|
+
kind: 'object';
|
|
22
|
+
value: Record<string, unknown>;
|
|
23
|
+
entries: Array<{
|
|
24
|
+
key: string;
|
|
25
|
+
node: JsonNode;
|
|
26
|
+
}>;
|
|
27
|
+
};
|
|
28
|
+
type Guidance = {
|
|
29
|
+
itemSeparator: string;
|
|
30
|
+
keySeparator: string;
|
|
31
|
+
itemBytes: Uint8Array;
|
|
32
|
+
keyBytes: Uint8Array;
|
|
33
|
+
whitespaceFlexible: boolean;
|
|
34
|
+
};
|
|
35
|
+
type Mode = 'value' | 'array-value' | 'object-key' | 'colon' | 'item-separator' | 'after-value' | 'string' | 'key-string' | 'escape' | 'key-escape' | 'unicode' | 'key-unicode' | 'number' | 'literal' | 'done' | 'dead';
|
|
36
|
+
type ObjectFrame = {
|
|
37
|
+
kind: 'object';
|
|
38
|
+
schema: Schema;
|
|
39
|
+
seen: ReadonlySet<string>;
|
|
40
|
+
entries: ReadonlyArray<{
|
|
41
|
+
key: string;
|
|
42
|
+
node: JsonNode;
|
|
43
|
+
}>;
|
|
44
|
+
key?: string;
|
|
45
|
+
childSchema?: Schema;
|
|
46
|
+
};
|
|
47
|
+
type ArrayFrame = {
|
|
48
|
+
kind: 'array';
|
|
49
|
+
schema: Schema;
|
|
50
|
+
length: number;
|
|
51
|
+
items: readonly JsonNode[];
|
|
52
|
+
};
|
|
53
|
+
type Frame = ObjectFrame | ArrayFrame;
|
|
54
|
+
type JsonState = {
|
|
55
|
+
mode: Mode;
|
|
56
|
+
schema: Schema;
|
|
57
|
+
stack: readonly Frame[];
|
|
58
|
+
bytes?: readonly number[];
|
|
59
|
+
stringLength?: number;
|
|
60
|
+
stringPending?: number;
|
|
61
|
+
text?: string;
|
|
62
|
+
literal?: string;
|
|
63
|
+
literalValue?: null | boolean;
|
|
64
|
+
index?: number;
|
|
65
|
+
highSurrogate?: number;
|
|
66
|
+
guidance: Guidance;
|
|
67
|
+
};
|
|
68
|
+
export declare function compileJsonSchema(schema: JSONSchema, stringKeyClamp?: number): ConstraintState<JsonState>;
|
|
69
|
+
export {};
|
|
70
|
+
//# sourceMappingURL=json.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/engine/json.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA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E,MAAM,SAAS,CAAC;AAY/C,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,eAAe,CAAC,MAAM,CAAC,CASpE"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { TokenizerSource } from './types';
|
|
2
|
+
export type TokenizerData = {
|
|
3
|
+
tokens: Uint8Array[];
|
|
4
|
+
eosTokenId: number;
|
|
5
|
+
specialTokenIds: Set<number>;
|
|
6
|
+
};
|
|
7
|
+
export declare function extractTokenizer(tokenizer: TokenizerSource): TokenizerData;
|
|
8
|
+
//# sourceMappingURL=tokenizer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokenizer.d.ts","sourceRoot":"","sources":["../../src/engine/tokenizer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAI/C,MAAM,MAAM,aAAa,GAAG;IACxB,MAAM,EAAE,UAAU,EAAE,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,eAAe,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAChC,CAAC;AAKF,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,eAAe,GAAG,aAAa,CAyD1E"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type JSONValue = null | boolean | number | string | JSONValue[] | {
|
|
2
|
+
[key: string]: JSONValue;
|
|
3
|
+
};
|
|
4
|
+
export type JSONSchema = boolean | {
|
|
5
|
+
[key: string]: JSONValue;
|
|
6
|
+
};
|
|
7
|
+
export type TokenizerSource = object | ((...args: unknown[]) => unknown);
|
|
8
|
+
export interface ConstraintState<State> {
|
|
9
|
+
readonly initial: State;
|
|
10
|
+
transition(state: State, byte: number): State;
|
|
11
|
+
viable(state: State): boolean;
|
|
12
|
+
accepting(state: State): boolean;
|
|
13
|
+
stringCapacity?(state: State): number | undefined;
|
|
14
|
+
maskKey?(state: State): string | undefined;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/engine/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,IAAI,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,EAAE,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AACtG,MAAM,MAAM,UAAU,GAAG,OAAO,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAChE,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,CAAC,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,CAAC;AAEzE,MAAM,WAAW,eAAe,CAAC,KAAK;IAClC,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC;IACxB,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC;IAC9C,MAAM,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC;IAC9B,SAAS,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC;IACjC,cAAc,CAAC,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,GAAG,SAAS,CAAC;IAClD,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,GAAG,SAAS,CAAC;CAC9C"}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AACxE,YAAY,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mask.d.ts","sourceRoot":"","sources":["../../src/utils/mask.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,2BAA2B,CAAC;AAIxD,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,GAAG,IAAI,CAyBpF"}
|