Skip to content
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

Advancing Tool Support - Part 3 #2121

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ThomasVitale
Copy link
Contributor

  • Introduced ToolCallingManager to manage the tool calling activities for resolving and executing tools. A default implementation is provided. It can be used to handle explicit tool execution on the client-side, superseding the previous FunctionCallingHelper class. It’s ready to be instrumented via Micrometer, and support exception handling when tool calls fail.
  • Introduced ToolCallExceptionConverter to handle exceptions in tool calling, and provided a default implementation propagating the error message to the chat morel.
  • Introduced ToolCallbackResolver to resolve ToolCallback instances. A default implementation is provided (DelegatingToolCallbackResolver), capable of delegating the resolution to a series of resolvers, including static resolution (StaticToolCallbackResolver) and dynamic resolution from the Spring context (SpringBeanToolCallbackResolver).
  • Improved configuration in ToolCallingChatOptions to enable/disable the tool execution within a ChatModel (superseding the previous proxyToolCalls option).
  • Added unit and integration tests to cover all the new use cases and existing functionality which was not covered by autotests (tool resolution from Spring context).
  • Deprecated FunctionCallbackResolver, AbstractToolCallSupport, and FunctionCallingHelper.

Relates to gh-2049

/**
* Merge the given {@link ChatOptions} into this instance.
*/
public ToolCallingChatOptions merge(ChatOptions options) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method would not by called by any class since we always use the logic in ModelOptionsUtil to merge options, so I removed this.

}

@Override
public String convert(ToolExecutionException exception) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this simple logic to handle tool execution errors. We can evolve this in separate PRs. Users have the possibility to provide their own handler.

* @author Thomas Vitale
* @since 1.0.0
*/
public class SpringBeanToolCallbackResolver implements ToolCallbackResolver {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's pretty much the same logic as DefaultFunctionCallbackResolver. I also added autotests since we didn't have any for this logic.


private final Map<String, ToolCallback> toolCallbacks = new HashMap<>();

public StaticToolCallbackResolver(List<ToolCallback> toolCallbacks) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the auto configuration, this can be populated by all the ToolCallbacks registered as beans in Spring (via @bean or @component).

import static org.assertj.core.api.Assertions.assertThat;

/**
* Unit tests for {@link ChatResponse}.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was surprised to see we didn't have any auto-tests for ChatResponse. It's worth adding them (in a separate task) while also making it null-safe, because there are a lot of paths ending in NullPointerException inside this class

*
* @author Thomas Vitale
*/
class SpringBeanToolCallbackResolverKotlinTests {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added extra tests to make sure the resolution via Spring beans works correctly also in Kotlin

runExplicitToolCallingExecutionWithOptionsStream(chatOptions, prompt);
}

private void runExplicitToolCallingExecutionWithOptions(ChatOptions chatOptions, Prompt prompt) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows how to do the tool execution on the client-side. It should be very straightforward using the new ToolCallingManager.

@Test
void explicitToolCallingExecutionWithLegacyOptions() {
ChatOptions chatOptions = FunctionCallingOptions.builder()
.functionCallbacks(ToolCallbacks.from(tools))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's backward compatible with the deprecated FunctionCallingOptions

* Introduced ToolCallingManager to manage the tool calling activities for resolving and executing tools. A default implementation is provided. It can be used to handle explicit tool execution on the client-side, superseding the previous FunctionCallingHelper class. It’s ready to be instrumented via Micrometer, and support exception handling when tool calls fail.
* Introduced ToolCallExceptionConverter to handle exceptions in tool calling, and provided a default implementation propagating the error message to the chat morel.
* Introduced ToolCallbackResolver to resolve ToolCallback instances. A default implementation is provided (DelegatingToolCallbackResolver), capable of delegating the resolution to a series of resolvers, including static resolution (StaticToolCallbackResolver) and dynamic resolution from the Spring context (SpringBeanToolCallbackResolver).
* Improved configuration in ToolCallingChatOptions to enable/disable the tool execution within a ChatModel (superseding the previous proxyToolCalls option).
* Added unit and integration tests to cover all the new use cases and existing functionality which was not covered by autotests (tool resolution from Spring context).
* Deprecated FunctionCallbackResolver, AbstractToolCallSupport, and FunctionCallingHelper.

Relates to spring-projectsgh-2049

Signed-off-by: Thomas Vitale <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants