-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function calling with AzureAIInference sample
- Loading branch information
1 parent
88635e1
commit d7cd01d
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
dotnet/samples/Concepts/FunctionCalling/AzureAIInference_FunctionCalling.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string> { "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; | ||
} | ||
} |