openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Chat/Example02_SimpleChatStreaming.cs
27lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Chat; |
| 3 | using System; |
| 4 | using System.ClientModel; |
| 5 | |
| 6 | namespace OpenAI.Examples; |
| 7 | |
| 8 | public partial class ChatExamples |
| 9 | { |
| 10 | [Test] |
| 11 | public void Example02_SimpleChatStreaming() |
| 12 | { |
| 13 | ChatClient client = new(model: "gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 14 | |
| 15 | CollectionResult<StreamingChatCompletionUpdate> updates |
| 16 | = client.CompleteChatStreaming("Say 'this is a test.'"); |
| 17 | |
| 18 | Console.WriteLine($"[ASSISTANT]:"); |
| 19 | foreach (StreamingChatCompletionUpdate update in updates) |
| 20 | { |
| 21 | foreach (ChatMessageContentPart updatePart in update.ContentUpdate) |
| 22 | { |
| 23 | Console.Write(updatePart); |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | } |