Skip to content

Commit 4c82211

Browse files
authored
Update chat-completion json_schema specs (JS) (#1537)
In huggingface/huggingface_hub#3082 @hanouticelina fixed the Python inference types for chat completion grammar input. Biggest update was: ```py @dataclass_with_extra class ChatCompletionInputResponseFormatText(BaseInferenceType): type: Literal["text"] @dataclass_with_extra class ChatCompletionInputResponseFormatJSONSchema(BaseInferenceType): type: Literal["json_schema"] json_schema: ChatCompletionInputJSONSchema @dataclass_with_extra class ChatCompletionInputResponseFormatJSONObject(BaseInferenceType): type: Literal["json_object"] ChatCompletionInputGrammarType = Union[ ChatCompletionInputResponseFormatText, ChatCompletionInputResponseFormatJSONSchema, ChatCompletionInputResponseFormatJSONObject, ] ``` --- This PR reflects "officially" this update in the JS specs. It makes JS and Python specs aligned again (was not the case for the last month, ending up with annoying PRs like this one huggingface/huggingface_hub#3104). The result is not as satisfying as what a manual implementation would give because the 3 objects are merged in 1 with optional fields (see `export type ChatCompletionInputGrammarTypeType = "text" | "json_schema" | "json_object";`). This is because quicktype do not handle properly unions (see glideapps/quicktype#1266 - 6 yo :confused: ). It's not the only case like this in our specs but first time I find out about it. Not a problem since we don't enforce data structures. In Python, the data structure will result in this: huggingface/huggingface_hub#3167
1 parent 56e9f9d commit 4c82211

File tree

2 files changed

+77
-45
lines changed

2 files changed

+77
-45
lines changed

packages/tasks/src/tasks/chat-completion/inference.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,35 @@ export interface ChatCompletionInputFunctionDefinition {
136136
[property: string]: unknown;
137137
}
138138
export interface ChatCompletionInputGrammarType {
139+
json_schema?: ChatCompletionInputJSONSchemaConfig;
139140
type: ChatCompletionInputGrammarTypeType;
141+
[property: string]: unknown;
142+
}
143+
export interface ChatCompletionInputJSONSchemaConfig {
140144
/**
141-
* A string that represents a [JSON Schema](https://json-schema.org/).
142-
*
143-
* JSON Schema is a declarative language that allows to annotate JSON documents
144-
* with types and descriptions.
145+
* A description of what the response format is for, used by the model to determine how to
146+
* respond in the format.
147+
*/
148+
description?: string;
149+
/**
150+
* The name of the response format.
151+
*/
152+
name: string;
153+
/**
154+
* The schema for the response format, described as a JSON Schema object. Learn how to build
155+
* JSON schemas [here](https://json-schema.org/).
156+
*/
157+
schema?: {
158+
[key: string]: unknown;
159+
};
160+
/**
161+
* Whether to enable strict schema adherence when generating the output. If set to true, the
162+
* model will always follow the exact schema defined in the `schema` field.
145163
*/
146-
value: unknown;
164+
strict?: boolean;
147165
[property: string]: unknown;
148166
}
149-
export type ChatCompletionInputGrammarTypeType = "json" | "regex" | "json_schema";
167+
export type ChatCompletionInputGrammarTypeType = "text" | "json_schema" | "json_object";
150168
export interface ChatCompletionInputStreamOptions {
151169
/**
152170
* If set, an additional chunk will be streamed before the data: [DONE] message. The usage

packages/tasks/src/tasks/chat-completion/spec/input.json

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -291,61 +291,75 @@
291291
"ChatCompletionInputGrammarType": {
292292
"oneOf": [
293293
{
294-
"type": "object",
295-
"required": ["type", "value"],
296-
"properties": {
297-
"type": {
298-
"type": "string",
299-
"enum": ["json"]
300-
},
301-
"value": {
302-
"description": "A string that represents a [JSON Schema](https://json-schema.org/).\n\nJSON Schema is a declarative language that allows to annotate JSON documents\nwith types and descriptions."
303-
}
304-
}
294+
"$ref": "#/$defs/ChatCompletionInputResponseFormatText"
305295
},
306296
{
307-
"type": "object",
308-
"required": ["type", "value"],
309-
"properties": {
310-
"type": {
311-
"type": "string",
312-
"enum": ["regex"]
313-
},
314-
"value": {
315-
"type": "string"
316-
}
317-
}
297+
"$ref": "#/$defs/ChatCompletionInputResponseFormatJSONSchema"
318298
},
319299
{
320-
"type": "object",
321-
"required": ["type", "value"],
322-
"properties": {
323-
"type": {
324-
"type": "string",
325-
"enum": ["json_schema"]
326-
},
327-
"value": {
328-
"$ref": "#/$defs/ChatCompletionInputJsonSchemaConfig"
329-
}
330-
}
300+
"$ref": "#/$defs/ChatCompletionInputResponseFormatJSONObject"
331301
}
332302
],
333-
"discriminator": {
334-
"propertyName": "type"
335-
},
336303
"title": "ChatCompletionInputGrammarType"
337304
},
305+
"ChatCompletionInputResponseFormatText": {
306+
"type": "object",
307+
"required": ["type"],
308+
"properties": {
309+
"type": {
310+
"type": "string",
311+
"enum": ["text"]
312+
}
313+
},
314+
"title": "ChatCompletionInputResponseFormatText"
315+
},
316+
"ChatCompletionInputResponseFormatJSONSchema": {
317+
"type": "object",
318+
"required": ["type", "json_schema"],
319+
"properties": {
320+
"type": {
321+
"type": "string",
322+
"enum": ["json_schema"]
323+
},
324+
"json_schema": {
325+
"$ref": "#/$defs/ChatCompletionInputJsonSchemaConfig"
326+
}
327+
},
328+
"title": "ChatCompletionInputResponseFormatJSONSchema"
329+
},
330+
"ChatCompletionInputResponseFormatJSONObject": {
331+
"type": "object",
332+
"required": ["type"],
333+
"properties": {
334+
"type": {
335+
"type": "string",
336+
"enum": ["json_object"]
337+
}
338+
},
339+
"title": "ChatCompletionInputResponseFormatJSONObject"
340+
},
338341
"ChatCompletionInputJsonSchemaConfig": {
339342
"type": "object",
340-
"required": ["schema"],
343+
"required": ["name"],
341344
"properties": {
342345
"name": {
343346
"type": "string",
344-
"description": "Optional name identifier for the schema",
347+
"description": "The name of the response format."
348+
},
349+
"description": {
350+
"type": "string",
351+
"description": "A description of what the response format is for, used by the model to determine how to respond in the format.",
345352
"nullable": true
346353
},
347354
"schema": {
348-
"description": "The actual JSON schema definition"
355+
"type": "object",
356+
"description": "The schema for the response format, described as a JSON Schema object. Learn how to build JSON schemas [here](https://json-schema.org/).",
357+
"nullable": true
358+
},
359+
"strict": {
360+
"type": "boolean",
361+
"description": "Whether to enable strict schema adherence when generating the output. If set to true, the model will always follow the exact schema defined in the `schema` field.",
362+
"nullable": true
349363
}
350364
},
351365
"title": "ChatCompletionInputJsonSchemaConfig"

0 commit comments

Comments
 (0)