openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/Chat/Example02_SimpleChatStreamingAsync.cs

31lines · modecode

1using NUnit.Framework;
2using OpenAI.Chat;
3using System;
4using System.ClientModel;
5using System.Threading.Tasks;
6
7namespace OpenAI.Examples;
8
9public 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