openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.9.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Responses/Example04_FunctionCallingStreamingAsync.cs

109lines · modeblame

d893a79fJose Arriaga Maldonado11 months ago1using NUnit.Framework;
2using OpenAI.Responses;
3using System;
4using System.ClientModel;
5using System.Collections.Generic;
6using System.Linq;
7using System.Text.Json;
8using System.Threading.Tasks;
9
10namespace OpenAI.Examples;
11
12// This example uses experimental APIs which are subject to change. To use experimental APIs,
13// please acknowledge their experimental status by suppressing the corresponding warning.
14#pragma warning disable OPENAI001
15
16public partial class ResponseExamples
17{
18// See Example03_FunctionCalling.cs for the tool and function definitions.
19
20[Test]
21public async Task Example04_FunctionCallingStreamingAsync()
22{
5ff668b5Copilot6 months ago23ResponsesClient client = new(apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
d893a79fJose Arriaga Maldonado11 months ago24
e68ed8b8Copilot5 months ago25CreateResponseOptions options = new("gpt-5", [ResponseItem.CreateUserMessageItem("What's the weather like today for my current location?")])
26{
27Tools = { getCurrentLocationTool, getCurrentWeatherTool },
28StreamingEnabled = true,
29};
d893a79fJose Arriaga Maldonado11 months ago30
e68ed8b8Copilot5 months ago31PrintMessageItems(options.InputItems.OfType<MessageResponseItem>());
d893a79fJose Arriaga Maldonado11 months ago32
33bool requiresAction;
34
35do
36{
37requiresAction = false;
74f7e256Jose Arriaga Maldonado8 months ago38
33db028aChristopher Scott8 months ago39AsyncCollectionResult<StreamingResponseUpdate> responseUpdates = client.CreateResponseStreamingAsync(options);
d893a79fJose Arriaga Maldonado11 months ago40
41await foreach (StreamingResponseUpdate update in responseUpdates)
42{
43if (update is StreamingResponseOutputItemAddedUpdate outputItemAddedUpdated)
44{
45if (outputItemAddedUpdated.Item is MessageResponseItem message && message.Role == MessageRole.Assistant)
46{
47Console.WriteLine($"[ASSISTANT]:");
48}
49}
50
51if (update is StreamingResponseOutputTextDeltaUpdate outputTextUpdate)
52{
53Console.Write(outputTextUpdate.Delta);
54}
55
56if (update is StreamingResponseOutputItemDoneUpdate outputItemDoneUpdate)
57{
e68ed8b8Copilot5 months ago58options.InputItems.Add(outputItemDoneUpdate.Item);
d893a79fJose Arriaga Maldonado11 months ago59
60if (outputItemDoneUpdate.Item is FunctionCallResponseItem functionCall)
61{
62switch (functionCall.FunctionName)
63{
64case nameof(GetCurrentLocation):
65{
66string functionOutput = GetCurrentLocation();
e68ed8b8Copilot5 months ago67options.InputItems.Add(new FunctionCallOutputResponseItem(functionCall.CallId, functionOutput));
d893a79fJose Arriaga Maldonado11 months ago68break;
69}
70
71case nameof(GetCurrentWeather):
72{
73// The arguments that the model wants to use to call the function are specified as a
74// stringified JSON object based on the schema defined in the tool definition. Note that
75// the model may hallucinate arguments too. Consequently, it is important to do the
76// appropriate parsing and validation before calling the function.
77using JsonDocument argumentsJson = JsonDocument.Parse(functionCall.FunctionArguments);
78bool hasLocation = argumentsJson.RootElement.TryGetProperty("location", out JsonElement location);
79bool hasUnit = argumentsJson.RootElement.TryGetProperty("unit", out JsonElement unit);
80
81if (!hasLocation)
82{
83throw new ArgumentNullException(nameof(location), "The location argument is required.");
84}
85
86string functionOutput = hasUnit
87? GetCurrentWeather(location.GetString(), unit.GetString())
88: GetCurrentWeather(location.GetString());
e68ed8b8Copilot5 months ago89options.InputItems.Add(new FunctionCallOutputResponseItem(functionCall.CallId, functionOutput));
d893a79fJose Arriaga Maldonado11 months ago90break;
91}
92
93default:
94{
95// Handle other unexpected calls.
96throw new NotImplementedException();
97}
98}
99
100requiresAction = true;
101break;
102}
103}
104}
105} while (requiresAction);
106}
107}
108
109#pragma warning restore OPENAI001