Flip default on FunctionInvokingChatClient.ConcurrentInvocation (#5485)

* 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>
This commit is contained in:
Stephen Toub 2024-10-09 06:54:43 -04:00 коммит произвёл GitHub
Родитель 331ddb5bb5
Коммит 0c8bc3ea38
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 21 добавлений и 9 удалений

Просмотреть файл

@ -82,15 +82,14 @@ public class FunctionInvokingChatClient : DelegatingChatClient
/// <remarks> /// <remarks>
/// <para> /// <para>
/// An individual response from the inner client may contain multiple function call requests. /// 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 /// By default, such function calls are processed serially. Set <see cref="ConcurrentInvocation"/> to
/// <see cref="ConcurrentInvocation"/> to false to disable such concurrent invocation and force /// <see langword="true"/> to enable concurrent invocation such that multiple function calls may execute in parallel.
/// the functions to be invoked serially.
/// </para> /// </para>
/// <para> /// <para>
/// The default value is <see langword="true"/>. /// The default value is <see langword="false"/>.
/// </para> /// </para>
/// </remarks> /// </remarks>
public bool ConcurrentInvocation { get; set; } = true; public bool ConcurrentInvocation { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether to keep intermediate messages in the chat history. /// Gets or sets a value indicating whether to keep intermediate messages in the chat history.

Просмотреть файл

@ -12,6 +12,19 @@ namespace Microsoft.Extensions.AI;
public class FunctionInvokingChatClientTests 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] [Fact]
public async Task SupportsSingleFunctionCallPerRequestAsync() public async Task SupportsSingleFunctionCallPerRequestAsync()
{ {
@ -71,7 +84,7 @@ public class FunctionInvokingChatClientTests
} }
[Fact] [Fact]
public async Task ParallelFunctionCallsInvokedConcurrentlyByDefaultAsync() public async Task ParallelFunctionCallsMayBeInvokedConcurrentlyAsync()
{ {
using var barrier = new Barrier(2); using var barrier = new Barrier(2);
@ -97,11 +110,11 @@ public class FunctionInvokingChatClientTests
new FunctionResultContent("callId2", "Func", result: "worldworld"), new FunctionResultContent("callId2", "Func", result: "worldworld"),
]), ]),
new ChatMessage(ChatRole.Assistant, "done"), new ChatMessage(ChatRole.Assistant, "done"),
]); ], configurePipeline: b => b.Use(s => new FunctionInvokingChatClient(s) { ConcurrentInvocation = true }));
} }
[Fact] [Fact]
public async Task ConcurrentInvocationOfParallelCallsCanBeDisabledAsync() public async Task ConcurrentInvocationOfParallelCallsDisabledByDefaultAsync()
{ {
int activeCount = 0; int activeCount = 0;
@ -130,7 +143,7 @@ public class FunctionInvokingChatClientTests
new FunctionResultContent("callId2", "Func", result: "worldworld"), new FunctionResultContent("callId2", "Func", result: "worldworld"),
]), ]),
new ChatMessage(ChatRole.Assistant, "done"), new ChatMessage(ChatRole.Assistant, "done"),
], configurePipeline: b => b.Use(s => new FunctionInvokingChatClient(s) { ConcurrentInvocation = false })); ]);
} }
[Theory] [Theory]