openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.8

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Chat/Example02_SimpleChatStreamingAsync.cs

28lines · 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(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