|
12 | 12 | # This golden example showcases ALL OpenAI chat completion features with proper tracing: |
13 | 13 | # 1. Vision (image understanding) with array content |
14 | 14 | # 2. Tool/function calling (single-turn) |
15 | | -# 3. Multi-turn tool calling with tool_call_id |
16 | | -# 4. Mixed content (text + images) |
17 | | -# 5. Reasoning models with advanced token metrics |
18 | | -# 6. Temperature variations and other parameters |
| 15 | +# 3. Streaming chat completions with automatic chunk aggregation |
| 16 | +# 4. Multi-turn tool calling with tool_call_id |
| 17 | +# 5. Mixed content (text + images) |
| 18 | +# 6. Reasoning models with advanced token metrics |
| 19 | +# 7. Temperature variations |
| 20 | +# 8. Advanced parameters |
19 | 21 | # |
20 | 22 | # This example validates that the Ruby SDK captures the same data as TypeScript/Go SDKs. |
21 | 23 | # |
|
116 | 118 | puts " Tokens: #{response.usage.total_tokens}" |
117 | 119 | end |
118 | 120 |
|
119 | | - # Example 3: Multi-turn Tool Calling (with tool_call_id) |
120 | | - puts "\n3. Multi-turn Tool Calling" |
| 121 | + # Example 3: Streaming Chat Completions |
| 122 | + puts "\n3. Streaming Chat Completions" |
| 123 | + puts "-" * 50 |
| 124 | + tracer.in_span("example-streaming") do |
| 125 | + print "Streaming response: " |
| 126 | + stream = client.chat.completions.stream_raw( |
| 127 | + model: "gpt-4o-mini", |
| 128 | + messages: [ |
| 129 | + {role: "user", content: "Count from 1 to 5"} |
| 130 | + ], |
| 131 | + max_tokens: 50, |
| 132 | + stream_options: { |
| 133 | + include_usage: true # Request usage stats in stream |
| 134 | + } |
| 135 | + ) |
| 136 | + |
| 137 | + # Consume and display the stream |
| 138 | + total_tokens = 0 |
| 139 | + stream.each do |chunk| |
| 140 | + delta_content = chunk.choices[0]&.delta&.content |
| 141 | + print delta_content if delta_content |
| 142 | + |
| 143 | + # Capture usage from final chunk |
| 144 | + total_tokens = chunk.usage&.total_tokens if chunk.usage |
| 145 | + end |
| 146 | + puts "" |
| 147 | + puts "✓ Streaming complete, tokens: #{total_tokens}" |
| 148 | + puts " (Note: Braintrust automatically aggregates all chunks for the trace)" |
| 149 | + end |
| 150 | + |
| 151 | + # Example 4: Multi-turn Tool Calling (with tool_call_id) |
| 152 | + puts "\n4. Multi-turn Tool Calling" |
121 | 153 | puts "-" * 50 |
122 | 154 | tracer.in_span("example-multi-turn-tools") do |
123 | 155 | # First request - model decides to call a tool |
|
195 | 227 | end |
196 | 228 | end |
197 | 229 |
|
198 | | - # Example 4: Mixed Content (text + image in same message) |
199 | | - puts "\n4. Mixed Content (Text + Image)" |
| 230 | + # Example 5: Mixed Content (text + image in same message) |
| 231 | + puts "\n5. Mixed Content (Text + Image)" |
200 | 232 | puts "-" * 50 |
201 | 233 | tracer.in_span("example-mixed-content") do |
202 | 234 | response = client.chat.completions.create( |
|
222 | 254 | puts " Tokens: #{response.usage.total_tokens}" |
223 | 255 | end |
224 | 256 |
|
225 | | - # Example 5: Reasoning Model (o1-mini) with Advanced Token Metrics |
226 | | - puts "\n5. Reasoning Model (o1-mini)" |
| 257 | + # Example 6: Reasoning Model (o1-mini) with Advanced Token Metrics |
| 258 | + puts "\n6. Reasoning Model (o1-mini)" |
227 | 259 | puts "-" * 50 |
228 | 260 | tracer.in_span("example-reasoning") do |
229 | 261 | response = client.chat.completions.create( |
|
252 | 284 | end |
253 | 285 | end |
254 | 286 |
|
255 | | - # Example 6: Temperature & Parameter Variations |
256 | | - puts "\n6. Temperature & Parameter Variations" |
| 287 | + # Example 7: Temperature & Parameter Variations |
| 288 | + puts "\n7. Temperature & Parameter Variations" |
257 | 289 | puts "-" * 50 |
258 | 290 | tracer.in_span("example-temperature-variations") do |
259 | 291 | [0.0, 0.7, 1.0].each do |temp| |
|
269 | 301 | end |
270 | 302 | end |
271 | 303 |
|
272 | | - # Example 7: Advanced Parameters Showcase |
273 | | - puts "\n7. Advanced Parameters (metadata capture)" |
| 304 | + # Example 8: Advanced Parameters Showcase |
| 305 | + puts "\n8. Advanced Parameters (metadata capture)" |
274 | 306 | puts "-" * 50 |
275 | 307 | tracer.in_span("example-advanced-params") do |
276 | 308 | response = client.chat.completions.create( |
|
302 | 334 | puts "This golden example validates that the Ruby SDK properly captures:" |
303 | 335 | puts " ✓ Vision messages with array content (text + image_url)" |
304 | 336 | puts " ✓ Tool calling (single and multi-turn with tool_call_id)" |
| 337 | +puts " ✓ Streaming chat completions with chunk aggregation" |
305 | 338 | puts " ✓ Mixed content messages (multiple text/image blocks)" |
306 | 339 | puts " ✓ Advanced token metrics (cached, reasoning, audio tokens)" |
307 | 340 | puts " ✓ All request parameters (temperature, top_p, seed, user, etc.)" |
|
0 commit comments