Class ChoiceAnswer

java.lang.Object
dev.langchain4j.model.decision.response.ChoiceAnswer
All Implemented Interfaces:
DecisionAnswer

@Experimental public final class ChoiceAnswer extends Object implements DecisionAnswer
The answer to a ChoiceQuestion: the chosen option, the probability of each option and, if the model reports one, a confidence.
Since:
1.21.0
  • Method Details

    • value

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

      public Map<String,Double> probabilities()
      The probability of each option, keyed by option name. Empty if the model does not report probabilities.

      Unlike confidence(), probabilities have the same meaning for every model, which makes them the better basis for thresholds. Their calibration still differs between models, so thresholds need to be tuned again when the model changes.

    • options

      public List<String> options()
      The names of the options that were offered, in the order of the question.

      They are set by the DecisionModel after checking the answer against the question, so they are always present in a DecisionResponse returned by a decision model. They are empty for an answer that was built without them.

    • probabilityOf

      public double probabilityOf(String option)
      The probability of the given option, or 0 if the model reported probabilities but none for this option.
      Throws:
      IllegalArgumentException - if the option was not offered (see options()), for example because its name is misspelled.
      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 and differs between models, so a threshold tuned for one model does not carry over to another.

    • builder

      public static ChoiceAnswer.Builder builder()
    • 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