openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Chat/Example02_SimpleChatStreaming.cs
30lines · modeblame
9f9f2936Jose Arriaga Maldonado2 years ago | 1 | using NUnit.Framework; |
| 2 | using OpenAI.Chat; | |
| 3 | using System; | |
| 4 | using System.ClientModel; | |
| 5 | | |
8cc6643fJose Arriaga Maldonado2 years ago | 6 | namespace OpenAI.Examples; |
9f9f2936Jose Arriaga Maldonado2 years ago | 7 | |
8cc6643fJose Arriaga Maldonado2 years ago | 8 | public partial class ChatExamples |
9f9f2936Jose Arriaga Maldonado2 years ago | 9 | { |
| 10 | [Test] | |
8cc6643fJose Arriaga Maldonado2 years ago | 11 | public void Example02_SimpleChatStreaming() |
9f9f2936Jose Arriaga Maldonado2 years ago | 12 | { |
| 13 | ChatClient client = new("gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); | |
| 14 | | |
974796a9Jose Arriaga Maldonado2 years ago | 15 | ResultCollection<StreamingChatCompletionUpdate> chatUpdates |
74848f8dJose Arriaga Maldonado2 years ago | 16 | = client.CompleteChatStreaming( |
| 17 | [ | |
| 18 | new UserChatMessage("Say 'this is a test.'"), | |
| 19 | ]); | |
9f9f2936Jose Arriaga Maldonado2 years ago | 20 | |
8cc6643fJose Arriaga Maldonado2 years ago | 21 | Console.WriteLine($"[ASSISTANT]:"); |
9f9f2936Jose Arriaga Maldonado2 years ago | 22 | foreach (StreamingChatCompletionUpdate chatUpdate in chatUpdates) |
| 23 | { | |
| 24 | foreach (ChatMessageContentPart contentPart in chatUpdate.ContentUpdate) | |
| 25 | { | |
| 26 | Console.Write(contentPart.Text); | |
| 27 | } | |
| 28 | } | |
| 29 | } | |
| 30 | } |