Class DecisionModelOutputGuardrail.Builder

java.lang.Object
dev.langchain4j.guardrails.DecisionModelOutputGuardrail.Builder
Enclosing class:
DecisionModelOutputGuardrail

public static class DecisionModelOutputGuardrail.Builder extends Object
Since:
1.21.0
  • Constructor Details

    • Builder

      public Builder()
  • Method Details

    • decisionModel

      public DecisionModelOutputGuardrail.Builder decisionModel(DecisionModel decisionModel)
      Sets the decision model that answers the checks. Required.
    • check

      public DecisionModelOutputGuardrail.Builder check(String name, String question)
      Adds a check. At least one check is required, and check names must be unique.
      Parameters:
      name - the name of the check, included in the failure message, for example "personalData".
      question - a yes/no question where "yes" means the response must be rejected, for example "Does the response reveal personal data?".
    • check

      public DecisionModelOutputGuardrail.Builder check(String name, String question, Double threshold)
      Adds a check with its own threshold, which overrides the threshold of the guardrail (see threshold(Double)) for this check: for example, a low threshold for a check that must rarely miss, and a high one for a check that should only reject clear cases. All checks are still answered in a single call.
      Parameters:
      name - the name of the check, see check(String, String).
      question - a yes/no question where "yes" means the response must be rejected.
      threshold - the probability of "yes", from 0 to 1, from which this check fails, or null to use the threshold of the guardrail.
    • checks

      Sets the checks, as yes/no questions keyed by check name, replacing the checks added so far. See check(String, String).
    • threshold

      public DecisionModelOutputGuardrail.Builder threshold(Double threshold)
      Sets the probability of "yes" from which a check fails, for the checks without their own threshold (see check(String, String, Double)).

      Default value is 0.5. A check fails when the probability of "yes" is greater than or equal to it. Unlike the minProbability of components that select something, reaching it rejects the response.

    • reprompt

      public DecisionModelOutputGuardrail.Builder reprompt(String reprompt)
      Sets the instruction sent to the model when a response is rejected, to ask it for a new response. Optional: by default, a rejected response fails with a fatal result.
    • maxMessages

      public DecisionModelOutputGuardrail.Builder maxMessages(Integer maxMessages)
      Sets the maximum number of messages of the conversation that the decision model receives in addition to the response: the last messages, plus the last user message if it is older. An AI message with tool calls and each tool result count as one message each. Tool results whose AI message does not fit are left out. With parallel tool calls, set it higher than the number of tool calls in one round. With 1, system messages are still sent; leave them out with messageFilter(Predicate). System messages are sent in addition and do not count. Checks of the response against the data of the tools need the messages with that data, so keep it large enough. For decision models with a small input limit, such as local models, set it lower (for example, maxMessages(4) and maxMessageLength(4000) help keep a request to Ollama under its 64 KiB limit; the system message and the response are always sent in full).

      Default value is 50.

      Since:
      1.23.0
    • maxMessageLength

      public DecisionModelOutputGuardrail.Builder maxMessageLength(Integer maxMessageLength)
      Sets the maximum length, in characters, of the text of each message the decision model receives, other than system messages and the response, and of the arguments of each tool call. A longer text is shortened in the middle: its beginning and its end are kept, with a note of about 90 characters saying how many characters were left out. Long tool results and user messages with retrieved content are the usual cases. A check that compares the response with data that was left out tends to reject the response, even when it is correct, so raise it for checks against long tool results. As a rough guide, 4 characters make one token in English text.

      Default value is 50000.

      Since:
      1.23.0
    • messageFilter

      public DecisionModelOutputGuardrail.Builder messageFilter(Predicate<? super ChatMessage> messageFilter)
      Sets which messages of the conversation the decision model receives, in addition to the response. For example, message -> !(message instanceof SystemMessage) leaves out the system message, and message -> !(message instanceof ToolExecutionResultMessage) leaves out tool results, which can be large.

      By default, all messages are sent: system messages, user messages, AI messages with their tool calls, and tool results. The filter also applies to the last user message, but not to the response. Keep or leave out an AI message with tool calls together with its tool results.

      Since:
      1.23.0
    • build