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 · modeblame

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