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