-
Notifications
You must be signed in to change notification settings - Fork 929
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
[DRAFT] Advancing Tool Support in Spring AI #2049
Comments
ThomasVitale
added a commit
to ThomasVitale/spring-ai
that referenced
this issue
Jan 11, 2025
* Defined new APIs consolidating the “tool” naming as opposed to the current “function”, aligning with the industry and solving the confusion between “function tool” and “Java Function”: ToolCallback and ToolCallingChatOptions. They extend the current ones to ensure backward compatibility, but FunctionCallback and FunctionCallingOptions can be considered deprecated. * Enhanced support for methods as tools, introducing support for declarative Tool-annotated methods via MethodToolCallback and MethodToolCallbackProvider (deprecating the existing MethodInvokingFunctionCallback). * Improved tool execution logic with granular support for returning the result directly to the client and exception handling. * Improved JSON Schema generation and parsing logic, consolidating the usage of the victools/jsonschema-generator library and dropping the non-maintained Jackson JSON Schema Module. This makes it possible to use tools with input lists/arrays, which the latter library was not supporting. * Extended ChatClient API with new methods tools() and toolCallbacks(). The existing functions() methods can be considered deprecated. Handles spring-projectsgh-2049 Signed-off-by: Thomas Vitale <[email protected]>
ThomasVitale
added a commit
to ThomasVitale/spring-ai
that referenced
this issue
Jan 12, 2025
* Defined new APIs consolidating the “tool” naming as opposed to the current “function”, aligning with the industry and solving the confusion between “function tool” and “Java Function”: ToolCallback and ToolCallingChatOptions. They extend the current ones to ensure backward compatibility, but FunctionCallback and FunctionCallingOptions can be considered deprecated. * Enhanced support for methods as tools, introducing support for declarative Tool-annotated methods via MethodToolCallback and MethodToolCallbackProvider (deprecating the existing MethodInvokingFunctionCallback). * Improved tool execution logic with granular support for returning the result directly to the client and exception handling. * Improved JSON Schema generation and parsing logic, consolidating the usage of the victools/jsonschema-generator library and dropping the non-maintained Jackson JSON Schema Module. This makes it possible to use tools with input lists/arrays, which the latter library was not supporting. * Extended ChatClient API with new methods tools() and toolCallbacks(). The existing functions() methods can be considered deprecated. Relates to spring-projectsgh-2049 Signed-off-by: Thomas Vitale <[email protected]>
ThomasVitale
added a commit
to ThomasVitale/spring-ai
that referenced
this issue
Jan 12, 2025
* Defined new APIs consolidating the “tool” naming as opposed to the current “function”, aligning with the industry and solving the confusion between “function tool” and “Java Function”: ToolCallback and ToolCallingChatOptions. They extend the current ones to ensure backward compatibility, but FunctionCallback and FunctionCallingOptions can be considered deprecated. * Enhanced support for methods as tools, introducing support for declarative Tool-annotated methods via MethodToolCallback and MethodToolCallbackProvider (deprecating the existing MethodInvokingFunctionCallback). * Improved tool execution logic with granular support for returning the result directly to the client and exception handling. * Improved JSON Schema generation and parsing logic, consolidating the usage of the victools/jsonschema-generator library and dropping the non-maintained Jackson JSON Schema Module. This makes it possible to use tools with input lists/arrays, which the latter library was not supporting. * Extended ChatClient API with new methods tools() and toolCallbacks(). The existing functions() methods can be considered deprecated. Relates to spring-projectsgh-2049 Signed-off-by: Thomas Vitale <[email protected]>
This would make tooling great ! |
Is there a plan to support Function parameters properties description? to make ai better understand how to get the corresponding parameters from the context. From openai doc, there is a description field for each function parameters property |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This issue presents ideas and suggestions for consolidating and advancing tool support in Spring AI, before reaching the first GA version.
Context
When talking about tool support, we can identify two main steps: tool registration and tool invocation.
Tool Registration
The goal of this step is to provide all the necessary information for the chat model to decide if and when to call tools, and for the client application to know how to invoke them.
Tool Metadata
When registering a tool, the following metadata are required because they are the ones used by a chat model:
Optionally, the following information can be provided as well:
Tool Source
Methods
In the context of Java, a tool can naturally be modelled as a method.
Objects
In special cases, there might be a wish for modelling tools as objects and register one of their methods for the invocation. One example would when using functional objects (
Function
/BiFunction
/Supplier
/Consumer
) as tool sources.Tool Invocation
The goal of this step is to execute the tool identified by a chat model, using the input provided by the model itself.
Observability is an important requirement for this step.
Building The Input
When a chat model determines that a certain tool needs to be called, it provides the tool identifier (the Name defined in the Tool Registration step) and the parameters to call it (compliant with the Input Type Schema defined in the Tool Registration step).
The client application is responsible for building the Java arguments to call the tool based on the input string provided by the model.
Executing The Tool
The client application is responsible for executing the tool, i.e. running the method defined in the Tool Registration step.
The execution should happen based on the Execution mode defined in the Tool Registration step: sync/async, blocking/non-blocking, virtual threads/reactive.
Parsing The Tool Call Result
The client application is responsible for parsing the tool call result based on the response format logic provided in the Tool Registration step, if any, or based on a default strategy.
Concluding The Tool Call
The client application is responsible for sending the tool call result to the chat model or return it directly to the user, if the Return Direct option was enabled in the Tool Registration step.
Design
I'd like to suggest a few changes to the current solution to support the concepts expressed in the previous sections.
I have drafted some of these already, you can check out the following material.
1. Naming
Currently, there is some mixed strategy in naming tool-related APIs. In some cases we use the term function, in other cases we use the term tool. Furthermore, when saying function, it could mean a tool or it could mean a Java Function. I suggest adopting the tool term in all tool-related APIs. That would avoid confusion (is it a generic tool or is it a Java function?) and would align with the naming adopted by most GenAI frameworks.
2. Tool Definition
I would evolve the current
FunctionCallback
API to aToolCallback
API with the following API. This new API would combine two responsibilities: registering the tool metadata and the invocation handling logic. For now, I'll skip the sync/async aspect that requires a dedicated discussion.And the
ToolMetadata
API:3. Schema Generation
I have already covered this topic in #2038, where I also suggest a possible solution.
4. Tool Registration
Let's consider two targets for tool registrations: methods and functional objects (
Function
/BiFunction
/Supplier
/Consumer
).In general, I would introduce a new
ToolCallbackProvider
API to abstract away the specifics of how tools are sourced and support handling more than one tool. That eliminates the need for aFunctionCallback.Builder
abstraction, which is hard to maintain considering the very different steps to register tools from different sources.For example, there could be a
MethodToolCallbackProvider
and anMcpToolCallbackProvider
.Methods
Methods should be the primary way of registering methods, and it should be possible to do it in a type-safe manner. I suggest introducing a
@Tool
annotation to mark methods in a class that can be made available to chat models.It should be possible to generate
ToolCallback
instances from@Tool
-annotated methods automatically, no matter if they are public, private, protected or package-scoped.@Tool
-annotated methods can be processed directly and for each of them aToolCallback
instance is created internally. For example, given amyTools
instance of theMyTools
class, I can pass it when calling a model viaChatClient
or viaChatModel
.@Tool
would be used to generateToolCallback
instances internally.MyTools.class
could also be processed at build-time, and the relatedToolCallback
instances made available as a "tool catalog" throughout the application lifecycle. In that case, it would also be possible to refer to a tool by its name, and under the hood it would be processed similarly to the previous step.Functional Objects
It should be possible to generate
ToolCallback
instances from functional objects (Function
/BiFunction
/Supplier
/Consumer
), no matter if they are defined in a configuration class with the@Bean
annotation or as@Component
-annotated classes.ToolCallback
is already available to a chat model with the given name (no matter the source, being method or functional object). If not, it would try to retrieve a bean from the Spring context named like the tool.4. Tool Invocation
To be continued...
The text was updated successfully, but these errors were encountered: