-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python: .Net: Add function calling with AzureAIInference sample #9954
Open
markwallace-microsoft
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
markwallace-microsoft:users/markwallace/issue_9933
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−10
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d7cd01d
Add function calling with AzureAIInference sample
markwallace-microsoft 717ccfe
Merge branch 'main' into users/markwallace/issue_9933
markwallace-microsoft 764bb92
Merge branch 'main' into users/markwallace/issue_9933
markwallace-microsoft da9c503
Merge branch 'main' into users/markwallace/issue_9933
markwallace-microsoft c40828c
Make tools and stop sequences optional
markwallace-microsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
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,89 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Connectors.AzureAIInference; | ||
|
||
namespace FunctionCalling; | ||
public class AzureAIInference_FunctionCalling : BaseTest | ||
{ | ||
private readonly LoggingHandler _handler; | ||
private readonly HttpClient _httpClient; | ||
private bool _isDisposed; | ||
|
||
public AzureAIInference_FunctionCalling(ITestOutputHelper output) : base(output) | ||
{ | ||
// Create a logging handler to output HTTP requests and responses | ||
this._handler = new LoggingHandler(new HttpClientHandler(), this.Output); | ||
this._httpClient = new(this._handler); | ||
} | ||
|
||
/// <summary> | ||
/// This example demonstrates usage of <see cref="FunctionChoiceBehavior.Auto"/> that advertises all kernel functions to the AI model. | ||
/// </summary> | ||
[Fact] | ||
public async Task FunctionCallingAsync() | ||
{ | ||
var kernel = CreateKernel(); | ||
AzureAIInferencePromptExecutionSettings 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))); | ||
} | ||
|
||
/// <summary> | ||
/// This example demonstrates usage of <see cref="FunctionChoiceBehavior.Auto"/> that advertises all kernel functions to the AI model. | ||
/// </summary> | ||
[Fact] | ||
public async Task FunctionCallingWithPromptExecutionSettingsAsync() | ||
{ | ||
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))); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (!this._isDisposed) | ||
{ | ||
if (disposing) | ||
{ | ||
this._handler.Dispose(); | ||
this._httpClient.Dispose(); | ||
} | ||
|
||
this._isDisposed = true; | ||
} | ||
base.Dispose(disposing); | ||
} | ||
|
||
private Kernel CreateKernel() | ||
{ | ||
// Create kernel | ||
var kernel = Kernel.CreateBuilder() | ||
.AddAzureAIInferenceChatCompletion( | ||
modelId: TestConfiguration.AzureAIInference.ChatModelId, | ||
endpoint: new Uri(TestConfiguration.AzureAIInference.Endpoint), | ||
apiKey: TestConfiguration.AzureAIInference.ApiKey, | ||
httpClient: this._httpClient) | ||
.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; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initialising tools to an empty array causes a Problem in the JSON payload.
@RogerBarreto Is there a reason why this is being initialised to an empty array?