chat_streaming/
chat_streaming.rs

1#![allow(clippy::uninlined_format_args)]
2//! Comprehensive chat streaming example demonstrating real-time response generation.
3//!
4//! This example demonstrates:
5//! - Basic streaming with `send_chat_stream()`
6//! - Processing chunks in real-time
7//! - Collecting full response from stream
8//! - Handling streaming errors
9//! - Streaming with different parameters
10//! - Streaming with tool calls (basic)
11//!
12//! Run with: `cargo run --example chat_streaming`
13
14use futures::StreamExt;
15use openai_ergonomic::{Client, Result};
16
17#[tokio::main]
18async fn main() -> Result<()> {
19    println!("=== Chat Streaming Examples ===\n");
20
21    // Initialize client
22    let client = Client::from_env()?.build();
23
24    // Example 1: Basic streaming
25    println!("1. Basic Streaming:");
26    basic_streaming(&client).await?;
27
28    // Example 2: Streaming with parameters
29    println!("\n2. Streaming with Parameters:");
30    streaming_with_parameters(&client).await?;
31
32    // Example 3: Collect full content
33    println!("\n3. Collect Full Content:");
34    collect_content(&client).await?;
35
36    // Example 4: Stream with system message
37    println!("\n4. Stream with System Message:");
38    streaming_with_system(&client).await?;
39
40    // Example 5: Multiple user turns
41    println!("\n5. Multiple User Turns:");
42    multiple_turns(&client).await?;
43
44    println!("\n=== All examples completed successfully ===");
45
46    Ok(())
47}
48
49async fn basic_streaming(client: &Client) -> Result<()> {
50    println!("Question: Tell me a short joke");
51
52    let builder = client.chat().user("Tell me a short joke");
53
54    let mut stream = client.send_chat_stream(builder).await?;
55
56    print!("Response: ");
57    while let Some(chunk) = stream.next().await {
58        let chunk = chunk?;
59        if let Some(content) = chunk.content() {
60            print!("{}", content);
61        }
62    }
63    println!();
64
65    Ok(())
66}
67
68async fn streaming_with_parameters(client: &Client) -> Result<()> {
69    println!("Question: Write a creative tagline for a bakery");
70
71    let builder = client
72        .chat()
73        .user("Write a creative tagline for a bakery")
74        .temperature(0.9)
75        .max_tokens(50);
76
77    let mut stream = client.send_chat_stream(builder).await?;
78
79    print!("Response: ");
80    while let Some(chunk) = stream.next().await {
81        let chunk = chunk?;
82        if let Some(content) = chunk.content() {
83            print!("{}", content);
84        }
85    }
86    println!();
87
88    Ok(())
89}
90
91async fn collect_content(client: &Client) -> Result<()> {
92    println!("Question: What is the capital of France?");
93
94    let builder = client.chat().user("What is the capital of France?");
95
96    let mut stream = client.send_chat_stream(builder).await?;
97
98    // Manually collect all content
99    let mut content = String::new();
100    while let Some(chunk) = stream.next().await {
101        let chunk = chunk?;
102        if let Some(text) = chunk.content() {
103            content.push_str(text);
104        }
105    }
106    println!("Full response: {}", content);
107
108    Ok(())
109}
110
111async fn streaming_with_system(client: &Client) -> Result<()> {
112    println!("System: You are a helpful assistant that speaks like a pirate");
113    println!("Question: Tell me about the weather");
114
115    let builder = client
116        .chat()
117        .system("You are a helpful assistant that speaks like a pirate")
118        .user("Tell me about the weather")
119        .max_tokens(100);
120
121    let mut stream = client.send_chat_stream(builder).await?;
122
123    print!("Response: ");
124    while let Some(chunk) = stream.next().await {
125        let chunk = chunk?;
126        if let Some(content) = chunk.content() {
127            print!("{}", content);
128        }
129    }
130    println!();
131
132    Ok(())
133}
134
135async fn multiple_turns(client: &Client) -> Result<()> {
136    println!("Building a conversation with multiple turns...\n");
137
138    // First turn
139    println!("User: What is 2+2?");
140    let builder = client.chat().user("What is 2+2?");
141
142    let mut stream = client.send_chat_stream(builder).await?;
143
144    print!("Assistant: ");
145    let mut first_response = String::new();
146    while let Some(chunk) = stream.next().await {
147        let chunk = chunk?;
148        if let Some(content) = chunk.content() {
149            print!("{}", content);
150            first_response.push_str(content);
151        }
152    }
153    println!();
154
155    // Second turn - continuing the conversation
156    println!("\nUser: Now multiply that by 3");
157    let builder = client
158        .chat()
159        .user("What is 2+2?")
160        .assistant(&first_response)
161        .user("Now multiply that by 3");
162
163    let mut stream = client.send_chat_stream(builder).await?;
164
165    print!("Assistant: ");
166    while let Some(chunk) = stream.next().await {
167        let chunk = chunk?;
168        if let Some(content) = chunk.content() {
169            print!("{}", content);
170        }
171    }
172    println!();
173
174    Ok(())
175}