-
Notifications
You must be signed in to change notification settings - Fork 841
Tool reduction #6781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Tool reduction #6781
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ace3d1d
[WIP] Basic prototype
MackinnonBuck c7608c1
Some refinements + more tests
MackinnonBuck a238349
Remove usage of 'field'
MackinnonBuck 06cedcc
Update System.Linq.Async version
MackinnonBuck 56f4bdd
Don't handle `OperationCanceledException`
MackinnonBuck b2583ec
More PR feedback
MackinnonBuck 491ff72
PR feedback + other improvements
MackinnonBuck d05b989
Merge remote-tracking branch 'origin/main' into mbuck/tool-reduction
MackinnonBuck b3f7f85
Enable asynchronous chat history embedding text generation
MackinnonBuck 62d1ae9
Merge branch 'main' into mbuck/tool-reduction
MackinnonBuck 1b4661d
Update test/Libraries/Microsoft.Extensions.AI.Integration.Tests/ChatC…
MackinnonBuck c73014c
Fix System.Numerics.Tensors dependency
MackinnonBuck 9540572
Revert "Fix System.Numerics.Tensors dependency"
MackinnonBuck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/Libraries/Microsoft.Extensions.AI.Abstractions/ToolReduction/IToolReductionStrategy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.Extensions.AI; | ||
|
|
||
| /// <summary> | ||
| /// Represents a strategy capable of selecting a reduced set of tools for a chat request. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// A tool reduction strategy is invoked prior to sending a request to an underlying <see cref="IChatClient"/>, | ||
| /// enabling scenarios where a large tool catalog must be trimmed to fit provider limits or to improve model | ||
| /// tool selection quality. | ||
| /// <para> | ||
| /// The implementation should return a non-<see langword="null"/> enumerable. Returning the original | ||
| /// <see cref="ChatOptions.Tools"/> instance indicates no change. Returning a different enumerable indicates | ||
| /// the caller may replace the existing tool list. | ||
| /// </para> | ||
| /// </remarks> | ||
| [Experimental("MEAI001")] | ||
| public interface IToolReductionStrategy | ||
| { | ||
| /// <summary> | ||
| /// Selects the tools that should be included for a specific request. | ||
| /// </summary> | ||
| /// <param name="messages">The chat messages for the request. This is an <see cref="IEnumerable{T}"/> to avoid premature materialization.</param> | ||
| /// <param name="options">The chat options for the request (may be <see langword="null"/>).</param> | ||
| /// <param name="cancellationToken">A token to observe cancellation.</param> | ||
| /// <returns> | ||
| /// A (possibly reduced) enumerable of <see cref="AITool"/> instances. Must never be <see langword="null"/>. | ||
| /// Returning the same instance referenced by <paramref name="options"/>.<see cref="ChatOptions.Tools"/> signals no change. | ||
| /// </returns> | ||
| Task<IEnumerable<AITool>> SelectToolsForRequestAsync( | ||
| IEnumerable<ChatMessage> messages, | ||
| ChatOptions? options, | ||
| CancellationToken cancellationToken = default); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...braries/Microsoft.Extensions.AI/ToolReduction/ChatClientBuilderToolReductionExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Shared.Diagnostics; | ||
|
|
||
| namespace Microsoft.Extensions.AI; | ||
|
|
||
| /// <summary>Extension methods for adding tool reduction middleware to a chat client pipeline.</summary> | ||
| [Experimental("MEAI001")] | ||
| public static class ChatClientBuilderToolReductionExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds tool reduction to the chat client pipeline using the specified <paramref name="strategy"/>. | ||
| /// </summary> | ||
| /// <param name="builder">The chat client builder.</param> | ||
| /// <param name="strategy">The reduction strategy.</param> | ||
| /// <returns>The original builder for chaining.</returns> | ||
| /// <exception cref="ArgumentNullException">If <paramref name="builder"/> or <paramref name="strategy"/> is <see langword="null"/>.</exception> | ||
| /// <remarks> | ||
| /// This should typically appear in the pipeline before function invocation middleware so that only the reduced tools | ||
| /// are exposed to the underlying provider. | ||
| /// </remarks> | ||
| public static ChatClientBuilder UseToolReduction(this ChatClientBuilder builder, IToolReductionStrategy strategy) | ||
| { | ||
| _ = Throw.IfNull(builder); | ||
| _ = Throw.IfNull(strategy); | ||
|
|
||
| return builder.Use(inner => new ToolReducingChatClient(inner, strategy)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.