openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0-beta.4

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Responses/Example02_HelloWorldStreamingAsync.cs

35lines · modecode

1using NUnit.Framework;
2using OpenAI.Responses;
3using System;
4using System.Threading.Tasks;
5
6namespace OpenAI.Examples;
7
8public partial class ResponseExamples
9{
10 [Test]
11 public async Task Example02_HelloWorldStreamingAsync()
12 {
13 OpenAIResponseClient client = new(
14 model: "gpt-4o-mini",
15 apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
16
17 Console.Write($"Streaming text: ");
18
19 await foreach (StreamingResponseUpdate update
20 in client.CreateResponseStreamingAsync("Hello, world!"))
21 {
22 if (update is StreamingResponseOutputTextDeltaUpdate outputTextUpdate)
23 {
24 // Streamed text will arrive as it's generated via delta events
25 Console.Write(outputTextUpdate.Delta);
26 }
27 else if (update is StreamingResponseCompletedUpdate responseCompletedUpdate)
28 {
29 // Item and response completed events have aggregated text available
30 Console.WriteLine();
31 Console.WriteLine($"Final text: {responseCompletedUpdate.Response.GetOutputText()}");
32 }
33 }
34 }
35}
36