openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-1011

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Chat/Example02_SimpleChatStreaming.cs

26lines · modecode

1using NUnit.Framework;
2using OpenAI.Chat;
3using System;
4using System.ClientModel;
5
6namespace OpenAI.Examples;
7
8public partial class ChatExamples
9{
10 [Test]
11 public void Example02_SimpleChatStreaming()
12 {
13 ChatClient client = new(model: "gpt-4o", apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
14
15 CollectionResult<StreamingChatCompletionUpdate> completionUpdates = client.CompleteChatStreaming("Say 'this is a test.'");
16
17 Console.Write($"[ASSISTANT]: ");
18 foreach (StreamingChatCompletionUpdate completionUpdate in completionUpdates)
19 {
20 if (completionUpdate.ContentUpdate.Count > 0)
21 {
22 Console.Write(completionUpdate.ContentUpdate[0].Text);
23 }
24 }
25 }
26}
27