pub struct ChatCompletionChunk { /* private fields */ }Expand description
A streaming chunk from a chat completion response.
Each chunk represents a delta update from the model as it generates the response.
Implementations§
Source§impl ChatCompletionChunk
impl ChatCompletionChunk
Sourcepub fn new(response: CreateChatCompletionStreamResponse) -> Self
pub fn new(response: CreateChatCompletionStreamResponse) -> Self
Create a new chunk from a stream response.
Sourcepub fn content(&self) -> Option<&str>
pub fn content(&self) -> Option<&str>
Get the content delta from this chunk, if any.
Returns the text content that was generated in this chunk.
Examples found in repository?
examples/chat_streaming.rs (line 59)
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}More examples
examples/langfuse_streaming.rs (line 105)
94async fn basic_streaming(client: &Client<LangfuseState<Span>>) -> Result<()> {
95 println!("Question: Tell me a short joke");
96
97 let builder = client.chat().user("Tell me a short joke");
98
99 let mut stream = client.send_chat_stream(builder).await?;
100
101 print!("Response: ");
102 let mut chunk_count = 0;
103 while let Some(chunk) = stream.next().await {
104 let chunk = chunk?;
105 if let Some(content) = chunk.content() {
106 print!("{}", content);
107 chunk_count += 1;
108 }
109 }
110 println!(
111 "\n(Received {} chunks, all traced to Langfuse)",
112 chunk_count
113 );
114
115 Ok(())
116}
117
118async fn streaming_with_parameters(client: &Client<LangfuseState<Span>>) -> Result<()> {
119 println!("Question: Write a creative tagline for a bakery");
120
121 let builder = client
122 .chat()
123 .user("Write a creative tagline for a bakery")
124 .temperature(0.9)
125 .max_tokens(50);
126
127 let mut stream = client.send_chat_stream(builder).await?;
128
129 print!("Response: ");
130 let mut chunk_count = 0;
131 while let Some(chunk) = stream.next().await {
132 let chunk = chunk?;
133 if let Some(content) = chunk.content() {
134 print!("{}", content);
135 chunk_count += 1;
136 }
137 }
138 println!(
139 "\n(Received {} chunks, all traced to Langfuse)",
140 chunk_count
141 );
142
143 Ok(())
144}
145
146async fn collect_content(client: &Client<LangfuseState<Span>>) -> Result<()> {
147 println!("Question: What is the capital of France?");
148
149 let builder = client.chat().user("What is the capital of France?");
150
151 let mut stream = client.send_chat_stream(builder).await?;
152
153 // Manually collect content (interceptor hooks are still called for each chunk)
154 let mut content = String::new();
155 while let Some(chunk) = stream.next().await {
156 let chunk = chunk?;
157 if let Some(text) = chunk.content() {
158 content.push_str(text);
159 }
160 }
161 println!("Full response: {}", content);
162 println!("(All chunks were traced to Langfuse during collection)");
163
164 Ok(())
165}Sourcepub fn role(&self) -> Option<&str>
pub fn role(&self) -> Option<&str>
Get the role from this chunk, if any.
This is typically only present in the first chunk.
Sourcepub fn tool_calls(&self) -> Option<&Vec<ChatCompletionMessageToolCallChunk>>
pub fn tool_calls(&self) -> Option<&Vec<ChatCompletionMessageToolCallChunk>>
Get tool calls from this chunk, if any.
Sourcepub fn finish_reason(&self) -> Option<&str>
pub fn finish_reason(&self) -> Option<&str>
Get the finish reason, if any.
This indicates why the generation stopped and is only present in the last chunk.
Sourcepub fn raw_response(&self) -> &CreateChatCompletionStreamResponse
pub fn raw_response(&self) -> &CreateChatCompletionStreamResponse
Get the underlying raw response.
Sourcepub fn delta(&self) -> Option<&ChatCompletionStreamResponseDelta>
pub fn delta(&self) -> Option<&ChatCompletionStreamResponseDelta>
Get the delta object directly.
Trait Implementations§
Source§impl Clone for ChatCompletionChunk
impl Clone for ChatCompletionChunk
Source§fn clone(&self) -> ChatCompletionChunk
fn clone(&self) -> ChatCompletionChunk
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for ChatCompletionChunk
impl RefUnwindSafe for ChatCompletionChunk
impl Send for ChatCompletionChunk
impl Sync for ChatCompletionChunk
impl Unpin for ChatCompletionChunk
impl UnwindSafe for ChatCompletionChunk
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more