Skip to content

Commit

Permalink
Merge pull request dongri#46 from zh4ngx/fix_finish_details
Browse files Browse the repository at this point in the history
Update ChatCompletionChoice
  • Loading branch information
Dongri Jin authored Nov 22, 2023
2 parents 282e7cb + 6e136b7 commit 4d9f7c1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
16 changes: 10 additions & 6 deletions examples/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}])
.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 {
Expand All @@ -80,10 +84,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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");
}
}
Expand Down
14 changes: 9 additions & 5 deletions examples/function_call_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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 {
Expand Down Expand Up @@ -94,10 +98,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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");
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/v1/chat_completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ pub struct ChatCompletionMessageForResponse {
pub struct ChatCompletionChoice {
pub index: i64,
pub message: ChatCompletionMessageForResponse,
pub finish_reason: FinishReason,
pub finish_reason: Option<FinishReason>,
pub finish_details: Option<FinishDetails>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -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")]
Expand Down

0 comments on commit 4d9f7c1

Please sign in to comment.