feat(chat): add response_format for JSON return values

This commit is contained in:
Nick Anderson
2024-05-14 22:20:41 -05:00
committed by Ivan Gabriele
parent da5fe54115
commit 85c3611afb

View File

@@ -38,6 +38,20 @@ pub enum ChatMessageRole {
User, User,
} }
#[derive(Debug, Serialize, Deserialize)]
pub struct ResponseFormat {
#[serde(rename = "type")]
pub type_: String,
}
impl ResponseFormat {
pub fn json_object() -> Self {
Self {
type_: "json_object".to_string(),
}
}
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Request // Request
@@ -50,6 +64,7 @@ pub struct ChatParams {
pub tool_choice: Option<tool::ToolChoice>, pub tool_choice: Option<tool::ToolChoice>,
pub tools: Option<Vec<tool::Tool>>, pub tools: Option<Vec<tool::Tool>>,
pub top_p: Option<f32>, pub top_p: Option<f32>,
pub response_format: Option<ResponseFormat>,
} }
impl Default for ChatParams { impl Default for ChatParams {
fn default() -> Self { fn default() -> Self {
@@ -61,6 +76,21 @@ impl Default for ChatParams {
tool_choice: None, tool_choice: None,
tools: None, tools: None,
top_p: None, top_p: None,
response_format: None,
}
}
}
impl ChatParams {
pub fn json_default() -> Self {
Self {
max_tokens: None,
random_seed: None,
safe_prompt: None,
temperature: None,
tool_choice: None,
tools: None,
top_p: None,
response_format: Some(ResponseFormat::json_object()),
} }
} }
} }
@@ -87,8 +117,8 @@ pub struct ChatRequest {
pub top_p: Option<f32>, pub top_p: Option<f32>,
// TODO Check this prop (seen in official Python client but not in API doc). // TODO Check this prop (seen in official Python client but not in API doc).
// pub tool_choice: Option<String>, // pub tool_choice: Option<String>,
// TODO Check this prop (seen in official Python client but not in API doc). #[serde(skip_serializing_if = "Option::is_none")]
// pub response_format: Option<String>, pub response_format: Option<ResponseFormat>,
} }
impl ChatRequest { impl ChatRequest {
pub fn new( pub fn new(
@@ -105,6 +135,7 @@ impl ChatRequest {
tool_choice, tool_choice,
tools, tools,
top_p, top_p,
response_format,
} = options.unwrap_or_default(); } = options.unwrap_or_default();
Self { Self {
@@ -119,6 +150,7 @@ impl ChatRequest {
tool_choice, tool_choice,
tools, tools,
top_p, top_p,
response_format,
} }
} }
} }