From 6e136b728144884cfe15c18489fc1695789910a1 Mon Sep 17 00:00:00 2001 From: Andy Zhang <1329212+zhangbanger@users.noreply.github.com> Date: Tue, 21 Nov 2023 13:06:20 -0800 Subject: [PATCH] Update ChatCompletionChoice with optional finish_reason and finish_details --- examples/function_call.rs | 16 ++++++++++------ examples/function_call_role.rs | 14 +++++++++----- src/v1/chat_completion.rs | 10 +++++++++- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/examples/function_call.rs b/examples/function_call.rs index ed83bb0..7cba333 100644 --- a/examples/function_call.rs +++ b/examples/function_call.rs @@ -50,21 +50,25 @@ fn main() -> Result<(), Box> { }]) .function_call(FunctionCallType::Auto); - // debug reuqest json + // debug request json // let serialized = serde_json::to_string(&req).unwrap(); // println!("{}", serialized); let result = client.chat_completion(req)?; match result.choices[0].finish_reason { - chat_completion::FinishReason::stop => { + None => { + println!("No finish_reason"); + println!("{:?}", result.choices[0].message.content); + } + Some(chat_completion::FinishReason::stop) => { println!("Stop"); println!("{:?}", result.choices[0].message.content); } - chat_completion::FinishReason::length => { + Some(chat_completion::FinishReason::length) => { println!("Length"); } - chat_completion::FinishReason::function_call => { + Some(chat_completion::FinishReason::function_call) => { println!("FunctionCall"); #[derive(Serialize, Deserialize)] struct Currency { @@ -80,10 +84,10 @@ fn main() -> Result<(), Box> { println!("{} price: {}", coin, price); } } - chat_completion::FinishReason::content_filter => { + Some(chat_completion::FinishReason::content_filter) => { println!("ContentFilter"); } - chat_completion::FinishReason::null => { + Some(chat_completion::FinishReason::null) => { println!("Null"); } } diff --git a/examples/function_call_role.rs b/examples/function_call_role.rs index eeb2489..a842814 100644 --- a/examples/function_call_role.rs +++ b/examples/function_call_role.rs @@ -52,14 +52,18 @@ fn main() -> Result<(), Box> { let result = client.chat_completion(req)?; match result.choices[0].finish_reason { - chat_completion::FinishReason::stop => { + None => { + println!("No finish_reason"); + println!("{:?}", result.choices[0].message.content); + } + Some(chat_completion::FinishReason::stop) => { println!("Stop"); println!("{:?}", result.choices[0].message.content); } - chat_completion::FinishReason::length => { + Some(chat_completion::FinishReason::length) => { println!("Length"); } - chat_completion::FinishReason::function_call => { + Some(chat_completion::FinishReason::function_call) => { println!("FunctionCall"); #[derive(Serialize, Deserialize)] struct Currency { @@ -94,10 +98,10 @@ fn main() -> Result<(), Box> { let result = client.chat_completion(req)?; println!("{:?}", result.choices[0].message.content); } - chat_completion::FinishReason::content_filter => { + Some(chat_completion::FinishReason::content_filter) => { println!("ContentFilter"); } - chat_completion::FinishReason::null => { + Some(chat_completion::FinishReason::null) => { println!("Null"); } } diff --git a/src/v1/chat_completion.rs b/src/v1/chat_completion.rs index b3d1708..909bada 100644 --- a/src/v1/chat_completion.rs +++ b/src/v1/chat_completion.rs @@ -119,7 +119,8 @@ pub struct ChatCompletionMessageForResponse { pub struct ChatCompletionChoice { pub index: i64, pub message: ChatCompletionMessageForResponse, - pub finish_reason: FinishReason, + pub finish_reason: Option, + pub finish_details: Option, } #[derive(Debug, Deserialize)] @@ -187,6 +188,13 @@ pub enum FinishReason { null, } +#[derive(Debug, Serialize, Deserialize)] +#[allow(non_camel_case_types)] +pub struct FinishDetails { + pub r#type: FinishReason, + pub stop: String, +} + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct FunctionCall { #[serde(skip_serializing_if = "Option::is_none")]