Class Choice<E>

java.lang.Object
dev.langchain4j.service.decision.Choice<E>
Type Parameters:
E - the type of the options: an enum whose constants are the options, or for example String

@Experimental public final class Choice<E> extends Object
The answer to a question of a decision service that chooses one of several options: the chosen option, the probability of each option and, if the model reports one, a confidence. It is the typed counterpart of ChoiceAnswer.

Decision services create it for enums, whose constants are the options. It can also be built for options that are only known at runtime, such as strings, for example from a ChoiceAnswer of the DecisionModel API.

Since:
1.21.0
  • Method Details

    • builder

      public static <E> Choice.Builder<E> builder()
    • value

      public E value()
      The chosen option, usually the one with the highest probability.
    • probabilities

      public Map<E,Double> probabilities()
      The probability of each option. Empty if the model does not report probabilities.
    • probabilityOf

      public double probabilityOf(E option)
      The probability of the given option, or 0 if the model reported probabilities but none for this option.
      Throws:
      IllegalStateException - if the model did not report probabilities.
    • margin

      public double margin()
      The difference between the two highest probabilities, from 0 to 1. A small margin means the model hesitated between two options, which is a common signal to escalate, for example to a human.

      If the model reported probabilities for only some of the options, the probability it did not report is assumed to belong to a single other option, so the margin is never overestimated.

      Throws:
      IllegalStateException - if the model did not report probabilities.
    • confidence

      public Double confidence()
      How confident the model is in value(), from 0 to 1, or null if the model does not report a confidence. The formula is defined by each model, so prefer probabilities() for thresholds.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object