openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Chat/Example02_SimpleChatStreamingAsync.cs
28lines · 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", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 15 | |
| 16 | AsyncCollectionResult<StreamingChatCompletionUpdate> updates |
| 17 | = client.CompleteChatStreamingAsync("Say 'this is a test.'"); |
| 18 | |
| 19 | Console.WriteLine($"[ASSISTANT]:"); |
| 20 | await foreach (StreamingChatCompletionUpdate update in updates) |
| 21 | { |
| 22 | foreach (ChatMessageContentPart updatePart in update.ContentUpdate) |
| 23 | { |
| 24 | Console.Write(updatePart.Text); |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |