Skip to content

Commit

Permalink
Support structured outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Aug 13, 2024
1 parent 6d70a33 commit b02f5c0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
31 changes: 30 additions & 1 deletion async-openai/src/types/assistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,45 @@ 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<JsonSchemaObject>,
}


#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
#[builder(name = "JsonSchemaObjectArgs")]
#[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)]
#[serde(rename_all = "snake_case")]
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 = "JsonSchemaObjectArgs")]
#[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<JsonSchemaObject>,
}

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

0 comments on commit b02f5c0

Please sign in to comment.