chat_completion/
chat_completion.rs

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