Skip to content

Commit

Permalink
Flip default on FunctionInvokingChatClient.ConcurrentInvocation (#5485)
Browse files Browse the repository at this point in the history
* 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.

* Update src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs

Co-authored-by: Igor Velikorossov <RussKie@users.noreply.github.com>

---------

Co-authored-by: Igor Velikorossov <RussKie@users.noreply.github.com>
  • Loading branch information
stephentoub and RussKie authored Oct 9, 2024
1 parent 331ddb5 commit 0c8bc3e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,14 @@ public FunctionInvokingChatClient(IChatClient innerClient)
/// <remarks>
/// <para>
/// 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
/// <see cref="ConcurrentInvocation"/> to false to disable such concurrent invocation and force
/// the functions to be invoked serially.
/// By default, such function calls are processed serially. Set <see cref="ConcurrentInvocation"/> to
/// <see langword="true"/> to enable concurrent invocation such that multiple function calls may execute in parallel.
/// </para>
/// <para>
/// The default value is <see langword="true"/>.
/// The default value is <see langword="false"/>.
/// </para>
/// </remarks>
public bool ConcurrentInvocation { get; set; } = true;
public bool ConcurrentInvocation { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to keep intermediate messages in the chat history.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -71,7 +84,7 @@ await InvokeAndAssertAsync(options, [
}

[Fact]
public async Task ParallelFunctionCallsInvokedConcurrentlyByDefaultAsync()
public async Task ParallelFunctionCallsMayBeInvokedConcurrentlyAsync()
{
using var barrier = new Barrier(2);

Expand All @@ -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;

Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 0c8bc3e

Please sign in to comment.