Skip to content
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
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
3 changes: 2 additions & 1 deletion dotnet/samples/Concepts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ dotnet test -l "console;verbosity=detailed" --filter "FullyQualifiedName=ChatCom

### FunctionCalling - Examples on `Function Calling` with function call capable models

- [Gemini_FunctionCalling](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/FunctionCalling/Gemini_FunctionCalling.cs)
- [FunctionCalling](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/FunctionCalling/FunctionCalling.cs)
- [Gemini_FunctionCalling](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/FunctionCalling/Gemini_FunctionCalling.cs)
- [AzureAIInference_FunctionCalling](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/FunctionCalling/AzureAIInference_FunctionCalling.cs)
- [NexusRaven_HuggingFaceTextGeneration](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/FunctionCalling/NexusRaven_FunctionCalling.cs)
- [MultipleFunctionsVsParameters](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/FunctionCalling/MultipleFunctionsVsParameters.cs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public void ItCreatesAzureAIInferenceExecutionSettingsWithCorrectDefaults()
Assert.Null(executionSettings.ResponseFormat);
Assert.Null(executionSettings.Seed);
Assert.Null(executionSettings.MaxTokens);
Assert.Null(executionSettings.Tools);
Assert.Null(executionSettings.StopSequences);
Assert.Empty(executionSettings.ExtensionData!);
Assert.Empty(executionSettings.Tools);
Assert.Empty(executionSettings.StopSequences!);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public object? ResponseFormat
/// <summary> A collection of textual sequences that will end completions generation. </summary>
[JsonPropertyName("stop")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public IList<string> StopSequences
public IList<string>? StopSequences
{
get => this._stopSequences;
set
Expand All @@ -170,7 +170,7 @@ public IList<string> StopSequences
/// </summary>
[JsonPropertyName("tools")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public IList<ChatCompletionsToolDefinition> Tools
public IList<ChatCompletionsToolDefinition>? Tools
{
get => this._tools;
set
Expand Down Expand Up @@ -229,8 +229,8 @@ public override PromptExecutionSettings Clone()
NucleusSamplingFactor = this.NucleusSamplingFactor,
MaxTokens = this.MaxTokens,
ResponseFormat = this.ResponseFormat,
StopSequences = new List<string>(this.StopSequences),
Tools = new List<ChatCompletionsToolDefinition>(this.Tools),
StopSequences = this.StopSequences is not null ? new List<string>(this.StopSequences) : null,
Tools = this.Tools is not null ? new List<ChatCompletionsToolDefinition>(this.Tools) : null,
Seed = this.Seed,
ExtensionData = this.ExtensionData is not null ? new Dictionary<string, object>(this.ExtensionData) : null,
};
Expand Down Expand Up @@ -273,8 +273,8 @@ public static AzureAIInferencePromptExecutionSettings FromExecutionSettings(Prom
private float? _nucleusSamplingFactor;
private int? _maxTokens;
private object? _responseFormat;
private IList<string> _stopSequences = [];
private IList<ChatCompletionsToolDefinition> _tools = [];
private IList<string>? _stopSequences;
private IList<ChatCompletionsToolDefinition>? _tools;
Copy link
Member Author

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?

private long? _seed;

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

# Create a chat history object with the system message.
chat_history = ChatHistory(system_message=system_message)
# Configure the store amd metadata settings for the chat completion service.
# Configure the store and metadata settings for the chat completion service.
request_settings.store = True
request_settings.metadata = {"chatbot": "Mosscap"}

Expand Down
Loading