chat_completion_stream/
chat_completion_stream.rs1use futures_util::StreamExt;
2use openai_api_rs::v1::api::OpenAIClient;
3use openai_api_rs::v1::chat_completion::chat_completion_stream::{
4 ChatCompletionStreamRequest, ChatCompletionStreamResponse,
5};
6use openai_api_rs::v1::chat_completion::{self};
7use openai_api_rs::v1::common::GPT4_O_MINI;
8use std::env;
9
10#[tokio::main]
11async fn main() -> Result<(), Box<dyn std::error::Error>> {
12 let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
13 let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
14
15 let req = ChatCompletionStreamRequest::new(
16 GPT4_O_MINI.to_string(),
17 vec![chat_completion::ChatCompletionMessage {
18 role: chat_completion::MessageRole::user,
19 content: chat_completion::Content::Text(String::from("What is bitcoin?")),
20 name: None,
21 tool_calls: None,
22 tool_call_id: None,
23 }],
24 );
25
26 let mut result = client.chat_completion_stream(req).await?;
27 while let Some(response) = result.next().await {
28 match response.clone() {
29 ChatCompletionStreamResponse::ToolCall(toolcalls) => {
30 println!("Tool Call: {:?}", toolcalls);
31 }
32 ChatCompletionStreamResponse::Content(content) => {
33 println!("Content: {:?}", content);
34 }
35 ChatCompletionStreamResponse::Done => {
36 println!("Done");
37 }
38 }
39 }
40
41 Ok(())
42}
43
44