From 01a9a956457113d89e7a90217bcdf2d6e890147c Mon Sep 17 00:00:00 2001 From: Maxuss Date: Sun, 22 Oct 2023 18:38:53 +0300 Subject: [PATCH] maybe fix function calls? --- src/client.rs | 6 +++--- src/converse.rs | 13 +++++++------ src/types.rs | 15 +++++++++++++-- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/client.rs b/src/client.rs index 7395d33..bb57d90 100644 --- a/src/client.rs +++ b/src/client.rs @@ -224,7 +224,7 @@ impl ChatGPT { role: Role::User, content: message.into(), #[cfg(feature = "functions")] - function_call: None, + function_properties: None, }], stream: false, temperature: self.config.temperature, @@ -267,7 +267,7 @@ impl ChatGPT { role: Role::User, content: message.into(), #[cfg(feature = "functions")] - function_call: None, + function_properties: None, }], stream: true, temperature: self.config.temperature, @@ -365,7 +365,7 @@ impl ChatGPT { role: Role::User, content: message.into(), #[cfg(feature = "functions")] - function_call: None, + function_properties: None, }], stream: false, temperature: self.config.temperature, diff --git a/src/converse.rs b/src/converse.rs index 78a345d..33a9351 100644 --- a/src/converse.rs +++ b/src/converse.rs @@ -18,6 +18,7 @@ use crate::{ client::ChatGPT, types::{ChatMessage, CompletionResponse, Role}, }; +use crate::types::FunctionCallProperties; /// Stores a single conversation session, and automatically saves message history pub struct Conversation { @@ -44,7 +45,7 @@ impl Conversation { role: Role::System, content: first_message, #[cfg(feature = "functions")] - function_call: None, + function_properties: None, }], #[cfg(feature = "functions")] functions: HashMap::with_capacity(4), @@ -103,7 +104,7 @@ impl Conversation { role, content: message.into(), #[cfg(feature = "functions")] - function_call: None, + function_properties: None, }); #[cfg(feature = "functions")] @@ -149,7 +150,7 @@ impl Conversation { role: Role::User, content: message.into(), #[cfg(feature = "functions")] - function_call: None, + function_properties: None, }); let resp = self .client @@ -184,7 +185,7 @@ impl Conversation { role, content: message.into(), #[cfg(feature = "functions")] - function_call: None, + function_properties: None, }); let stream = self.client.send_history_streaming(&self.history).await?; Ok(stream) @@ -244,8 +245,8 @@ impl Conversation { &mut self, message: &ChatMessage, ) -> Option { - if let Some(call) = &message.function_call { - if let Some(Ok(result)) = self.process_function(call).await { + if let Some(FunctionCallProperties { function_call, .. }) = &message.function_properties { + if let Some(Ok(result)) = self.process_function(function_call).await { Some(result) } else { None diff --git a/src/types.rs b/src/types.rs index 35c650a..9905322 100644 --- a/src/types.rs +++ b/src/types.rs @@ -29,8 +29,19 @@ pub struct ChatMessage { pub content: String, /// Function call (if present) #[cfg(feature = "functions")] + #[serde(skip_serializing_if = "Option::is_none", flatten)] + pub function_properties: Option, +} + +/// Container for called function info +#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)] +pub struct FunctionCallProperties { + /// The actual function call + pub function_call: FunctionCall, + /// Name of the function called + #[serde(rename = "name")] #[serde(skip_serializing_if = "Option::is_none")] - pub function_call: Option, + pub function_name: Option } fn deserialize_maybe_null<'de, D>(deserializer: D) -> Result @@ -63,7 +74,7 @@ impl ChatMessage { role, content: String::new(), #[cfg(feature = "functions")] - function_call: None, + function_properties: None, }; result.push(msg); }