-
Notifications
You must be signed in to change notification settings - Fork 348
Add ChatTools and ResponseTools helper classes #422
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
Changes from 1 commit
a0ec8a3
04b7069
294ed2d
d909b21
1dfadee
12e8a87
47f3e2d
93f94c6
8ab51f1
4309965
dcc655c
24f1821
a63b571
7bc969c
d8da69a
567ffa0
fe67356
385dd35
78e3e5f
f46fd40
4009c2b
5703fc0
76b907a
5ce1d02
c984bc8
de89187
869a455
6079911
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,7 @@ public ResponseTools(EmbeddingClient client = null) | |
| public ResponseTools(params Type[] tools) : this((EmbeddingClient)null) | ||
| { | ||
| foreach (var t in tools) | ||
| AddLocalTool(t); | ||
| AddFunctionTool(t); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -58,27 +58,27 @@ public ResponseTools(params Type[] tools) : this((EmbeddingClient)null) | |
| /// Adds local tool implementations from the provided types. | ||
| /// </summary> | ||
| /// <param name="tools">Types containing static methods to be used as tools.</param> | ||
| public void AddLocalTools(params Type[] tools) | ||
| public void AddFunctionTools(params Type[] tools) | ||
| { | ||
| foreach (Type functionHolder in tools) | ||
| AddLocalTool(functionHolder); | ||
| AddFunctionTool(functionHolder); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds all public static methods from the specified type as tools. | ||
| /// </summary> | ||
| /// <param name="tool">The type containing tool methods.</param> | ||
| internal void AddLocalTool(Type tool) | ||
| internal void AddFunctionTool(Type tool) | ||
| { | ||
| #pragma warning disable IL2070 | ||
| foreach (MethodInfo function in tool.GetMethods(BindingFlags.Public | BindingFlags.Static)) | ||
| { | ||
| AddLocalTool(function); | ||
| AddFunctionTool(function); | ||
| } | ||
| #pragma warning restore IL2070 | ||
| } | ||
|
|
||
| internal void AddLocalTool(MethodInfo function) | ||
| internal void AddFunctionTool(MethodInfo function) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is reinventing AIFunctionFactory from Microsoft.Extensions.AI, albeit with fewer features developers need. It'd be better to just use AIFunctionFactory. Static methods alone are insufficient to address real-world need. Developers need support instance methods, both on supplied instances and those manufactured from DI. Developers need to be able to inject state, such as satisfying parameters from dependency injection. Developers need to support flowing cancellation. Developers need support for complex parameters and return types, and with proper NativeAOT support. And so on.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would love to use some reusable library for this. But currently, we are not ready take a hard dependency on MEAI. Let's keep discussing how to solve the problem offline (not in this PR) |
||
| { | ||
| string name = function.Name; | ||
| var tool = ResponseTool.CreateFunctionTool(name, ToolsUtility.GetMethodDescription(function), ToolsUtility.BuildParametersJson(function.GetParameters())); | ||
|
|
@@ -97,7 +97,7 @@ public async Task AddMcpToolsAsync(McpClient client) | |
| _mcpClientsByEndpoint[client.Endpoint.AbsoluteUri] = client; | ||
| await client.StartAsync().ConfigureAwait(false); | ||
| BinaryData tools = await client.ListToolsAsync().ConfigureAwait(false); | ||
| await AddToolsAsync(tools, client).ConfigureAwait(false); | ||
| await AddMcpToolsAsync(tools, client).ConfigureAwait(false); | ||
| _mcpClients.Add(client); | ||
| } | ||
|
|
||
|
|
@@ -112,7 +112,7 @@ public async Task AddMcpToolsAsync(Uri mcpEndpoint) | |
| await AddMcpToolsAsync(client).ConfigureAwait(false); | ||
| } | ||
|
|
||
| private async Task AddToolsAsync(BinaryData toolDefinitions, McpClient client) | ||
| private async Task AddMcpToolsAsync(BinaryData toolDefinitions, McpClient client) | ||
| { | ||
| List<ResponseTool> toolsToVectorize = new(); | ||
| var parsedTools = ToolsUtility.ParseMcpToolDefinitions(toolDefinitions, client); | ||
|
|
@@ -161,7 +161,7 @@ private ResponseTool ParseToolDefinition(BinaryData data) | |
| /// Converts the tools collection to <see cref="ResponseCreationOptions"> configured with the tools contained in this instance.. | ||
| /// </summary> | ||
| /// <returns>A new ResponseCreationOptions containing all defined tools.</returns> | ||
| public ResponseCreationOptions CreateResponseOptions() | ||
| public ResponseCreationOptions ToResponseCreationOptions() | ||
| { | ||
| var options = new ResponseCreationOptions(); | ||
| foreach (var tool in _tools) | ||
|
|
@@ -176,10 +176,10 @@ public ResponseCreationOptions CreateResponseOptions() | |
| /// <param name="maxTools">The maximum number of tools to return. Default is 5.</param> | ||
| /// <param name="minVectorDistance">The similarity threshold for including tools. Default is 0.29.</param> | ||
| /// <returns>A new ResponseCreationOptions containing the most relevant tools.</returns> | ||
| public ResponseCreationOptions CreateResponseOptions(string prompt, int maxTools = 5, float minVectorDistance = 0.29f) | ||
| public ResponseCreationOptions ToResponseCreationOptions(string prompt, int maxTools = 5, float minVectorDistance = 0.29f) | ||
| { | ||
| if (!CanFilterTools) | ||
| return CreateResponseOptions(); | ||
| return ToResponseCreationOptions(); | ||
|
|
||
| var completionOptions = new ResponseCreationOptions(); | ||
| foreach (var tool in FindRelatedTools(false, prompt, maxTools, minVectorDistance).GetAwaiter().GetResult()) | ||
|
|
@@ -194,10 +194,10 @@ public ResponseCreationOptions CreateResponseOptions(string prompt, int maxTools | |
| /// <param name="maxTools">The maximum number of tools to return. Default is 5.</param> | ||
| /// <param name="minVectorDistance">The similarity threshold for including tools. Default is 0.29.</param> | ||
| /// <returns>A new ResponseCreationOptions containing the most relevant tools.</returns> | ||
| public async Task<ResponseCreationOptions> CreateResponseOptionsAsync(string prompt, int maxTools = 5, float minVectorDistance = 0.29f) | ||
| public async Task<ResponseCreationOptions> ToResponseCreationOptionsAsync(string prompt, int maxTools = 5, float minVectorDistance = 0.29f) | ||
| { | ||
| if (!CanFilterTools) | ||
| return CreateResponseOptions(); | ||
| return ToResponseCreationOptions(); | ||
|
|
||
| var completionOptions = new ResponseCreationOptions(); | ||
| foreach (var tool in await FindRelatedTools(true, prompt, maxTools, minVectorDistance).ConfigureAwait(false)) | ||
|
|
@@ -225,12 +225,6 @@ await ToolsUtility.GetEmbeddingAsync(_client, prompt).ConfigureAwait(false) : | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Implicitly converts ResponseTools to ResponseCreationOptions. | ||
| /// </summary> | ||
| /// <param name="tools">The ResponseTools instance to convert.</param> | ||
| public static implicit operator ResponseCreationOptions(ResponseTools tools) => tools.CreateResponseOptions(); | ||
|
|
||
| internal string CallLocal(FunctionCallResponseItem call) | ||
| { | ||
| List<object> arguments = new(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.