openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Responses/Example04_FunctionCallingStreamingAsync.cs
112lines · modeblame
d893a79fJose Arriaga Maldonado11 months ago | 1 | using NUnit.Framework; |
| 2 | using OpenAI.Responses; | |
| 3 | using System; | |
| 4 | using System.ClientModel; | |
| 5 | using System.Collections.Generic; | |
| 6 | using System.Linq; | |
| 7 | using System.Text.Json; | |
| 8 | using System.Threading.Tasks; | |
| 9 | | |
| 10 | namespace 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 | | |
| 16 | public partial class ResponseExamples | |
| 17 | { | |
| 18 | // See Example03_FunctionCalling.cs for the tool and function definitions. | |
| 19 | | |
| 20 | [Test] | |
| 21 | public async Task Example04_FunctionCallingStreamingAsync() | |
| 22 | { | |
| 23 | OpenAIResponseClient client = new("gpt-5", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); | |
| 24 | | |
| 25 | List<ResponseItem> inputItems = | |
| 26 | [ | |
| 27 | ResponseItem.CreateUserMessageItem("What's the weather like today for my current location?"), | |
| 28 | ]; | |
| 29 | | |
| 30 | ResponseCreationOptions options = new() | |
| 31 | { | |
| 32 | Tools = { getCurrentLocationTool, getCurrentWeatherTool }, | |
| 33 | }; | |
| 34 | | |
| 35 | PrintMessageItems(inputItems.OfType<MessageResponseItem>()); | |
| 36 | | |
| 37 | bool requiresAction; | |
| 38 | | |
| 39 | do | |
| 40 | { | |
| 41 | requiresAction = false; | |
| 42 | AsyncCollectionResult<StreamingResponseUpdate> responseUpdates = client.CreateResponseStreamingAsync(inputItems, options); | |
| 43 | | |
| 44 | await foreach (StreamingResponseUpdate update in responseUpdates) | |
| 45 | { | |
| 46 | if (update is StreamingResponseOutputItemAddedUpdate outputItemAddedUpdated) | |
| 47 | { | |
| 48 | if (outputItemAddedUpdated.Item is MessageResponseItem message && message.Role == MessageRole.Assistant) | |
| 49 | { | |
| 50 | Console.WriteLine($"[ASSISTANT]:"); | |
| 51 | } | |
| 52 | } | |
| 53 | | |
| 54 | if (update is StreamingResponseOutputTextDeltaUpdate outputTextUpdate) | |
| 55 | { | |
| 56 | Console.Write(outputTextUpdate.Delta); | |
| 57 | } | |
| 58 | | |
| 59 | if (update is StreamingResponseOutputItemDoneUpdate outputItemDoneUpdate) | |
| 60 | { | |
| 61 | inputItems.Add(outputItemDoneUpdate.Item); | |
| 62 | | |
| 63 | if (outputItemDoneUpdate.Item is FunctionCallResponseItem functionCall) | |
| 64 | { | |
| 65 | switch (functionCall.FunctionName) | |
| 66 | { | |
| 67 | case nameof(GetCurrentLocation): | |
| 68 | { | |
| 69 | string functionOutput = GetCurrentLocation(); | |
| 70 | inputItems.Add(new FunctionCallOutputResponseItem(functionCall.CallId, functionOutput)); | |
| 71 | break; | |
| 72 | } | |
| 73 | | |
| 74 | case nameof(GetCurrentWeather): | |
| 75 | { | |
| 76 | // The arguments that the model wants to use to call the function are specified as a | |
| 77 | // stringified JSON object based on the schema defined in the tool definition. Note that | |
| 78 | // the model may hallucinate arguments too. Consequently, it is important to do the | |
| 79 | // appropriate parsing and validation before calling the function. | |
| 80 | using JsonDocument argumentsJson = JsonDocument.Parse(functionCall.FunctionArguments); | |
| 81 | bool hasLocation = argumentsJson.RootElement.TryGetProperty("location", out JsonElement location); | |
| 82 | bool hasUnit = argumentsJson.RootElement.TryGetProperty("unit", out JsonElement unit); | |
| 83 | | |
| 84 | if (!hasLocation) | |
| 85 | { | |
| 86 | throw new ArgumentNullException(nameof(location), "The location argument is required."); | |
| 87 | } | |
| 88 | | |
| 89 | string functionOutput = hasUnit | |
| 90 | ? GetCurrentWeather(location.GetString(), unit.GetString()) | |
| 91 | : GetCurrentWeather(location.GetString()); | |
| 92 | inputItems.Add(new FunctionCallOutputResponseItem(functionCall.CallId, functionOutput)); | |
| 93 | break; | |
| 94 | } | |
| 95 | | |
| 96 | default: | |
| 97 | { | |
| 98 | // Handle other unexpected calls. | |
| 99 | throw new NotImplementedException(); | |
| 100 | } | |
| 101 | } | |
| 102 | | |
| 103 | requiresAction = true; | |
| 104 | break; | |
| 105 | } | |
| 106 | } | |
| 107 | } | |
| 108 | } while (requiresAction); | |
| 109 | } | |
| 110 | } | |
| 111 | | |
| 112 | #pragma warning restore OPENAI001 |