openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
joseharriaga/ReorganizeExamples

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Chat/Example02_SimpleChatStreamingAsync.cs

27lines · 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", apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
15
16 AsyncCollectionResult<StreamingChatCompletionUpdate> completionUpdates = client.CompleteChatStreamingAsync("Say 'this is a test.'");
17
18 Console.Write($"[ASSISTANT]: ");
19 await foreach (StreamingChatCompletionUpdate completionUpdate in completionUpdates)
20 {
21 if (completionUpdate.ContentUpdate.Count > 0)
22 {
23 Console.Write(completionUpdate.ContentUpdate[0].Text);
24 }
25 }
26 }
27}
28