Skip to content

Commit 46d0b49

Browse files
Tool reduction (#6781)
1 parent 4a0bf09 commit 46d0b49

File tree

10 files changed

+1518
-1
lines changed

10 files changed

+1518
-1
lines changed

eng/packages/General.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
3030
<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" />
3131
<PackageVersion Include="System.Memory" Version="4.5.5" />
32+
<PackageVersion Include="System.Numerics.Tensors" Version="$(SystemNumericsTensorsVersion)" />
3233
<PackageVersion Include="System.Private.Uri" Version="4.3.2" />
3334
<PackageVersion Include="System.Runtime.Caching" Version="$(SystemRuntimeCachingVersion)" />
3435
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.0" />

eng/packages/TestOnly.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<PackageVersion Include="Polly.Testing" Version="8.4.2" />
2222
<PackageVersion Include="StrongNamer" Version="0.2.5" />
2323
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="$(SystemConfigurationConfigurationManagerVersion)" />
24-
<PackageVersion Include="System.Numerics.Tensors" Version="$(SystemNumericsTensorsVersion)" />
2524
<PackageVersion Include="Verify.Xunit" Version="28.15.0" />
2625
<PackageVersion Include="Xunit.Combinatorial" Version="1.6.24" />
2726
<PackageVersion Include="xunit.extensibility.execution" Version="$(XUnitVersion)" />
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace Microsoft.Extensions.AI;
10+
11+
/// <summary>
12+
/// Represents a strategy capable of selecting a reduced set of tools for a chat request.
13+
/// </summary>
14+
/// <remarks>
15+
/// A tool reduction strategy is invoked prior to sending a request to an underlying <see cref="IChatClient"/>,
16+
/// enabling scenarios where a large tool catalog must be trimmed to fit provider limits or to improve model
17+
/// tool selection quality.
18+
/// <para>
19+
/// The implementation should return a non-<see langword="null"/> enumerable. Returning the original
20+
/// <see cref="ChatOptions.Tools"/> instance indicates no change. Returning a different enumerable indicates
21+
/// the caller may replace the existing tool list.
22+
/// </para>
23+
/// </remarks>
24+
[Experimental("MEAI001")]
25+
public interface IToolReductionStrategy
26+
{
27+
/// <summary>
28+
/// Selects the tools that should be included for a specific request.
29+
/// </summary>
30+
/// <param name="messages">The chat messages for the request. This is an <see cref="IEnumerable{T}"/> to avoid premature materialization.</param>
31+
/// <param name="options">The chat options for the request (may be <see langword="null"/>).</param>
32+
/// <param name="cancellationToken">A token to observe cancellation.</param>
33+
/// <returns>
34+
/// A (possibly reduced) enumerable of <see cref="AITool"/> instances. Must never be <see langword="null"/>.
35+
/// Returning the same instance referenced by <paramref name="options"/>.<see cref="ChatOptions.Tools"/> signals no change.
36+
/// </returns>
37+
Task<IEnumerable<AITool>> SelectToolsForRequestAsync(
38+
IEnumerable<ChatMessage> messages,
39+
ChatOptions? options,
40+
CancellationToken cancellationToken = default);
41+
}

src/Libraries/Microsoft.Extensions.AI/Microsoft.Extensions.AI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" />
4545
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
4646
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
47+
<PackageReference Include="System.Numerics.Tensors" />
4748
</ItemGroup>
4849

4950
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Diagnostics.CodeAnalysis;
6+
using Microsoft.Shared.Diagnostics;
7+
8+
namespace Microsoft.Extensions.AI;
9+
10+
/// <summary>Extension methods for adding tool reduction middleware to a chat client pipeline.</summary>
11+
[Experimental("MEAI001")]
12+
public static class ChatClientBuilderToolReductionExtensions
13+
{
14+
/// <summary>
15+
/// Adds tool reduction to the chat client pipeline using the specified <paramref name="strategy"/>.
16+
/// </summary>
17+
/// <param name="builder">The chat client builder.</param>
18+
/// <param name="strategy">The reduction strategy.</param>
19+
/// <returns>The original builder for chaining.</returns>
20+
/// <exception cref="ArgumentNullException">If <paramref name="builder"/> or <paramref name="strategy"/> is <see langword="null"/>.</exception>
21+
/// <remarks>
22+
/// This should typically appear in the pipeline before function invocation middleware so that only the reduced tools
23+
/// are exposed to the underlying provider.
24+
/// </remarks>
25+
public static ChatClientBuilder UseToolReduction(this ChatClientBuilder builder, IToolReductionStrategy strategy)
26+
{
27+
_ = Throw.IfNull(builder);
28+
_ = Throw.IfNull(strategy);
29+
30+
return builder.Use(inner => new ToolReducingChatClient(inner, strategy));
31+
}
32+
}

0 commit comments

Comments
 (0)