mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-08-22 23:25:39 +00:00
chore: update examples
This commit is contained in:
@ -2,8 +2,7 @@ use openai_api_rs::v1::api::Client;
|
|||||||
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
|
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
#[tokio::main]
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
||||||
let req = ChatCompletionRequest {
|
let req = ChatCompletionRequest {
|
||||||
model: chat_completion::GPT4.to_string(),
|
model: chat_completion::GPT4.to_string(),
|
||||||
@ -26,7 +25,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
logit_bias: None,
|
logit_bias: None,
|
||||||
user: None,
|
user: None,
|
||||||
};
|
};
|
||||||
let result = client.chat_completion(req).await?;
|
let result = client.chat_completion(req)?;
|
||||||
println!("{:?}", result.choices[0].message.content);
|
println!("{:?}", result.choices[0].message.content);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,7 @@ use openai_api_rs::v1::api::Client;
|
|||||||
use openai_api_rs::v1::completion::{self, CompletionRequest};
|
use openai_api_rs::v1::completion::{self, CompletionRequest};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
#[tokio::main]
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
||||||
let req = CompletionRequest {
|
let req = CompletionRequest {
|
||||||
model: completion::GPT3_TEXT_DAVINCI_003.to_string(),
|
model: completion::GPT3_TEXT_DAVINCI_003.to_string(),
|
||||||
@ -23,7 +22,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
logit_bias: None,
|
logit_bias: None,
|
||||||
user: None,
|
user: None,
|
||||||
};
|
};
|
||||||
let result = client.completion(req).await?;
|
let result = client.completion(req)?;
|
||||||
println!("{:}", result.choices[0].text);
|
println!("{:}", result.choices[0].text);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -2,15 +2,14 @@ use openai_api_rs::v1::api::Client;
|
|||||||
use openai_api_rs::v1::embedding::EmbeddingRequest;
|
use openai_api_rs::v1::embedding::EmbeddingRequest;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
#[tokio::main]
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
||||||
let req = EmbeddingRequest {
|
let req = EmbeddingRequest {
|
||||||
model: "text-embedding-ada-002".to_string(),
|
model: "text-embedding-ada-002".to_string(),
|
||||||
input: "story time".to_string(),
|
input: "story time".to_string(),
|
||||||
user: Option::None,
|
user: Option::None,
|
||||||
};
|
};
|
||||||
let result = client.embedding(req).await?;
|
let result = client.embedding(req)?;
|
||||||
println!("{:?}", result.data);
|
println!("{:?}", result.data);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::{env, vec};
|
use std::{env, vec};
|
||||||
|
|
||||||
async fn get_coin_price(coin: &str) -> f64 {
|
fn get_coin_price(coin: &str) -> f64 {
|
||||||
let coin = coin.to_lowercase();
|
let coin = coin.to_lowercase();
|
||||||
match coin.as_str() {
|
match coin.as_str() {
|
||||||
"btc" | "bitcoin" => 10000.0,
|
"btc" | "bitcoin" => 10000.0,
|
||||||
@ -13,8 +13,7 @@ async fn get_coin_price(coin: &str) -> f64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
||||||
|
|
||||||
let mut properties = HashMap::new();
|
let mut properties = HashMap::new();
|
||||||
@ -60,7 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
user: None,
|
user: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = client.chat_completion(req).await?;
|
let result = client.chat_completion(req)?;
|
||||||
|
|
||||||
match result.choices[0].finish_reason {
|
match result.choices[0].finish_reason {
|
||||||
chat_completion::FinishReason::stop => {
|
chat_completion::FinishReason::stop => {
|
||||||
@ -82,7 +81,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let c: Currency = serde_json::from_str(&arguments)?;
|
let c: Currency = serde_json::from_str(&arguments)?;
|
||||||
let coin = c.coin;
|
let coin = c.coin;
|
||||||
if name == "get_coin_price" {
|
if name == "get_coin_price" {
|
||||||
let price = get_coin_price(&coin).await;
|
let price = get_coin_price(&coin);
|
||||||
println!("{} price: {}", coin, price);
|
println!("{} price: {}", coin, price);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::{env, vec};
|
use std::{env, vec};
|
||||||
|
|
||||||
async fn get_coin_price(coin: &str) -> f64 {
|
fn get_coin_price(coin: &str) -> f64 {
|
||||||
let coin = coin.to_lowercase();
|
let coin = coin.to_lowercase();
|
||||||
match coin.as_str() {
|
match coin.as_str() {
|
||||||
"btc" | "bitcoin" => 10000.0,
|
"btc" | "bitcoin" => 10000.0,
|
||||||
@ -13,8 +13,7 @@ async fn get_coin_price(coin: &str) -> f64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
||||||
|
|
||||||
let mut properties = HashMap::new();
|
let mut properties = HashMap::new();
|
||||||
@ -60,7 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
user: None,
|
user: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = client.chat_completion(req).await?;
|
let result = client.chat_completion(req)?;
|
||||||
|
|
||||||
match result.choices[0].finish_reason {
|
match result.choices[0].finish_reason {
|
||||||
chat_completion::FinishReason::stop => {
|
chat_completion::FinishReason::stop => {
|
||||||
@ -93,7 +92,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
chat_completion::ChatCompletionMessage {
|
chat_completion::ChatCompletionMessage {
|
||||||
role: chat_completion::MessageRole::function,
|
role: chat_completion::MessageRole::function,
|
||||||
content: {
|
content: {
|
||||||
let price = get_coin_price(&coin).await;
|
let price = get_coin_price(&coin);
|
||||||
format!("{{\"price\": {}}}", price)
|
format!("{{\"price\": {}}}", price)
|
||||||
},
|
},
|
||||||
name: Some(String::from("get_coin_price")),
|
name: Some(String::from("get_coin_price")),
|
||||||
@ -113,7 +112,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
logit_bias: None,
|
logit_bias: None,
|
||||||
user: None,
|
user: None,
|
||||||
};
|
};
|
||||||
let result = client.chat_completion(req).await?;
|
let result = client.chat_completion(req)?;
|
||||||
println!("{:?}", result.choices[0].message.content);
|
println!("{:?}", result.choices[0].message.content);
|
||||||
}
|
}
|
||||||
chat_completion::FinishReason::content_filter => {
|
chat_completion::FinishReason::content_filter => {
|
||||||
|
Reference in New Issue
Block a user