Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support structured outputs #255

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion async-openai/src/types/assistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,33 @@ pub enum AssistantsApiResponseFormatOption {
/// 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.
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
pub struct AssistantsApiResponseFormat {
/// Must be one of `text` or `json_object`.
/// Must be one of `text`, `json_object` or `json_schema`.
pub r#type: AssistantsApiResponseFormatType,

#[serde(skip_serializing_if = "Option::is_none")]
pub json_schema: Option<AssistantJsonSchemaObject>,
}

#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
#[builder(name = "AssistantJsonSchemaObjectArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
pub struct AssistantJsonSchemaObject {
/// The name of the JSONSchema. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
pub name: String,
/// A description of what the function does, used by the model to choose when and how to call the function.
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// 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.
///
/// Omitting `parameters` defines a function with an empty parameter list.
#[serde(skip_serializing_if = "Option::is_none")]
pub schema: Option<serde_json::Value>,

#[serde(skip_serializing_if = "Option::is_none")]
pub strict: Option<bool>,
}

#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
Expand All @@ -139,6 +164,7 @@ pub enum AssistantsApiResponseFormatType {
#[default]
Text,
JsonObject,
JsonSchema,
}

/// Retrieval tool
Expand Down
32 changes: 31 additions & 1 deletion async-openai/src/types/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ pub struct ChatCompletionResponseMessage {
/// The role of the author of this message.
pub role: Role,

pub refusal: Option<String>,

/// Deprecated and replaced by `tool_calls`.
/// The name and arguments of a function that should be called, as generated by the model.
#[deprecated]
Expand Down Expand Up @@ -312,8 +314,33 @@ pub struct FunctionObject {
pub enum ChatCompletionResponseFormatType {
Text,
JsonObject,
JsonSchema,
}


#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
#[builder(name = "ChatJsonSchemaObjectArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
pub struct ChatJsonSchemaObject {
/// The name of the JSONSchema. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
pub name: String,
/// A description of what the function does, used by the model to choose when and how to call the function.
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// 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.
///
/// Omitting `parameters` defines a function with an empty parameter list.
#[serde(skip_serializing_if = "Option::is_none")]
pub schema: Option<serde_json::Value>,

#[serde(skip_serializing_if = "Option::is_none")]
pub strict: Option<bool>,
}


#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub struct ChatCompletionResponseFormat {
/// Setting to `json_object` enables JSON mode. This guarantees that the message the model generates is valid JSON.
Expand All @@ -323,8 +350,11 @@ pub struct ChatCompletionResponseFormat {
/// content may be partial (i.e. cut off) if `finish_reason="length"`, which indicates the generation
/// exceeded `max_tokens` or the conversation exceeded the max context length.
///
/// Must be one of `text` or `json_object`.
/// Must be one of `text`, `json_schema` or `json_object`.
pub r#type: ChatCompletionResponseFormatType,

#[serde(skip_serializing_if = "Option::is_none")]
pub json_schema: Option<ChatJsonSchemaObject>,
}

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