From b68d05cdaa5f68105da401e8dd1a2343ca7829ec Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 8 Oct 2024 23:21:13 -0400 Subject: [PATCH 1/2] Flip default on FunctionInvokingChatClient.ConcurrentInvocation For better reliability, default ConcurrentInvocation to false, so that it doesn't introduce concurrency / parallelism where there wasn't any. --- .../FunctionInvokingChatClient.cs | 9 ++++---- .../FunctionInvokingChatClientTests.cs | 21 +++++++++++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs b/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs index c46d7f43156..d538b596475 100644 --- a/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs +++ b/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs @@ -82,15 +82,14 @@ public FunctionInvokingChatClient(IChatClient innerClient) /// /// /// An individual response from the inner client may contain multiple function call requests. - /// By default, such function calls may be issued to execute concurrently with each other. Set - /// to false to disable such concurrent invocation and force - /// the functions to be invoked serially. + /// By default, such function calls are processed serially. Set to + /// true to enable concurrent invocation such that multiple function calls may execute in parallel. /// /// - /// The default value is . + /// The default value is . /// /// - public bool ConcurrentInvocation { get; set; } = true; + public bool ConcurrentInvocation { get; set; } /// /// Gets or sets a value indicating whether to keep intermediate messages in the chat history. diff --git a/test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs b/test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs index 8ad0c6d7944..20780d968f7 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs @@ -12,6 +12,19 @@ namespace Microsoft.Extensions.AI; public class FunctionInvokingChatClientTests { + [Fact] + public void Ctor_HasExpectedDefaults() + { + using TestChatClient innerClient = new(); + using FunctionInvokingChatClient client = new(innerClient); + + Assert.False(client.ConcurrentInvocation); + Assert.False(client.DetailedErrors); + Assert.True(client.KeepFunctionCallingMessages); + Assert.Null(client.MaximumIterationsPerRequest); + Assert.False(client.RetryOnError); + } + [Fact] public async Task SupportsSingleFunctionCallPerRequestAsync() { @@ -71,7 +84,7 @@ await InvokeAndAssertAsync(options, [ } [Fact] - public async Task ParallelFunctionCallsInvokedConcurrentlyByDefaultAsync() + public async Task ParallelFunctionCallsMayBeInvokedConcurrentlyAsync() { using var barrier = new Barrier(2); @@ -97,11 +110,11 @@ await InvokeAndAssertAsync(options, [ new FunctionResultContent("callId2", "Func", result: "worldworld"), ]), new ChatMessage(ChatRole.Assistant, "done"), - ]); + ], configurePipeline: b => b.Use(s => new FunctionInvokingChatClient(s) { ConcurrentInvocation = true })); } [Fact] - public async Task ConcurrentInvocationOfParallelCallsCanBeDisabledAsync() + public async Task ConcurrentInvocationOfParallelCallsDisabledByDefaultAsync() { int activeCount = 0; @@ -130,7 +143,7 @@ await InvokeAndAssertAsync(options, [ new FunctionResultContent("callId2", "Func", result: "worldworld"), ]), new ChatMessage(ChatRole.Assistant, "done"), - ], configurePipeline: b => b.Use(s => new FunctionInvokingChatClient(s) { ConcurrentInvocation = false })); + ]); } [Theory] From cab42b09d2afb97f94d4e067b8473a72daf823b0 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 9 Oct 2024 00:06:24 -0400 Subject: [PATCH 2/2] Update src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs Co-authored-by: Igor Velikorossov --- .../ChatCompletion/FunctionInvokingChatClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs b/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs index d538b596475..16e9d62f25b 100644 --- a/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs +++ b/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs @@ -83,7 +83,7 @@ public FunctionInvokingChatClient(IChatClient innerClient) /// /// An individual response from the inner client may contain multiple function call requests. /// By default, such function calls are processed serially. Set to - /// true to enable concurrent invocation such that multiple function calls may execute in parallel. + /// to enable concurrent invocation such that multiple function calls may execute in parallel. /// /// /// The default value is .