Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit ddb100d

Browse files
Add support for Structured Outputs on OpenAI (#30)
1 parent 6983ab2 commit ddb100d

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

src/models/openai/chat.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ class OpenAIChatInput {
115115
*
116116
* If set to `ResponseFormat.Json`, the response will be a JSON object.
117117
*
118+
* If set to `ResponseFormat.JsonSchema`, the response will be a JSON object
119+
* that conforms to the provided JSON schema.
120+
*
118121
* @default ResponseFormat.Text
119122
*/
120123
@alias("response_format")
@@ -286,6 +289,8 @@ class OpenAIChatOutput {
286289
usage!: Usage;
287290
}
288291

292+
type JsonSchemaFunction = (jsonSchema: string) => ResponseFormat;
293+
289294
/**
290295
* An object specifying the format that the model must output.
291296
*/
@@ -296,6 +301,13 @@ export class ResponseFormat {
296301
*/
297302
readonly type!: string;
298303

304+
/**
305+
* The JSON schema to use for the response format.
306+
*/
307+
@omitnull()
308+
@alias("json_schema")
309+
readonly jsonSchema: JSON.Raw | null = null;
310+
299311
/**
300312
* Instructs the model to output the response as a JSON object.
301313
*
@@ -307,6 +319,20 @@ export class ResponseFormat {
307319
*/
308320
static Json: ResponseFormat = { type: "json_object" };
309321

322+
/**
323+
* Enables Structured Outputs which guarantees the model will match your supplied JSON schema.
324+
*
325+
* See https://platform.openai.com/docs/guides/structured-outputs
326+
*/
327+
static JsonSchema: JsonSchemaFunction = (
328+
jsonSchema: string,
329+
): ResponseFormat => {
330+
return {
331+
type: "json_schema",
332+
jsonSchema: jsonSchema,
333+
};
334+
};
335+
310336
/**
311337
* Instructs the model to output the response as a plain text string.
312338
*
@@ -339,7 +365,7 @@ export class Tool {
339365
/**
340366
* The definition of the function.
341367
*/
342-
function: FunctionDefinition = new FunctionDefinition();
368+
function!: FunctionDefinition;
343369
}
344370

345371
/**
@@ -360,13 +386,30 @@ export class FunctionDefinition {
360386
@omitnull()
361387
description: string | null = null;
362388

389+
/**
390+
* Whether to enable strict schema adherence when generating the function call.
391+
* If set to true, the model will follow the exact schema defined in the parameters field.
392+
*
393+
* See https://platform.openai.com/docs/guides/function-calling
394+
*
395+
* @remarks
396+
* In order to guarantee strict schema adherence, disable parallel function calls
397+
* by setting {@link OpenAIChatInput.parallelToolCalls}=false.
398+
*
399+
* See https://platform.openai.com/docs/guides/function-calling/parallel-function-calling-and-structured-outputs
400+
*
401+
* @default false
402+
*/
403+
@omitif("this.strict == false")
404+
strict: bool = false;
405+
363406
/**
364407
* The parameters the functions accepts, described as a JSON Schema object.
365408
*
366409
* See https://platform.openai.com/docs/guides/function-calling
367410
*/
368411
@omitnull()
369-
parameters: JSON.Raw | null = null; // TODO: verify this works
412+
parameters: JSON.Raw | null = null;
370413
}
371414

372415
/**
@@ -681,6 +724,11 @@ class CompletionMessage extends Message {
681724
super(role, content);
682725
}
683726

727+
/**
728+
* The refusal message generated by the model.
729+
*/
730+
refusal: string | null = null;
731+
684732
/**
685733
* The tool calls generated by the model, such as function calls.
686734
*/

0 commit comments

Comments
 (0)