mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-08-22 23:25:39 +00:00
Fix Deprecated
This commit is contained in:
@ -33,16 +33,18 @@ 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")]),
|
||||
},
|
||||
},
|
||||
}]);
|
||||
|
||||
@ -60,40 +62,44 @@ 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 arguments = function_call.arguments.clone().unwrap();
|
||||
let c: Currency = serde_json::from_str(&arguments)?;
|
||||
let coin = c.coin;
|
||||
let tool_calls = result.choices[0].message.tool_calls.as_ref().unwrap();
|
||||
for tool_call in tool_calls {
|
||||
let function_call = &tool_call.function;
|
||||
let arguments = function_call.arguments.clone().unwrap();
|
||||
let c: Currency = serde_json::from_str(&arguments)?;
|
||||
let coin = c.coin;
|
||||
println!("coin: {}", coin);
|
||||
let price = get_coin_price(&coin);
|
||||
println!("price: {}", price);
|
||||
|
||||
let req = ChatCompletionRequest::new(
|
||||
GPT3_5_TURBO_0613.to_string(),
|
||||
vec![
|
||||
chat_completion::ChatCompletionMessage {
|
||||
role: chat_completion::MessageRole::user,
|
||||
content: String::from("What is the price of Ethereum?"),
|
||||
name: None,
|
||||
function_call: None,
|
||||
},
|
||||
chat_completion::ChatCompletionMessage {
|
||||
role: chat_completion::MessageRole::function,
|
||||
content: {
|
||||
let price = get_coin_price(&coin);
|
||||
format!("{{\"price\": {}}}", price)
|
||||
let req = ChatCompletionRequest::new(
|
||||
GPT3_5_TURBO_0613.to_string(),
|
||||
vec![
|
||||
chat_completion::ChatCompletionMessage {
|
||||
role: chat_completion::MessageRole::user,
|
||||
content: String::from("What is the price of Ethereum?"),
|
||||
name: None,
|
||||
},
|
||||
name: Some(String::from("get_coin_price")),
|
||||
function_call: None,
|
||||
},
|
||||
],
|
||||
);
|
||||
chat_completion::ChatCompletionMessage {
|
||||
role: chat_completion::MessageRole::function,
|
||||
content: {
|
||||
let price = get_coin_price(&coin);
|
||||
format!("{{\"price\": {}}}", price)
|
||||
},
|
||||
name: Some(String::from("get_coin_price")),
|
||||
},
|
||||
],
|
||||
);
|
||||
|
||||
let result = client.chat_completion(req)?;
|
||||
println!("{:?}", result.choices[0].message.content);
|
||||
let result = client.chat_completion(req)?;
|
||||
println!("{:?}", result.choices[0].message.content);
|
||||
}
|
||||
}
|
||||
Some(chat_completion::FinishReason::content_filter) => {
|
||||
println!("ContentFilter");
|
||||
|
Reference in New Issue
Block a user