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

Add response_format to ChatCompletionRequest #33

Merged
merged 1 commit into from
Aug 27, 2024
Merged
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: 28 additions & 0 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,34 @@ pub struct ChatCompletionRequest {
#[builder(default)]
#[serde(skip_serializing_if = "Option::is_none")]
function_call: Option<Value>,

/// An object specifying the format that the model must output. Compatible with GPT-4 Turbo and all GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106.
/// Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is valid JSON.
/// Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if finish_reason="length", which indicates the generation exceeded max_tokens or the conversation exceeded the max context length.
#[builder(default)]
#[serde(skip_serializing_if = "Option::is_none")]
response_format: Option<ChatCompletionResponseFormat>,
}

#[derive(Serialize, Debug, Clone)]
pub struct ChatCompletionResponseFormat {
/// Must be one of text or json_object (defaults to text)
#[serde(rename = "type")]
typ: String,
}

impl ChatCompletionResponseFormat {
pub fn json_object() -> Self {
ChatCompletionResponseFormat {
typ: "json_object".to_string(),
}
}

pub fn text() -> Self {
ChatCompletionResponseFormat {
typ: "text".to_string(),
}
}
}

impl<C> ChatCompletionGeneric<C> {
Expand Down
Loading