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