openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
docs/quickstart/responses/stream_responses_build_realtime_apps.cs
25lines · modecode
| 1 | // SAMPLE: Generate streaming response for realtime apps through Responses API |
| 2 | // PAGE: https://platform.openai.com/docs/quickstart#stream-responses-and-build-realtime-apps |
| 3 | // GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start |
| 4 | #pragma warning disable OPENAI001 |
| 5 | |
| 6 | #:package System.Linq.Async@6.* |
| 7 | #:package OpenAI@2.* |
| 8 | |
| 9 | using OpenAI.Responses; |
| 10 | |
| 11 | string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!; |
| 12 | ResponsesClient client = new(key); |
| 13 | |
| 14 | var responses = client.CreateResponseStreamingAsync( |
| 15 | "gpt-5.2", |
| 16 | "Say 'double bubble bath' ten times fast." |
| 17 | ); |
| 18 | |
| 19 | await foreach (var response in responses) |
| 20 | { |
| 21 | if (response is StreamingResponseOutputTextDeltaUpdate delta) |
| 22 | { |
| 23 | Console.Write(delta.Delta); |
| 24 | } |
| 25 | } |
| 26 | |