Class DecisionMethod

java.lang.Object
dev.langchain4j.service.decision.internal.DecisionMethod

public final class DecisionMethod extends Object
The analysis of one decision service method: the questions it asks, how its parameters become the input, and how the answers are mapped back to its return type.

All validation happens in of(Method), so frameworks can detect errors at build time. At runtime, they can call invoke(DecisionServiceConfig, Object[]) or invokeAsync(DecisionServiceConfig, Object[]) to implement the method without the proxy created by DecisionServices.

  • Method Details

    • of

      public static DecisionMethod of(Method method)
      Analyzes the given method of a decision service interface.
      Throws:
      IllegalConfigurationException - if the method cannot be implemented.
    • of

      public static DecisionMethod of(Method method, Type returnType)
      Analyzes the given method as if it returned the given type. Frameworks can use this for return types that are not supported here, for example a reactive type such as Mutiny's Uni<T>: they pass T and call invokeAsync(DecisionServiceConfig, Object[]).
      Throws:
      IllegalConfigurationException - if the method cannot be implemented.
    • method

      public Method method()
    • questions

      public Map<String,Question> questions()
      The questions asked by this method, keyed by question name.
    • isAsync

      public boolean isAsync()
      Whether the method returns a CompletableFuture or a CompletionStage.
    • contentType

      public Type contentType()
      The type of the result, without the CompletableFuture and DecisionResult wrappers.
    • reflectiveTypes

      public Set<Class<?>> reflectiveTypes()
      The classes accessed by reflection when mapping the answers, so that frameworks can register them for native images: the returned object types and their superclasses (their declared fields and constructors are used, and for records their components), and the enums whose constants are the options (their constant fields are read for @Description). When of(Method) itself runs in a native image, the service interface (its methods, with their parameters and annotations) must be registered as well.
    • inputTypes

      public List<Type> inputTypes()
      The types of the parameters that are sent to the model. Objects among them are converted to maps with the JSON codec, so frameworks may need to register them, and the types they contain, for native images.
    • toRequest

      public DecisionRequest toRequest(Object[] args)
      Creates the request for an invocation of this method with the given arguments.
    • toResult

      public Object toResult(DecisionResponse response, Function<String,Double> thresholds)
      Maps the response to the result of this method, without the CompletableFuture wrapper.
      Parameters:
      thresholds - the threshold of each boolean answer, by question name; null means 0.5.
    • invoke

      public Object invoke(DecisionServiceConfig config, Object[] args)
      Invokes this method with the given configuration of the decision service.
    • invokeAsync

      public CompletableFuture<Object> invokeAsync(DecisionServiceConfig config, Object[] args)
      Invokes this method asynchronously with the given model, whatever its return type. The call to the model starts immediately; frameworks with lazy types (such as Mutiny's Uni) should call this method on subscription.
      Returns:
      a future of the result, without the CompletableFuture wrapper of the method's return type.