Class ExecutableMethodToolExecutor

java.lang.Object
io.micronaut.langchain4j.tools.ExecutableMethodToolExecutor
All Implemented Interfaces:
dev.langchain4j.service.tool.ToolExecutor

@Internal public final class ExecutableMethodToolExecutor extends Object implements dev.langchain4j.service.tool.ToolExecutor
A LangChain4j ToolExecutor that invokes a Tool method of a bean through its Micronaut ExecutableMethod, with a ToolSpecification built from the compile-time annotation metadata and argument names of the method. LangChain4j neither scans the class of the bean for @Tool methods nor invokes them reflectively, so tool classes work without reflection metadata, in native images and in the classes generated for other languages (Python).

The specification, the coercion of the arguments and the conversion of the result follow LangChain4j's ToolSpecifications and DefaultToolExecutor. The JSON schema of a POJO parameter is still derived by LangChain4j from the class of the parameter.

Since:
2.4.0
  • Constructor Summary

    Constructors
    Constructor
    Description
    ExecutableMethodToolExecutor(@NonNull Object bean, @NonNull io.micronaut.inject.ExecutableMethod<?,?> method)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    execute(dev.langchain4j.agent.tool.ToolExecutionRequest request, Object memoryId)
     
    CompletableFuture<dev.langchain4j.service.tool.ToolExecutionResult>
    executeAsync(dev.langchain4j.agent.tool.ToolExecutionRequest request, dev.langchain4j.invocation.InvocationContext context)
     
    dev.langchain4j.service.tool.ToolExecutionResult
    executeWithContext(dev.langchain4j.agent.tool.ToolExecutionRequest request, dev.langchain4j.invocation.InvocationContext context)
     
    static @NonNull dev.langchain4j.service.tool.AiServiceTool
    toAiServiceTool(@NonNull Object bean, @NonNull io.micronaut.inject.ExecutableMethod<?,?> method)
    Builds the tool for a Tool method of a bean.
    @NonNull dev.langchain4j.agent.tool.ToolSpecification
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ExecutableMethodToolExecutor

      public ExecutableMethodToolExecutor(@NonNull Object bean, @NonNull io.micronaut.inject.ExecutableMethod<?,?> method)
      Parameters:
      bean - The bean declaring the tool
      method - The tool method
  • Method Details

    • toAiServiceTool

      public static @NonNull dev.langchain4j.service.tool.AiServiceTool toAiServiceTool(@NonNull Object bean, @NonNull io.micronaut.inject.ExecutableMethod<?,?> method)
      Builds the tool for a Tool method of a bean.
      Parameters:
      bean - The bean
      method - The tool method
      Returns:
      The tool
    • toolSpecification

      public @NonNull dev.langchain4j.agent.tool.ToolSpecification toolSpecification()
      Returns:
      The specification of the tool, as LangChain4j's ToolSpecifications.toolSpecificationFrom builds it
    • execute

      public String execute(dev.langchain4j.agent.tool.ToolExecutionRequest request, Object memoryId)
      Specified by:
      execute in interface dev.langchain4j.service.tool.ToolExecutor
    • executeWithContext

      public dev.langchain4j.service.tool.ToolExecutionResult executeWithContext(dev.langchain4j.agent.tool.ToolExecutionRequest request, dev.langchain4j.invocation.InvocationContext context)
      Specified by:
      executeWithContext in interface dev.langchain4j.service.tool.ToolExecutor
    • executeAsync

      public CompletableFuture<dev.langchain4j.service.tool.ToolExecutionResult> executeAsync(dev.langchain4j.agent.tool.ToolExecutionRequest request, dev.langchain4j.invocation.InvocationContext context)
      Specified by:
      executeAsync in interface dev.langchain4j.service.tool.ToolExecutor