openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Responses/Example02_SimpleResponseStreamingAsync.cs
34lines · modeblame
5dce104aJose Arriaga Maldonado1 years ago | 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 | { | |
5ff668b5Copilot5 months ago | 19 | ResponsesClient client = new(apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
5dce104aJose Arriaga Maldonado1 years ago | 20 | |
5ff668b5Copilot5 months ago | 21 | AsyncCollectionResult<StreamingResponseUpdate> responseUpdates = client.CreateResponseStreamingAsync("gpt-5", "Say 'this is a test.'"); |
5dce104aJose Arriaga Maldonado1 years ago | 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 |