diff --git a/src/models/openai/chat.ts b/src/models/openai/chat.ts index 95f11a9..1ce81c3 100644 --- a/src/models/openai/chat.ts +++ b/src/models/openai/chat.ts @@ -115,6 +115,9 @@ class OpenAIChatInput { * * If set to `ResponseFormat.Json`, the response will be a JSON object. * + * If set to `ResponseFormat.JsonSchema`, the response will be a JSON object + * that conforms to the provided JSON schema. + * * @default ResponseFormat.Text */ @alias("response_format") @@ -286,6 +289,8 @@ class OpenAIChatOutput { usage!: Usage; } +type JsonSchemaFunction = (jsonSchema: string) => ResponseFormat; + /** * An object specifying the format that the model must output. */ @@ -296,6 +301,13 @@ export class ResponseFormat { */ readonly type!: string; + /** + * The JSON schema to use for the response format. + */ + @omitnull() + @alias("json_schema") + readonly jsonSchema: JSON.Raw | null = null; + /** * Instructs the model to output the response as a JSON object. * @@ -307,6 +319,20 @@ export class ResponseFormat { */ static Json: ResponseFormat = { type: "json_object" }; + /** + * Enables Structured Outputs which guarantees the model will match your supplied JSON schema. + * + * See https://platform.openai.com/docs/guides/structured-outputs + */ + static JsonSchema: JsonSchemaFunction = ( + jsonSchema: string, + ): ResponseFormat => { + return { + type: "json_schema", + jsonSchema: jsonSchema, + }; + }; + /** * Instructs the model to output the response as a plain text string. * @@ -339,7 +365,7 @@ export class Tool { /** * The definition of the function. */ - function: FunctionDefinition = new FunctionDefinition(); + function!: FunctionDefinition; } /** @@ -360,13 +386,30 @@ export class FunctionDefinition { @omitnull() description: string | null = null; + /** + * Whether to enable strict schema adherence when generating the function call. + * If set to true, the model will follow the exact schema defined in the parameters field. + * + * See https://platform.openai.com/docs/guides/function-calling + * + * @remarks + * In order to guarantee strict schema adherence, disable parallel function calls + * by setting {@link OpenAIChatInput.parallelToolCalls}=false. + * + * See https://platform.openai.com/docs/guides/function-calling/parallel-function-calling-and-structured-outputs + * + * @default false + */ + @omitif("this.strict == false") + strict: bool = false; + /** * The parameters the functions accepts, described as a JSON Schema object. * * See https://platform.openai.com/docs/guides/function-calling */ @omitnull() - parameters: JSON.Raw | null = null; // TODO: verify this works + parameters: JSON.Raw | null = null; } /** @@ -681,6 +724,11 @@ class CompletionMessage extends Message { super(role, content); } + /** + * The refusal message generated by the model. + */ + refusal: string | null = null; + /** * The tool calls generated by the model, such as function calls. */