From d7cd01df9bc47a617dca88a952b72e0fcec4c144 Mon Sep 17 00:00:00 2001 From: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> Date: Thu, 12 Dec 2024 12:45:49 +0000 Subject: [PATCH] Add function calling with AzureAIInference sample --- .../AzureAIInference_FunctionCalling.cs | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 dotnet/samples/Concepts/FunctionCalling/AzureAIInference_FunctionCalling.cs diff --git a/dotnet/samples/Concepts/FunctionCalling/AzureAIInference_FunctionCalling.cs b/dotnet/samples/Concepts/FunctionCalling/AzureAIInference_FunctionCalling.cs new file mode 100644 index 000000000000..edaa78f2bba9 --- /dev/null +++ b/dotnet/samples/Concepts/FunctionCalling/AzureAIInference_FunctionCalling.cs @@ -0,0 +1,47 @@ +// Copyright (c) Microsoft. All rights reserved. + +using Microsoft.SemanticKernel; + +namespace FunctionCalling; +public class AzureAIInference_FunctionCalling(ITestOutputHelper output) : BaseTest(output) +{ + [Fact] + public async Task FunctionCallingAsync() + { + var kernel = CreateKernel(); + PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }; + Console.WriteLine(await kernel.InvokePromptAsync("Given the current time of day and weather, what is the likely color of the sky in Boston?", new(settings))); + } + + private static Kernel CreateKernel() + { + // Create kernel + var kernel = Kernel.CreateBuilder() + .AddAzureAIInferenceChatCompletion( + modelId: TestConfiguration.AzureAIInference.ChatModelId, + endpoint: new Uri(TestConfiguration.AzureAIInference.Endpoint), + apiKey: TestConfiguration.AzureAIInference.ApiKey) + .Build(); + + // Add a plugin with some helper functions we want to allow the model to call. + kernel.ImportPluginFromFunctions("HelperFunctions", + [ + kernel.CreateFunctionFromMethod(() => new List { "Squirrel Steals Show", "Dog Wins Lottery" }, "GetLatestNewsTitles", "Retrieves latest news titles."), + kernel.CreateFunctionFromMethod(() => DateTime.UtcNow.ToString("R"), "GetCurrentUtcDateTime", "Retrieves the current date time in UTC."), + kernel.CreateFunctionFromMethod((string cityName, string currentDateTime) => + cityName switch + { + "Boston" => "61 and rainy", + "London" => "55 and cloudy", + "Miami" => "80 and sunny", + "Paris" => "60 and rainy", + "Tokyo" => "50 and sunny", + "Sydney" => "75 and sunny", + "Tel Aviv" => "80 and sunny", + _ => "31 and snowing", + }, "GetWeatherForCity", "Gets the current weather for the specified city"), + ]); + + return kernel; + } +}