Fix Deprecated

This commit is contained in:
Dongri Jin
2024-01-07 07:39:36 +09:00
parent f0973eaf5b
commit 5d7d335c74
4 changed files with 75 additions and 104 deletions

View File

@ -1,5 +1,5 @@
use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest, FunctionCallType};
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
use openai_api_rs::v1::common::GPT3_5_TURBO_0613;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
@ -33,19 +33,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
role: chat_completion::MessageRole::user,
content: String::from("What is the price of Ethereum?"),
name: None,
function_call: None,
}],
)
.functions(vec![chat_completion::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: chat_completion::FunctionParameters {
schema_type: chat_completion::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
.tools(vec![chat_completion::Tool {
r#type: chat_completion::ToolType::Function,
function: chat_completion::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: chat_completion::FunctionParameters {
schema_type: chat_completion::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
},
},
}])
.function_call(FunctionCallType::Auto);
.tool_choice(chat_completion::ToolChoiceType::Auto);
// debug request json
// let serialized = serde_json::to_string(&req).unwrap();
@ -65,20 +67,22 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Some(chat_completion::FinishReason::length) => {
println!("Length");
}
Some(chat_completion::FinishReason::function_call) => {
println!("FunctionCall");
Some(chat_completion::FinishReason::tool_calls) => {
println!("ToolCalls");
#[derive(Serialize, Deserialize)]
struct Currency {
coin: String,
}
let function_call = result.choices[0].message.function_call.as_ref().unwrap();
let name = function_call.name.clone().unwrap();
let arguments = function_call.arguments.clone().unwrap();
let c: Currency = serde_json::from_str(&arguments)?;
let coin = c.coin;
if name == "get_coin_price" {
let price = get_coin_price(&coin);
println!("{} price: {}", coin, price);
let tool_calls = result.choices[0].message.tool_calls.as_ref().unwrap();
for tool_call in tool_calls {
let name = tool_call.function.name.clone().unwrap();
let arguments = tool_call.function.arguments.clone().unwrap();
let c: Currency = serde_json::from_str(&arguments)?;
let coin = c.coin;
if name == "get_coin_price" {
let price = get_coin_price(&coin);
println!("{} price: {}", coin, price);
}
}
}
Some(chat_completion::FinishReason::content_filter) => {