Class DecisionServices
java.lang.Object
dev.langchain4j.service.decision.DecisionServices
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,truewhen the probability of "yes" reaches the threshold (seeDecisionServices.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 constantScale<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
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilds decision services. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> DecisionServices.Builder<T> Creates a builder for an implementation of the given interface.static <T> Tcreate(Class<T> serviceInterface, DecisionModel decisionModel) Creates an implementation of the given interface whose methods are answered by the given decision model.
-
Method Details
-
create
Creates an implementation of the given interface whose methods are answered by the given decision model. Equivalent tobuilder(serviceInterface).decisionModel(decisionModel).build(). -
builder
Creates a builder for an implementation of the given interface.
-