From 589871c0559e01e5e1d31a5c61617dd6914be491 Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Wed, 27 Dec 2023 00:50:07 -0500 Subject: [PATCH 1/4] Deprecate `functions` and `function_call` --- src/v1/chat_completion.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/v1/chat_completion.rs b/src/v1/chat_completion.rs index f66c1e6..b6c2eda 100644 --- a/src/v1/chat_completion.rs +++ b/src/v1/chat_completion.rs @@ -18,9 +18,17 @@ pub struct ChatCompletionRequest { pub model: String, pub messages: Vec, #[serde(skip_serializing_if = "Option::is_none")] + #[deprecated( + since = "2.1.5", + note = "This field is deprecated. Use `tools` instead." + )] pub functions: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[serde(serialize_with = "serialize_function_call")] + #[deprecated( + since = "2.1.5", + note = "This field is deprecated. Use `tool_choice` instead." + )] pub function_call: Option, #[serde(skip_serializing_if = "Option::is_none")] pub temperature: Option, From 9d6733f98196935fe13737c39fe839186af56d2c Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Wed, 27 Dec 2023 01:14:41 -0500 Subject: [PATCH 2/4] Add `tools` and `tool_choice` fields --- src/v1/chat_completion.rs | 45 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/v1/chat_completion.rs b/src/v1/chat_completion.rs index b6c2eda..49af052 100644 --- a/src/v1/chat_completion.rs +++ b/src/v1/chat_completion.rs @@ -13,6 +13,13 @@ pub enum FunctionCallType { Function { name: String }, } +#[derive(Debug, Serialize, Clone)] +pub enum ToolChoiceType { + None, + Auto, + ToolChoice { tool: Tool }, +} + #[derive(Debug, Serialize, Clone)] pub struct ChatCompletionRequest { pub model: String, @@ -54,6 +61,11 @@ pub struct ChatCompletionRequest { pub user: Option, #[serde(skip_serializing_if = "Option::is_none")] pub seed: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub tools: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(serialize_with = "serialize_tool_choice")] + pub tool_choice: Option, } impl ChatCompletionRequest { @@ -75,6 +87,8 @@ impl ChatCompletionRequest { logit_bias: None, user: None, seed: None, + tools: None, + tool_choice: None, } } } @@ -94,7 +108,9 @@ impl_builder_methods!( frequency_penalty: f64, logit_bias: HashMap, user: String, - seed: i64 + seed: i64, + tools: Tool, + tool_choice: ToolChoiceType ); #[derive(Debug, Serialize, Deserialize, Clone)] @@ -233,3 +249,30 @@ where None => serializer.serialize_none(), } } + +fn serialize_tool_choice( + value: &Option, + serializer: S, +) -> Result +where + S: Serializer, +{ + match value { + Some(ToolChoiceType::None) => serializer.serialize_str("none"), + Some(ToolChoiceType::Auto) => serializer.serialize_str("auto"), + Some(ToolChoiceType::ToolChoice { tool }) => { + let mut map = serializer.serialize_map(Some(2))?; + map.serialize_entry("type", &tool.tool_type)?; + map.serialize_entry("function", &tool.function)?; + map.end() + } + None => serializer.serialize_none(), + } +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Tool { + #[serde(rename = "type")] + tool_type: String, + function: Function, +} From 297f2a13d68bc8a59d9ec4a32480ff0a0adc51a1 Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Wed, 27 Dec 2023 01:27:50 -0500 Subject: [PATCH 3/4] Fix type of `tools` --- src/v1/chat_completion.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/v1/chat_completion.rs b/src/v1/chat_completion.rs index 49af052..add3e6c 100644 --- a/src/v1/chat_completion.rs +++ b/src/v1/chat_completion.rs @@ -62,7 +62,7 @@ pub struct ChatCompletionRequest { #[serde(skip_serializing_if = "Option::is_none")] pub seed: Option, #[serde(skip_serializing_if = "Option::is_none")] - pub tools: Option, + pub tools: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[serde(serialize_with = "serialize_tool_choice")] pub tool_choice: Option, From 4afe1ffb0f9a2d6830387d93ed4d80e273a44a29 Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Wed, 27 Dec 2023 01:32:28 -0500 Subject: [PATCH 4/4] Fix `tools` type (again) --- src/v1/chat_completion.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/v1/chat_completion.rs b/src/v1/chat_completion.rs index add3e6c..8ae275a 100644 --- a/src/v1/chat_completion.rs +++ b/src/v1/chat_completion.rs @@ -109,7 +109,7 @@ impl_builder_methods!( logit_bias: HashMap, user: String, seed: i64, - tools: Tool, + tools: Vec, tool_choice: ToolChoiceType );