Class ToolService
java.lang.Object
dev.langchain4j.service.tool.ToolService
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidafterToolExecution(Consumer<ToolExecution> afterToolExecution) applyToolHallucinationStrategy(ToolExecutionRequest toolRequest) voidvoidbeforeToolExecution(Consumer<BeforeToolExecution> beforeToolExecution) createContext(InvocationContext invocationContext, UserMessage userMessage, List<ChatMessage> messages) executeInferenceAndToolsLoop(AiServiceContext context, Object memoryId, ChatResponse chatResponse, ChatRequestParameters parameters, List<ChatMessage> messages, ChatMemory chatMemory, InvocationContext invocationContext, ToolServiceContext toolServiceContext, boolean isReturnTypeResult) executeTool(InvocationContext invocationContext, Map<String, ToolExecutor> toolExecutors, ToolExecutionRequest toolRequest, Consumer<BeforeToolExecution> externalBeforeToolExecution, Consumer<ToolExecution> externalAfterToolExecution) voidvoidexecuteToolsConcurrently(Executor executor) static ToolExecutionResultexecuteWithErrorHandling(ToolExecutionRequest toolRequest, ToolExecutor toolExecutor, InvocationContext invocationContext, ToolArgumentsErrorHandler argumentsErrorHandler, ToolExecutionErrorHandler executionErrorHandler) voidexecutor()static List<AiServiceTool> Scans the given object for@Tool-annotated methods and returns a list ofAiServiceTools.voidhallucinatedToolNameStrategy(Function<ToolExecutionRequest, ToolExecutionResultMessage> toolHallucinationStrategy) booleanisImmediateTool(String toolName) intvoidmaxSequentialToolsInvocations(int maxSequentialToolsInvocations) static ToolServiceContextrefreshDynamicProviders(ToolServiceContext toolServiceContext, List<ChatMessage> messages, InvocationContext invocationContext) Re-evaluates dynamic tool providers and returns an updatedToolServiceContextwith any newly provided tool specifications and executors.Deprecated.voidtoolProvider(ToolProvider toolProvider) voidtoolProviders(Collection<ToolProvider> toolProviders) voidtools(Collection<Object> objectsWithTools) voidtools(Map<ToolSpecification, ToolExecutor> tools) voidtools(Map<ToolSpecification, ToolExecutor> tools, Set<String> immediateReturnToolNames) voidtoolSearchStrategy(ToolSearchStrategy toolSearchStrategy)
-
Constructor Details
-
ToolService
public ToolService()
-
-
Method Details
-
hallucinatedToolNameStrategy
public void hallucinatedToolNameStrategy(Function<ToolExecutionRequest, ToolExecutionResultMessage> toolHallucinationStrategy) -
toolProvider
-
toolProviders
- Since:
- 1.12.0
-
tools
-
tools
-
tools
-
findTools
Scans the given object for@Tool-annotated methods and returns a list ofAiServiceTools.- Parameters:
objectWithTools- an object containing@Tool-annotated methods- Returns:
- list of resolved tools
- Throws:
IllegalConfigurationException- if the object has no@Tool-annotated methods- Since:
- 1.13.0
-
executeToolsConcurrently
public void executeToolsConcurrently()- Since:
- 1.4.0
-
executeToolsConcurrently
- Since:
- 1.4.0
-
maxSequentialToolsInvocations
public void maxSequentialToolsInvocations(int maxSequentialToolsInvocations) -
maxSequentialToolsInvocations
public int maxSequentialToolsInvocations() -
beforeToolExecution
- Since:
- 1.11.0
-
afterToolExecution
- Since:
- 1.11.0
-
argumentsErrorHandler
- Since:
- 1.4.0
-
argumentsErrorHandler
- Since:
- 1.4.0
-
executionErrorHandler
- Since:
- 1.4.0
-
executionErrorHandler
- Since:
- 1.4.0
-
toolSearchStrategy
- Since:
- 1.12.0
-
createContext
public ToolServiceContext createContext(InvocationContext invocationContext, UserMessage userMessage, List<ChatMessage> messages) -
executeInferenceAndToolsLoop
public ToolServiceResult executeInferenceAndToolsLoop(AiServiceContext context, Object memoryId, ChatResponse chatResponse, ChatRequestParameters parameters, List<ChatMessage> messages, ChatMemory chatMemory, InvocationContext invocationContext, ToolServiceContext toolServiceContext, boolean isReturnTypeResult) -
refreshDynamicProviders
public static ToolServiceContext refreshDynamicProviders(ToolServiceContext toolServiceContext, List<ChatMessage> messages, InvocationContext invocationContext) Re-evaluates dynamic tool providers and returns an updatedToolServiceContextwith any newly provided tool specifications and executors.Non-dynamic providers are not re-called — their tools remain unchanged. Tools returned by dynamic providers are only added, never removed: once a tool is present in the context, it stays for the remainder of the AI service invocation.
- Since:
- 1.13.0
-
executeTool
public ToolExecutionResult executeTool(InvocationContext invocationContext, Map<String, ToolExecutor> toolExecutors, ToolExecutionRequest toolRequest, Consumer<BeforeToolExecution> externalBeforeToolExecution, Consumer<ToolExecution> externalAfterToolExecution) -
executeWithErrorHandling
public static ToolExecutionResult executeWithErrorHandling(ToolExecutionRequest toolRequest, ToolExecutor toolExecutor, InvocationContext invocationContext, ToolArgumentsErrorHandler argumentsErrorHandler, ToolExecutionErrorHandler executionErrorHandler) -
applyToolHallucinationStrategy
-
toolSpecifications
-
toolExecutors
-
executor
- Since:
- 1.4.0
-
toolProviders
- Since:
- 1.12.0
-
toolProvider
Deprecated.usetoolProviders()instead -
isImmediateTool
-
toolProviders()instead