feat(autoconfigure): Support both FunctionCallback and ToolCallback in ToolCallingAutoConfiguration
- Extends the ToolCallingAutoConfiguration to support both FunctionCallback and ToolCallback types. - The toolCallbackResolver bean now handles both callback types through ObjectProvider injection. - Added comprehensive tests to verify the resolution of multiple function and tool callbacks. - Introduce new StaticToolCallbackProvider implementation - Update ToolCallbackProvider to return FunctionCallback[] - Migrate from List to ToolCallbackProvider in configurations - Update tests to use new provider pattern - Enhance tool callback providers to support multiple clients - Refactor AsyncMcpToolCallbackProvider and SyncMcpToolCallbackProvider to handle multiple MCP clients - Add ToolCallbackProvider support to ChatClient API - Deprecate direct tool callback list methods in favor of providers - Fix typos in Closeable class names - Update MCP documentation with new examples and usage patterns Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
@@ -36,6 +36,7 @@ import org.springframework.ai.converter.StructuredOutputConverter;
|
||||
import org.springframework.ai.model.Media;
|
||||
import org.springframework.ai.model.function.FunctionCallback;
|
||||
import org.springframework.ai.tool.ToolCallback;
|
||||
import org.springframework.ai.tool.ToolCallbackProvider;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -223,6 +224,8 @@ public interface ChatClient {
|
||||
|
||||
ChatClientRequestSpec tools(Object... toolObjects);
|
||||
|
||||
ChatClientRequestSpec tools(ToolCallbackProvider... toolCallbackProviders);
|
||||
|
||||
@Deprecated
|
||||
<I, O> ChatClientRequestSpec functions(FunctionCallback... functionCallbacks);
|
||||
|
||||
@@ -290,6 +293,8 @@ public interface ChatClient {
|
||||
|
||||
Builder defaultTools(Object... toolObjects);
|
||||
|
||||
Builder defaultTools(ToolCallbackProvider... toolCallbackProviders);
|
||||
|
||||
/**
|
||||
* @deprecated in favor of {@link #defaultTools(String...)}
|
||||
*/
|
||||
|
||||
@@ -35,6 +35,7 @@ import io.micrometer.observation.ObservationRegistry;
|
||||
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
|
||||
|
||||
import org.springframework.ai.tool.ToolCallback;
|
||||
import org.springframework.ai.tool.ToolCallbackProvider;
|
||||
import org.springframework.ai.tool.ToolCallbacks;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
@@ -871,6 +872,16 @@ public class DefaultChatClient implements ChatClient {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatClientRequestSpec tools(ToolCallbackProvider... toolCallbackProviders) {
|
||||
Assert.notNull(toolCallbackProviders, "toolCallbackProviders cannot be null");
|
||||
Assert.noNullElements(toolCallbackProviders, "toolCallbackProviders cannot contain null elements");
|
||||
for (ToolCallbackProvider toolCallbackProvider : toolCallbackProviders) {
|
||||
this.functionCallbacks.addAll(List.of(toolCallbackProvider.getToolCallbacks()));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Deprecated // Use tools()
|
||||
public ChatClientRequestSpec functions(String... functionBeanNames) {
|
||||
return tools(functionBeanNames);
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.springframework.ai.chat.model.ToolContext;
|
||||
import org.springframework.ai.chat.prompt.ChatOptions;
|
||||
import org.springframework.ai.model.function.FunctionCallback;
|
||||
import org.springframework.ai.tool.ToolCallback;
|
||||
import org.springframework.ai.tool.ToolCallbackProvider;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -173,6 +174,12 @@ public class DefaultChatClientBuilder implements Builder {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder defaultTools(ToolCallbackProvider... toolCallbackProviders) {
|
||||
this.defaultRequest.tools(toolCallbackProviders);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Deprecated // Use defaultTools()
|
||||
public <I, O> Builder defaultFunction(String name, String description, java.util.function.Function<I, O> function) {
|
||||
this.defaultRequest
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2025 - 2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.ai.tool;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ai.model.function.FunctionCallback;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A simple implementation of {@link ToolCallbackProvider} that maintains a static array
|
||||
* of {@link FunctionCallback} objects. This provider is immutable after construction and
|
||||
* provides a straightforward way to supply a fixed set of tool callbacks to AI models.
|
||||
*
|
||||
* <p>
|
||||
* This implementation is thread-safe as it maintains an immutable array of callbacks that
|
||||
* is set during construction and cannot be modified afterwards.
|
||||
*
|
||||
* <p>
|
||||
* Example usage: <pre>{@code
|
||||
* FunctionCallback callback1 = new MyFunctionCallback();
|
||||
* FunctionCallback callback2 = new AnotherFunctionCallback();
|
||||
*
|
||||
* // Create provider with varargs constructor
|
||||
* ToolCallbackProvider provider1 = new StaticToolCallbackProvider(callback1, callback2);
|
||||
*
|
||||
* // Or create provider with List constructor
|
||||
* List<FunctionCallback> callbacks = Arrays.asList(callback1, callback2);
|
||||
* ToolCallbackProvider provider2 = new StaticToolCallbackProvider(callbacks);
|
||||
* }</pre>
|
||||
*
|
||||
* @author Christian Tzolov
|
||||
* @since 1.0.0
|
||||
* @see ToolCallbackProvider
|
||||
* @see FunctionCallback
|
||||
*/
|
||||
public class StaticToolCallbackProvider implements ToolCallbackProvider {
|
||||
|
||||
private final FunctionCallback[] toolCallbacks;
|
||||
|
||||
/**
|
||||
* Constructs a new StaticToolCallbackProvider with the specified array of function
|
||||
* callbacks.
|
||||
* @param toolCallbacks the array of function callbacks to be provided by this
|
||||
* provider. Must not be null, though an empty array is permitted.
|
||||
* @throws IllegalArgumentException if the toolCallbacks array is null
|
||||
*/
|
||||
public StaticToolCallbackProvider(FunctionCallback... toolCallbacks) {
|
||||
Assert.notNull(toolCallbacks, "ToolCallbacks must not be null");
|
||||
this.toolCallbacks = toolCallbacks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new StaticToolCallbackProvider with the specified list of function
|
||||
* callbacks. The list is converted to an array internally.
|
||||
* @param toolCallbacks the list of function callbacks to be provided by this
|
||||
* provider. Must not be null and must not contain null elements.
|
||||
* @throws IllegalArgumentException if the toolCallbacks list is null or contains null
|
||||
* elements
|
||||
*/
|
||||
public StaticToolCallbackProvider(List<? extends FunctionCallback> toolCallbacks) {
|
||||
Assert.noNullElements(toolCallbacks, "toolCallbacks cannot contain null elements");
|
||||
this.toolCallbacks = toolCallbacks.toArray(new FunctionCallback[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the array of function callbacks held by this provider.
|
||||
* @return an array containing all function callbacks provided during construction.
|
||||
* The returned array is a direct reference to the internal array, as the callbacks
|
||||
* are expected to be immutable.
|
||||
*/
|
||||
@Override
|
||||
public FunctionCallback[] getToolCallbacks() {
|
||||
return this.toolCallbacks;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.ai.tool;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ai.model.function.FunctionCallback;
|
||||
|
||||
/**
|
||||
* Provides {@link ToolCallback} instances for tools defined in different sources.
|
||||
*
|
||||
@@ -24,6 +28,14 @@ package org.springframework.ai.tool;
|
||||
*/
|
||||
public interface ToolCallbackProvider {
|
||||
|
||||
ToolCallback[] getToolCallbacks();
|
||||
FunctionCallback[] getToolCallbacks();
|
||||
|
||||
public static ToolCallbackProvider from(List<? extends FunctionCallback> toolCallbacks) {
|
||||
return new StaticToolCallbackProvider(toolCallbacks);
|
||||
}
|
||||
|
||||
public static ToolCallbackProvider from(FunctionCallback... toolCallbacks) {
|
||||
return new StaticToolCallbackProvider(toolCallbacks);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user