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_HelloWorldStreaming.cs

34lines · modecode

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