Class DecisionServices

java.lang.Object
dev.langchain4j.service.decision.DecisionServices

@Experimental public final class DecisionServices extends Object
Creates implementations of Java interfaces whose methods are answered by a DecisionModel.

The parameters of a method are sent to the model as the input, keyed by parameter name, and the return type determines the questions:

  • boolean: a yes/no question, true when the probability of "yes" reaches the threshold (see DecisionServices.Builder.thresholdProvider(ThresholdProvider))
  • YesNoAnswer: a yes/no question, with the probability of "yes"
  • an enum: a question choosing one of its constants
  • Choice<enum>: the same, with the probability of each constant
  • Scale<enum>: a question placing the input on an ordered scale, whose levels are the constants from the first (lowest) to the last (highest)
  • an object or record whose fields are of the types above: one question per field, in a single call
Any of these can be wrapped in DecisionResult (to also get the raw response) and/or in CompletableFuture or CompletionStage (to call the model asynchronously).

A parameter of type DecisionRequestParameters is not sent as part of the input: it sets the parameters of the call, such as the model name, overriding the model's defaults.

The question is set with @Decide: on the method, or on the fields of the returned object. The meaning of enum constants is described with @Description:

enum Team {
    @Description("Payments, invoices, refunds") BILLING,
    @Description("Problems using the product") SUPPORT
}

class Triage {
    @Decide("Which team should handle this ticket?") Team team;
    @Decide("Does this need attention today?") boolean urgent;
}

interface SupportDesk {

    Triage triage(String ticket);

    @Decide("Is this message spam?")
    YesNoAnswer isSpam(String message);

    @Decide("Which team should handle this ticket?")
    Choice<Team> route(String ticket);
}

SupportDesk supportDesk = DecisionServices.builder(SupportDesk.class)
        .decisionModel(decisionModel)
        .build();
The created implementation holds no state of its own, so it can be shared between threads.
Since:
1.21.0
  • Method Details

    • create

      public static <T> T create(Class<T> serviceInterface, DecisionModel decisionModel)
      Creates an implementation of the given interface whose methods are answered by the given decision model. Equivalent to builder(serviceInterface).decisionModel(decisionModel).build().
    • builder

      public static <T> DecisionServices.Builder<T> builder(Class<T> serviceInterface)
      Creates a builder for an implementation of the given interface.