Skip to content

Commit

Permalink
Merge pull request #36 from dongri/fix-function-call-enum
Browse files Browse the repository at this point in the history
Fix function call type
  • Loading branch information
Dongri Jin committed Oct 6, 2023
2 parents b079334 + 6431c9c commit 1651062
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
6 changes: 5 additions & 1 deletion examples/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
required: Some(vec![String::from("coin")]),
},
}]),
function_call: Some(FunctionCallType::auto), //Some(FunctionCallType::Function { name: "test".to_string() })
function_call: Some(FunctionCallType::Auto), // Some(FunctionCallType::Function { name: "test".to_string() }),
temperature: None,
top_p: None,
n: None,
Expand All @@ -59,6 +59,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
user: None,
};

// debug reuqest json
// let serialized = serde_json::to_string(&req).unwrap();
// println!("{}", serialized);

let result = client.chat_completion(req)?;

match result.choices[0].finish_reason {
Expand Down
30 changes: 25 additions & 5 deletions src/v1/chat_completion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use serde::ser::SerializeMap;
use serde::{Deserialize, Serialize, Serializer};
use std::collections::HashMap;

use crate::v1::common;
Expand All @@ -14,11 +15,10 @@ pub const GPT4_32K_0314: &str = "gpt-4-32k-0314";
pub const GPT4_0613: &str = "gpt-4-0613";

#[derive(Debug, Serialize)]
#[allow(non_camel_case_types)]
pub enum FunctionCallType {
none,
auto,
function { name: String },
None,
Auto,
Function { name: String },
}

#[derive(Debug, Serialize)]
Expand All @@ -28,6 +28,7 @@ pub struct ChatCompletionRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub functions: Option<Vec<Function>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(serialize_with = "serialize_function_call")]
pub function_call: Option<FunctionCallType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f64>,
Expand Down Expand Up @@ -160,3 +161,22 @@ pub struct FunctionCall {
#[serde(skip_serializing_if = "Option::is_none")]
pub arguments: Option<String>,
}

fn serialize_function_call<S>(
value: &Option<FunctionCallType>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match value {
Some(FunctionCallType::None) => serializer.serialize_str("none"),
Some(FunctionCallType::Auto) => serializer.serialize_str("auto"),
Some(FunctionCallType::Function { name }) => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("name", name)?;
map.end()
}
None => serializer.serialize_none(),
}
}

0 comments on commit 1651062

Please sign in to comment.