chat_completion/
chat_completion.rs

1use openai_api_rs::v1::api::OpenAIClient;
2use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
3use openai_api_rs::v1::common::GPT4_O_MINI;
4use std::env;
5
6#[tokio::main]
7async fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
9    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
10
11    let req = ChatCompletionRequest::new(
12        GPT4_O_MINI.to_string(),
13        vec![chat_completion::ChatCompletionMessage {
14            role: chat_completion::MessageRole::user,
15            content: chat_completion::Content::Text(String::from("What is bitcoin?")),
16            name: None,
17            tool_calls: None,
18            tool_call_id: None,
19        }],
20    );
21
22    let result = client.chat_completion(req).await?;
23    println!("Content: {:?}", result.choices[0].message.content);
24
25    // print response headers
26    for (key, value) in client.response_headers.unwrap().iter() {
27        println!("{}: {:?}", key, value);
28    }
29
30    Ok(())
31}
32
33// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example chat_completion