openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.6.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Responses/Example03_FunctionCallingAsync.cs

98lines · modeblame

d893a79fJose Arriaga Maldonado11 months ago1using NUnit.Framework;
2using OpenAI.Responses;
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Text.Json;
7using System.Threading.Tasks;
8
9namespace OpenAI.Examples;
10
11// This example uses experimental APIs which are subject to change. To use experimental APIs,
12// please acknowledge their experimental status by suppressing the corresponding warning.
13#pragma warning disable OPENAI001
14
15public partial class ResponseExamples
16{
17// See Example03_FunctionCalling.cs for the tool and function definitions.
18
19[Test]
20public async Task Example03_FunctionCallingAsync()
21{
22OpenAIResponseClient client = new("gpt-5", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
23
24List<ResponseItem> inputItems =
25[
26ResponseItem.CreateUserMessageItem("What's the weather like today for my current location?"),
27];
28
29ResponseCreationOptions options = new()
30{
31Tools = { getCurrentLocationTool, getCurrentWeatherTool },
32};
33
34PrintMessageItems(inputItems.OfType<MessageResponseItem>());
35
36bool requiresAction;
37
38do
39{
40requiresAction = false;
41OpenAIResponse response = await client.CreateResponseAsync(inputItems, options);
42
43inputItems.AddRange(response.OutputItems);
44
45foreach (ResponseItem outputItem in response.OutputItems)
46{
47if (outputItem is FunctionCallResponseItem functionCall)
48{
49switch (functionCall.FunctionName)
50{
51case nameof(GetCurrentLocation):
52{
53string functionOutput = GetCurrentLocation();
54inputItems.Add(new FunctionCallOutputResponseItem(functionCall.CallId, functionOutput));
55break;
56}
57
58case nameof(GetCurrentWeather):
59{
60// The arguments that the model wants to use to call the function are specified as a
61// stringified JSON object based on the schema defined in the tool definition. Note that
62// the model may hallucinate arguments too. Consequently, it is important to do the
63// appropriate parsing and validation before calling the function.
64using JsonDocument argumentsJson = JsonDocument.Parse(functionCall.FunctionArguments);
65bool hasLocation = argumentsJson.RootElement.TryGetProperty("location", out JsonElement location);
66bool hasUnit = argumentsJson.RootElement.TryGetProperty("unit", out JsonElement unit);
67
68if (!hasLocation)
69{
70throw new ArgumentNullException(nameof(location), "The location argument is required.");
71}
72
73string functionOutput = hasUnit
74? GetCurrentWeather(location.GetString(), unit.GetString())
75: GetCurrentWeather(location.GetString());
76inputItems.Add(new FunctionCallOutputResponseItem(functionCall.CallId, functionOutput));
77break;
78}
79
80default:
81{
82// Handle other unexpected calls.
83throw new NotImplementedException();
84}
85}
86
87requiresAction = true;
88break;
89}
90}
91
92PrintMessageItems(response.OutputItems.OfType<MessageResponseItem>());
93
94} while (requiresAction);
95}
96}
97
98#pragma warning restore OPENAI001