mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-08-22 15:15:34 +00:00
Add chat gpt api
This commit is contained in:
21
examples/chat_completion.rs
Normal file
21
examples/chat_completion.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use openai_api_rs::v1::api::Client;
|
||||
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
|
||||
use std::env;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
||||
let req = ChatCompletionRequest {
|
||||
model: chat_completion::GPT3_5_TURBO.to_string(),
|
||||
messages: vec![chat_completion::ChatCompletionMessage {
|
||||
role: chat_completion::MessageRole::user,
|
||||
content: String::from("NFTとは?"),
|
||||
}],
|
||||
};
|
||||
let result = client.chat_completion(req).await?;
|
||||
println!("{:?}", result.choices[0].message.content);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example chat_completion
|
Reference in New Issue
Block a user