Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7780060

Browse files
committed
Add FunctionInvokingChatClient.FunctionInvoker delegate (#6564)
We've had a bunch of requests to be able to customize how function invocation is handled, and while it's already possible today by deriving from FunctionInvokingChatClient and overriding its InvokeFunctionAsync, there's a lot of ceremony involved in that. By having a property on the client instance, that behavior can instead be configured as part of a UseFunctionInvocation call.
1 parent498c427 commit7780060

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

‎src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ public int MaximumConsecutiveErrorsPerRequest
205205
set=>_maximumConsecutiveErrorsPerRequest=Throw.IfLessThan(value,0);
206206
}
207207

208+
/// <summary>Gets or sets a delegate used to invoke <see cref="AIFunction"/> instances.</summary>
209+
/// <remarks>
210+
/// By default, the protected <see cref="InvokeFunctionAsync"/> method is called for each <see cref="AIFunction"/> to be invoked,
211+
/// invoking the instance and returning its result. If this delegate is set to a non-<see langword="null"/> value,
212+
/// <see cref="InvokeFunctionAsync"/> will replace its normal invocation with a call to this delegate, enabling
213+
/// this delegate to assume all invocation handling of the function.
214+
/// </remarks>
215+
publicFunc<FunctionInvocationContext,CancellationToken,ValueTask<object?>>?FunctionInvoker{get;set;}
216+
208217
/// <inheritdoc/>
209218
publicoverrideasyncTask<ChatResponse>GetResponseAsync(
210219
IEnumerable<ChatMessage>messages,ChatOptions?options=null,CancellationTokencancellationToken=default)
@@ -872,7 +881,9 @@ FunctionResultContent CreateFunctionResultContent(FunctionInvocationResult resul
872881
{
873882
_=Throw.IfNull(context);
874883

875-
returncontext.Function.InvokeAsync(context.Arguments,cancellationToken);
884+
returnFunctionInvokeris{}invoker?
885+
invoker(context,cancellationToken):
886+
context.Function.InvokeAsync(context.Arguments,cancellationToken);
876887
}
877888

878889
privatestaticTimeSpanGetElapsedTime(longstartingTimestamp)=>

‎src/Libraries/Microsoft.Extensions.AI/Microsoft.Extensions.AI.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,10 @@
527527
"Member":"System.IServiceProvider? Microsoft.Extensions.AI.FunctionInvokingChatClient.FunctionInvocationServices { get; }",
528528
"Stage":"Stable"
529529
},
530+
{
531+
"Member":"System.Func<Microsoft.Extensions.AI.FunctionInvocationContext, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<object?>>? Microsoft.Extensions.AI.FunctionInvokingChatClient.FunctionInvoker { get; set; }",
532+
"Stage":"Stable"
533+
},
530534
{
531535
"Member":"bool Microsoft.Extensions.AI.FunctionInvokingChatClient.IncludeDetailedErrors { get; set; }",
532536
"Stage":"Stable"

‎test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
usingSystem.Collections.Generic;
77
usingSystem.Diagnostics;
88
usingSystem.Linq;
9+
usingSystem.Text.Json;
910
usingSystem.Threading;
1011
usingSystem.Threading.Tasks;
1112
usingMicrosoft.Extensions.DependencyInjection;
@@ -37,6 +38,35 @@ public void Ctor_HasExpectedDefaults()
3738
Assert.False(client.IncludeDetailedErrors);
3839
Assert.Equal(10,client.MaximumIterationsPerRequest);
3940
Assert.Equal(3,client.MaximumConsecutiveErrorsPerRequest);
41+
Assert.Null(client.FunctionInvoker);
42+
}
43+
44+
[Fact]
45+
publicvoidProperties_Roundtrip()
46+
{
47+
usingTestChatClientinnerClient=new();
48+
usingFunctionInvokingChatClientclient=new(innerClient);
49+
50+
Assert.False(client.AllowConcurrentInvocation);
51+
client.AllowConcurrentInvocation=true;
52+
Assert.True(client.AllowConcurrentInvocation);
53+
54+
Assert.False(client.IncludeDetailedErrors);
55+
client.IncludeDetailedErrors=true;
56+
Assert.True(client.IncludeDetailedErrors);
57+
58+
Assert.Equal(10,client.MaximumIterationsPerRequest);
59+
client.MaximumIterationsPerRequest=5;
60+
Assert.Equal(5,client.MaximumIterationsPerRequest);
61+
62+
Assert.Equal(3,client.MaximumConsecutiveErrorsPerRequest);
63+
client.MaximumConsecutiveErrorsPerRequest=1;
64+
Assert.Equal(1,client.MaximumConsecutiveErrorsPerRequest);
65+
66+
Assert.Null(client.FunctionInvoker);
67+
Func<FunctionInvocationContext,CancellationToken,ValueTask<object?>>invoker=(ctx,ct)=>newValueTask<object?>("test");
68+
client.FunctionInvoker=invoker;
69+
Assert.Same(invoker,client.FunctionInvoker);
4070
}
4171

4272
[Fact]
@@ -208,6 +238,49 @@ public async Task ConcurrentInvocationOfParallelCallsDisabledByDefaultAsync()
208238
awaitInvokeAndAssertStreamingAsync(options,plan);
209239
}
210240

241+
[Fact]
242+
publicasyncTaskFunctionInvokerDelegateOverridesHandlingAsync()
243+
{
244+
varoptions=newChatOptions
245+
{
246+
Tools=
247+
[
248+
AIFunctionFactory.Create(()=>"Result 1","Func1"),
249+
AIFunctionFactory.Create((inti)=>$"Result 2:{i}","Func2"),
250+
AIFunctionFactory.Create((inti)=>{},"VoidReturn"),
251+
]
252+
};
253+
254+
List<ChatMessage>plan=
255+
[
256+
newChatMessage(ChatRole.User,"hello"),
257+
newChatMessage(ChatRole.Assistant,[newFunctionCallContent("callId1","Func1")]),
258+
newChatMessage(ChatRole.Tool,[newFunctionResultContent("callId1",result:"Result 1 from delegate")]),
259+
newChatMessage(ChatRole.Assistant,[newFunctionCallContent("callId2","Func2",arguments:newDictionary<string,object?>{{"i",42}})]),
260+
newChatMessage(ChatRole.Tool,[newFunctionResultContent("callId2",result:"Result 2: 42 from delegate")]),
261+
newChatMessage(ChatRole.Assistant,[newFunctionCallContent("callId3","VoidReturn",arguments:newDictionary<string,object?>{{"i",43}})]),
262+
newChatMessage(ChatRole.Tool,[newFunctionResultContent("callId3",result:"Success: Function completed.")]),
263+
newChatMessage(ChatRole.Assistant,"world"),
264+
];
265+
266+
Func<ChatClientBuilder,ChatClientBuilder>configure= b=>b.Use(
267+
s=>newFunctionInvokingChatClient(s)
268+
{
269+
FunctionInvoker=async(ctx,cancellationToken)=>
270+
{
271+
Assert.NotNull(ctx);
272+
varresult=awaitctx.Function.InvokeAsync(ctx.Arguments,cancellationToken);
273+
returnresultisJsonElemente?
274+
JsonSerializer.SerializeToElement($"{e.GetString()} from delegate",AIJsonUtilities.DefaultOptions):
275+
result;
276+
}
277+
});
278+
279+
awaitInvokeAndAssertAsync(options,plan,configurePipeline:configure);
280+
281+
awaitInvokeAndAssertStreamingAsync(options,plan,configurePipeline:configure);
282+
}
283+
211284
[Fact]
212285
publicasyncTaskContinuesWithSuccessfulCallsUntilMaximumIterations()
213286
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp