Skip to content

Commit 8f92fac

Browse files
authored
.Net: AutoFunctionInvocation IChatClient Support (#11536)
### Motivation and Context Auto Invocation filters now are supported when using IChatClients with FunctionChoiceBehavior enabled. - Resolves #10730 - Added dedicated AddChatClient to OpenAI Connector - Added KernelFunctionInvokingChatClient - Updated dependency from ME.AI.Abstractions to ME.AI
1 parent 6c42605 commit 8f92fac

41 files changed

Lines changed: 2385 additions & 1111 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎dotnet/Directory.Packages.props‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
<PackageVersion Include="SharpToken" Version="2.0.3" />
9494
<!-- Microsoft.Extensions.* -->
9595
<PackageVersion Include="Microsoft.Extensions.AI" Version="9.4.0-preview.1.25207.5" />
96-
<PackageVersion Include="Microsoft.Extensions.AI.Abstractions" Version="9.4.0-preview.1.25207.5" />
9796
<PackageVersion Include="Microsoft.Extensions.AI.AzureAIInference" Version="9.4.0-preview.1.25207.5" />
9897
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="9.4.0-preview.1.25207.5" />
9998
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
@@ -109,7 +108,7 @@
109108
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="8.9.1" />
110109
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
111110
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.1" />
112-
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
111+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.3" />
113112
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
114113
<PackageVersion Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />
115114
<PackageVersion Include="Microsoft.Extensions.Options.DataAnnotations" Version="8.0.0" />

‎dotnet/samples/Concepts/Agents/ChatCompletion_ServiceSelection.cs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ private Kernel CreateKernelWithTwoServices(bool useChatClient)
109109
{
110110
builder.Services.AddKeyedChatClient(
111111
ServiceKeyBad,
112-
new OpenAI.OpenAIClient("bad-key").AsChatClient(TestConfiguration.OpenAI.ChatModelId));
112+
new OpenAI.OpenAIClient("bad-key").GetChatClient(TestConfiguration.OpenAI.ChatModelId).AsIChatClient());
113113

114114
builder.Services.AddKeyedChatClient(
115115
ServiceKeyGood,
116-
new OpenAI.OpenAIClient(TestConfiguration.OpenAI.ApiKey).AsChatClient(TestConfiguration.OpenAI.ChatModelId));
116+
new OpenAI.OpenAIClient(TestConfiguration.OpenAI.ApiKey).GetChatClient(TestConfiguration.OpenAI.ChatModelId).AsIChatClient());
117117
}
118118
else
119119
{
@@ -122,14 +122,16 @@ private Kernel CreateKernelWithTwoServices(bool useChatClient)
122122
new Azure.AI.OpenAI.AzureOpenAIClient(
123123
new Uri(TestConfiguration.AzureOpenAI.Endpoint),
124124
new Azure.AzureKeyCredential("bad-key"))
125-
.AsChatClient(TestConfiguration.AzureOpenAI.ChatDeploymentName));
125+
.GetChatClient(TestConfiguration.AzureOpenAI.ChatDeploymentName)
126+
.AsIChatClient());
126127

127128
builder.Services.AddKeyedChatClient(
128129
ServiceKeyGood,
129130
new Azure.AI.OpenAI.AzureOpenAIClient(
130131
new Uri(TestConfiguration.AzureOpenAI.Endpoint),
131132
new Azure.AzureKeyCredential(TestConfiguration.AzureOpenAI.ApiKey))
132-
.AsChatClient(TestConfiguration.AzureOpenAI.ChatDeploymentName));
133+
.GetChatClient(TestConfiguration.AzureOpenAI.ChatDeploymentName)
134+
.AsIChatClient());
133135
}
134136
}
135137
else
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.SemanticKernel;
5+
using Microsoft.SemanticKernel.Connectors.OpenAI;
6+
7+
namespace Filtering;
8+
9+
public class ChatClient_AutoFunctionInvocationFiltering(ITestOutputHelper output) : BaseTest(output)
10+
{
11+
/// <summary>
12+
/// Shows how to use <see cref="IAutoFunctionInvocationFilter"/>.
13+
/// </summary>
14+
[Fact]
15+
public async Task UsingAutoFunctionInvocationFilter()
16+
{
17+
var builder = Kernel.CreateBuilder();
18+
19+
builder.AddOpenAIChatClient("gpt-4", TestConfiguration.OpenAI.ApiKey);
20+
21+
// This filter outputs information about auto function invocation and returns overridden result.
22+
builder.Services.AddSingleton<IAutoFunctionInvocationFilter>(new AutoFunctionInvocationFilter(this.Output));
23+
24+
var kernel = builder.Build();
25+
26+
var function = KernelFunctionFactory.CreateFromMethod(() => "Result from function", "MyFunction");
27+
28+
kernel.ImportPluginFromFunctions("MyPlugin", [function]);
29+
30+
var executionSettings = new OpenAIPromptExecutionSettings
31+
{
32+
FunctionChoiceBehavior = FunctionChoiceBehavior.Required([function], autoInvoke: true)
33+
};
34+
35+
var result = await kernel.InvokePromptAsync("Invoke provided function and return result", new(executionSettings));
36+
37+
Console.WriteLine(result);
38+
39+
// Output:
40+
// Request sequence number: 0
41+
// Function sequence number: 0
42+
// Total number of functions: 1
43+
// Result from auto function invocation filter.
44+
}
45+
46+
/// <summary>
47+
/// Shows how to get list of function calls by using <see cref="IAutoFunctionInvocationFilter"/>.
48+
/// </summary>
49+
[Fact]
50+
public async Task GetFunctionCallsWithFilterAsync()
51+
{
52+
var builder = Kernel.CreateBuilder();
53+
54+
builder.AddOpenAIChatCompletion("gpt-3.5-turbo-1106", TestConfiguration.OpenAI.ApiKey);
55+
56+
builder.Services.AddSingleton<IAutoFunctionInvocationFilter>(new FunctionCallsFilter(this.Output));
57+
58+
var kernel = builder.Build();
59+
60+
kernel.ImportPluginFromFunctions("HelperFunctions",
61+
[
62+
kernel.CreateFunctionFromMethod(() => DateTime.UtcNow.ToString("R"), "GetCurrentUtcTime", "Retrieves the current time in UTC."),
63+
kernel.CreateFunctionFromMethod((string cityName) =>
64+
cityName switch
65+
{
66+
"Boston" => "61 and rainy",
67+
"London" => "55 and cloudy",
68+
"Miami" => "80 and sunny",
69+
"Paris" => "60 and rainy",
70+
"Tokyo" => "50 and sunny",
71+
"Sydney" => "75 and sunny",
72+
"Tel Aviv" => "80 and sunny",
73+
_ => "31 and snowing",
74+
}, "GetWeatherForCity", "Gets the current weather for the specified city"),
75+
]);
76+
77+
var executionSettings = new OpenAIPromptExecutionSettings
78+
{
79+
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
80+
};
81+
82+
await foreach (var chunk in kernel.InvokePromptStreamingAsync("Check current UTC time and return current weather in Boston city.", new(executionSettings)))
83+
{
84+
Console.WriteLine(chunk.ToString());
85+
}
86+
87+
// Output:
88+
// Request #0. Function call: HelperFunctions.GetCurrentUtcTime.
89+
// Request #0. Function call: HelperFunctions.GetWeatherForCity.
90+
// The current UTC time is {time of execution}, and the current weather in Boston is 61°F and rainy.
91+
}
92+
93+
/// <summary>Shows available syntax for auto function invocation filter.</summary>
94+
private sealed class AutoFunctionInvocationFilter(ITestOutputHelper output) : IAutoFunctionInvocationFilter
95+
{
96+
public async Task OnAutoFunctionInvocationAsync(AutoFunctionInvocationContext context, Func<AutoFunctionInvocationContext, Task> next)
97+
{
98+
// Example: get function information
99+
var functionName = context.Function.Name;
100+
101+
// Example: get chat history
102+
var chatHistory = context.ChatHistory;
103+
104+
// Example: get information about all functions which will be invoked
105+
var functionCalls = FunctionCallContent.GetFunctionCalls(context.ChatHistory.Last());
106+
107+
// In function calling functionality there are two loops.
108+
// Outer loop is "request" loop - it performs multiple requests to LLM until user ask will be satisfied.
109+
// Inner loop is "function" loop - it handles LLM response with multiple function calls.
110+
111+
// Workflow example:
112+
// 1. Request to LLM #1 -> Response with 3 functions to call.
113+
// 1.1. Function #1 called.
114+
// 1.2. Function #2 called.
115+
// 1.3. Function #3 called.
116+
// 2. Request to LLM #2 -> Response with 2 functions to call.
117+
// 2.1. Function #1 called.
118+
// 2.2. Function #2 called.
119+
120+
// context.RequestSequenceIndex - it's a sequence number of outer/request loop operation.
121+
// context.FunctionSequenceIndex - it's a sequence number of inner/function loop operation.
122+
// context.FunctionCount - number of functions which will be called per request (based on example above: 3 for first request, 2 for second request).
123+
124+
// Example: get request sequence index
125+
output.WriteLine($"Request sequence index: {context.RequestSequenceIndex}");
126+
127+
// Example: get function sequence index
128+
output.WriteLine($"Function sequence index: {context.FunctionSequenceIndex}");
129+
130+
// Example: get total number of functions which will be called
131+
output.WriteLine($"Total number of functions: {context.FunctionCount}");
132+
133+
// Calling next filter in pipeline or function itself.
134+
// By skipping this call, next filters and function won't be invoked, and function call loop will proceed to the next function.
135+
await next(context);
136+
137+
// Example: get function result
138+
var result = context.Result;
139+
140+
// Example: override function result value
141+
context.Result = new FunctionResult(context.Result, "Result from auto function invocation filter");
142+
143+
// Example: Terminate function invocation
144+
context.Terminate = true;
145+
}
146+
}
147+
148+
/// <summary>Shows how to get list of all function calls per request.</summary>
149+
private sealed class FunctionCallsFilter(ITestOutputHelper output) : IAutoFunctionInvocationFilter
150+
{
151+
public async Task OnAutoFunctionInvocationAsync(AutoFunctionInvocationContext context, Func<AutoFunctionInvocationContext, Task> next)
152+
{
153+
var chatHistory = context.ChatHistory;
154+
var functionCalls = FunctionCallContent.GetFunctionCalls(chatHistory.Last()).ToArray();
155+
156+
if (functionCalls is { Length: > 0 })
157+
{
158+
foreach (var functionCall in functionCalls)
159+
{
160+
output.WriteLine($"Request #{context.RequestSequenceIndex}. Function call: {functionCall.PluginName}.{functionCall.FunctionName}.");
161+
}
162+
}
163+
164+
await next(context);
165+
}
166+
}
167+
}

‎dotnet/samples/Concepts/Kernel/CustomAIServiceSelector.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace KernelExamples;
1111

1212
/// <summary>
13-
/// This sample shows how to use a custom AI service selector to select a specific model by matching it's id.
13+
/// This sample shows how to use a custom AI service selector to select a specific model by matching the model id.
1414
/// </summary>
1515
public class CustomAIServiceSelector(ITestOutputHelper output) : BaseTest(output)
1616
{
@@ -39,7 +39,8 @@ public async Task UsingCustomSelectToSelectServiceByMatchingModelId()
3939
builder.Services
4040
.AddSingleton<IAIServiceSelector>(customSelector)
4141
.AddKeyedChatClient("OpenAIChatClient", new OpenAI.OpenAIClient(TestConfiguration.OpenAI.ApiKey)
42-
.AsChatClient("gpt-4o")); // Add a IChatClient to the kernel
42+
.GetChatClient("gpt-4o")
43+
.AsIChatClient()); // Add a IChatClient to the kernel
4344

4445
Kernel kernel = builder.Build();
4546

@@ -60,7 +61,6 @@ private sealed class GptAIServiceSelector(string modelNameStartsWith, ITestOutpu
6061
private readonly ITestOutputHelper _output = output;
6162
private readonly string _modelNameStartsWith = modelNameStartsWith;
6263

63-
/// <inheritdoc/>
6464
private bool TrySelect<T>(
6565
Kernel kernel, KernelFunction function, KernelArguments arguments,
6666
[NotNullWhen(true)] out T? service, out PromptExecutionSettings? serviceSettings) where T : class
@@ -78,7 +78,7 @@ private bool TrySelect<T>(
7878
else if (serviceToCheck is IChatClient chatClient)
7979
{
8080
var metadata = chatClient.GetService<ChatClientMetadata>();
81-
serviceModelId = metadata?.ModelId;
81+
serviceModelId = metadata?.DefaultModelId;
8282
endpoint = metadata?.ProviderUri?.ToString();
8383
}
8484

‎dotnet/samples/GettingStartedWithAgents/Step06_DependencyInjection.cs‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,25 @@ public async Task UseDependencyInjectionToCreateAgentAsync(bool useChatClient)
4343
IChatClient chatClient;
4444
if (this.UseOpenAIConfig)
4545
{
46-
chatClient = new Microsoft.Extensions.AI.OpenAIChatClient(
47-
new OpenAI.OpenAIClient(TestConfiguration.OpenAI.ApiKey),
48-
TestConfiguration.OpenAI.ChatModelId);
46+
chatClient = new OpenAI.OpenAIClient(TestConfiguration.OpenAI.ApiKey)
47+
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
48+
.AsIChatClient();
4949
}
5050
else if (!string.IsNullOrEmpty(this.ApiKey))
5151
{
52-
chatClient = new Microsoft.Extensions.AI.OpenAIChatClient(
53-
openAIClient: new AzureOpenAIClient(
52+
chatClient = new AzureOpenAIClient(
5453
endpoint: new Uri(TestConfiguration.AzureOpenAI.Endpoint),
55-
credential: new ApiKeyCredential(TestConfiguration.AzureOpenAI.ApiKey)),
56-
modelId: TestConfiguration.AzureOpenAI.ChatModelId);
54+
credential: new ApiKeyCredential(TestConfiguration.AzureOpenAI.ApiKey))
55+
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
56+
.AsIChatClient();
5757
}
5858
else
5959
{
60-
chatClient = new Microsoft.Extensions.AI.OpenAIChatClient(
61-
openAIClient: new AzureOpenAIClient(
60+
chatClient = new AzureOpenAIClient(
6261
endpoint: new Uri(TestConfiguration.AzureOpenAI.Endpoint),
63-
credential: new AzureCliCredential()),
64-
modelId: TestConfiguration.AzureOpenAI.ChatModelId);
62+
credential: new AzureCliCredential())
63+
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
64+
.AsIChatClient();
6565
}
6666

6767
var functionCallingChatClient = chatClient!.AsKernelFunctionInvokingChatClient();

‎dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Connectors.OpenAI.UnitTests.csproj‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@
9696
<None Update="TestData\text-to-image-response.json">
9797
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
9898
</None>
99+
<None Update="TestData\filters_chatclient_multiple_function_calls_test_response.json">
100+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
101+
</None>
102+
<None Update="TestData\chat_completion_streaming_chatclient_multiple_function_calls_test_response.txt">
103+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
104+
</None>
105+
<None Update="TestData\filters_chatclient_streaming_multiple_function_calls_test_response.txt">
106+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
107+
</None>
99108
</ItemGroup>
100109

101110
</Project>

0 commit comments

Comments
 (0)