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/Example03_FunctionCallingAsync.cs

99lines · 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{
5ff668b5Copilot5 months ago22ResponsesClient client = new(apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
d893a79fJose Arriaga Maldonado11 months ago23
24List<ResponseItem> inputItems =
25[
26ResponseItem.CreateUserMessageItem("What's the weather like today for my current location?"),
27];
28
29PrintMessageItems(inputItems.OfType<MessageResponseItem>());
30
31bool requiresAction;
32
33do
34{
35requiresAction = false;
74f7e256Jose Arriaga Maldonado8 months ago36
5ff668b5Copilot5 months ago37CreateResponseOptions options = new("gpt-5", inputItems)
74f7e256Jose Arriaga Maldonado8 months ago38{
39Tools = { getCurrentLocationTool, getCurrentWeatherTool },
40};
41
33db028aChristopher Scott8 months ago42ResponseResult response = await client.CreateResponseAsync(options);
d893a79fJose Arriaga Maldonado11 months ago43
44inputItems.AddRange(response.OutputItems);
45
46foreach (ResponseItem outputItem in response.OutputItems)
47{
48if (outputItem is FunctionCallResponseItem functionCall)
49{
50switch (functionCall.FunctionName)
51{
52case nameof(GetCurrentLocation):
53{
54string functionOutput = GetCurrentLocation();
55inputItems.Add(new FunctionCallOutputResponseItem(functionCall.CallId, functionOutput));
56break;
57}
58
59case nameof(GetCurrentWeather):
60{
61// The arguments that the model wants to use to call the function are specified as a
62// stringified JSON object based on the schema defined in the tool definition. Note that
63// the model may hallucinate arguments too. Consequently, it is important to do the
64// appropriate parsing and validation before calling the function.
65using JsonDocument argumentsJson = JsonDocument.Parse(functionCall.FunctionArguments);
66bool hasLocation = argumentsJson.RootElement.TryGetProperty("location", out JsonElement location);
67bool hasUnit = argumentsJson.RootElement.TryGetProperty("unit", out JsonElement unit);
68
69if (!hasLocation)
70{
71throw new ArgumentNullException(nameof(location), "The location argument is required.");
72}
73
74string functionOutput = hasUnit
75? GetCurrentWeather(location.GetString(), unit.GetString())
76: GetCurrentWeather(location.GetString());
77inputItems.Add(new FunctionCallOutputResponseItem(functionCall.CallId, functionOutput));
78break;
79}
80
81default:
82{
83// Handle other unexpected calls.
84throw new NotImplementedException();
85}
86}
87
88requiresAction = true;
89break;
90}
91}
92
93PrintMessageItems(response.OutputItems.OfType<MessageResponseItem>());
94
95} while (requiresAction);
96}
97}
98
99#pragma warning restore OPENAI001