openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Responses/Example02_SimpleResponseStreamingAsync.cs
34lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Responses; |
| 3 | using System; |
| 4 | using System.ClientModel; |
| 5 | using System.Threading.Tasks; |
| 6 | |
| 7 | namespace OpenAI.Examples; |
| 8 | |
| 9 | // This example uses experimental APIs which are subject to change. To use experimental APIs, |
| 10 | // please acknowledge their experimental status by suppressing the corresponding warning. |
| 11 | |
| 12 | #pragma warning disable OPENAI001 |
| 13 | |
| 14 | public partial class ResponseExamples |
| 15 | { |
| 16 | [Test] |
| 17 | public async Task Example02_SimpleResponseStreamingAsync() |
| 18 | { |
| 19 | OpenAIResponseClient client = new(model: "gpt-5", apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 20 | |
| 21 | AsyncCollectionResult<StreamingResponseUpdate> responseUpdates = client.CreateResponseStreamingAsync("Say 'this is a test.'"); |
| 22 | |
| 23 | Console.Write($"[ASSISTANT]: "); |
| 24 | await foreach (StreamingResponseUpdate update in responseUpdates) |
| 25 | { |
| 26 | if (update is StreamingResponseOutputTextDeltaUpdate outputTextUpdate) |
| 27 | { |
| 28 | Console.Write(outputTextUpdate.Delta); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #pragma warning restore OPENAI001 |