openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Chat/Example02_SimpleChatStreamingAsync.cs
27lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Chat; |
| 3 | using System; |
| 4 | using System.ClientModel; |
| 5 | using System.Threading.Tasks; |
| 6 | |
| 7 | namespace OpenAI.Examples; |
| 8 | |
| 9 | public partial class ChatExamples |
| 10 | { |
| 11 | [Test] |
| 12 | public async Task Example02_SimpleChatStreamingAsync() |
| 13 | { |
| 14 | ChatClient client = new(model: "gpt-4o", apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 15 | |
| 16 | AsyncCollectionResult<StreamingChatCompletionUpdate> completionUpdates = client.CompleteChatStreamingAsync("Say 'this is a test.'"); |
| 17 | |
| 18 | Console.Write($"[ASSISTANT]: "); |
| 19 | await foreach (StreamingChatCompletionUpdate completionUpdate in completionUpdates) |
| 20 | { |
| 21 | if (completionUpdate.ContentUpdate.Count > 0) |
| 22 | { |
| 23 | Console.Write(completionUpdate.ContentUpdate[0].Text); |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |