Skip to content

Commit 6cbbd8d

Browse files
committed
Support structured outputs
1 parent 6d70a33 commit 6cbbd8d

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

async-openai/src/types/assistant.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,45 @@ pub enum AssistantsApiResponseFormatOption {
129129
/// An object describing the expected output of the model. If `json_object` only `function` type `tools` are allowed to be passed to the Run. If `text` the model can return text or any value needed.
130130
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
131131
pub struct AssistantsApiResponseFormat {
132-
/// Must be one of `text` or `json_object`.
132+
/// Must be one of `text`, `json_object` or `json_schema`.
133133
pub r#type: AssistantsApiResponseFormatType,
134+
135+
#[serde(skip_serializing_if = "Option::is_none")]
136+
pub json_schema: Option<JsonSchemaObject>,
137+
}
138+
139+
140+
#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
141+
#[builder(name = "JsonSchemaObjectArgs")]
142+
#[builder(pattern = "mutable")]
143+
#[builder(setter(into, strip_option), default)]
144+
#[builder(derive(Debug))]
145+
#[builder(build_fn(error = "OpenAIError"))]
146+
pub struct JsonSchemaObject {
147+
/// The name of the JSONSchema. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
148+
pub name: String,
149+
/// A description of what the function does, used by the model to choose when and how to call the function.
150+
#[serde(skip_serializing_if = "Option::is_none")]
151+
pub description: Option<String>,
152+
/// The parameters the functions accepts, described as a JSON Schema object. See the [guide](https://platform.openai.com/docs/guides/text-generation/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.
153+
///
154+
/// Omitting `parameters` defines a function with an empty parameter list.
155+
#[serde(skip_serializing_if = "Option::is_none")]
156+
pub schema: Option<serde_json::Value>,
157+
158+
#[serde(skip_serializing_if = "Option::is_none")]
159+
pub strict: Option<bool>,
134160
}
135161

162+
163+
136164
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
137165
#[serde(rename_all = "snake_case")]
138166
pub enum AssistantsApiResponseFormatType {
139167
#[default]
140168
Text,
141169
JsonObject,
170+
JsonSchema,
142171
}
143172

144173
/// Retrieval tool

async-openai/src/types/chat.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ pub struct ChatCompletionResponseMessage {
263263
/// The role of the author of this message.
264264
pub role: Role,
265265

266+
pub refusal: Option<String>,
267+
266268
/// Deprecated and replaced by `tool_calls`.
267269
/// The name and arguments of a function that should be called, as generated by the model.
268270
#[deprecated]
@@ -312,8 +314,33 @@ pub struct FunctionObject {
312314
pub enum ChatCompletionResponseFormatType {
313315
Text,
314316
JsonObject,
317+
JsonSchema,
318+
}
319+
320+
321+
#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
322+
#[builder(name = "JsonSchemaObjectArgs")]
323+
#[builder(pattern = "mutable")]
324+
#[builder(setter(into, strip_option), default)]
325+
#[builder(derive(Debug))]
326+
#[builder(build_fn(error = "OpenAIError"))]
327+
pub struct JsonSchemaObject {
328+
/// The name of the JSONSchema. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
329+
pub name: String,
330+
/// A description of what the function does, used by the model to choose when and how to call the function.
331+
#[serde(skip_serializing_if = "Option::is_none")]
332+
pub description: Option<String>,
333+
/// The parameters the functions accepts, described as a JSON Schema object. See the [guide](https://platform.openai.com/docs/guides/text-generation/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.
334+
///
335+
/// Omitting `parameters` defines a function with an empty parameter list.
336+
#[serde(skip_serializing_if = "Option::is_none")]
337+
pub schema: Option<serde_json::Value>,
338+
339+
#[serde(skip_serializing_if = "Option::is_none")]
340+
pub strict: Option<bool>,
315341
}
316342

343+
317344
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
318345
pub struct ChatCompletionResponseFormat {
319346
/// Setting to `json_object` enables JSON mode. This guarantees that the message the model generates is valid JSON.
@@ -323,8 +350,11 @@ pub struct ChatCompletionResponseFormat {
323350
/// content may be partial (i.e. cut off) if `finish_reason="length"`, which indicates the generation
324351
/// exceeded `max_tokens` or the conversation exceeded the max context length.
325352
///
326-
/// Must be one of `text` or `json_object`.
353+
/// Must be one of `text`, `json_schema` or `json_object`.
327354
pub r#type: ChatCompletionResponseFormatType,
355+
356+
#[serde(skip_serializing_if = "Option::is_none")]
357+
pub json_schema: Option<JsonSchemaObject>,
328358
}
329359

330360
#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]

0 commit comments

Comments
 (0)