Class DecisionModelOutputGuardrail

java.lang.Object
dev.langchain4j.guardrails.DecisionModelOutputGuardrail
All Implemented Interfaces:
Guardrail<OutputGuardrailRequest, OutputGuardrailResult>, OutputGuardrail

@Experimental public class DecisionModelOutputGuardrail extends Object implements OutputGuardrail
An OutputGuardrail that checks the responses of the model with a DecisionModel.

Each check is a yes/no question where "yes" means the response must be rejected. All checks are answered in a single call. The decision model receives the conversation that led to the response, ending with the response: the messages sent to the model to produce the response (OutputGuardrailRequest.chatRequest()), including the system message, tool calls and tool results, so that checks can compare the response with the instructions of the assistant and with the data its tools returned (see DecisionModelOutputGuardrail.Builder.maxMessages(Integer), DecisionModelOutputGuardrail.Builder.maxMessageLength(Integer) and DecisionModelOutputGuardrail.Builder.messageFilter(Predicate)). The user message comes with the content retrieved for it (RAG), and after a reprompt, the reprompt and its tool calls are included. When the request is not known, the messages are taken from the chat memory, or, without one, the user message is sent with the response. Each question is preceded by a fixed English sentence saying that the response is the last assistant message of the conversation. For example:

OutputGuardrail guardrail = DecisionModelOutputGuardrail.builder()
        .decisionModel(decisionModel)
        .check("personalData", "Does the response reveal personal data, such as contact details or account numbers?")
        .check("unanswered", "Does the response fail to address the user message?")
        .reprompt("Answer the user message without revealing personal data.")
        .build();
A rejected response fails with a fatal result or, if DecisionModelOutputGuardrail.Builder.reprompt(String) is set, the model is asked again with that instruction. Responses without text (for example, only tool calls) are not checked. If the decision model fails, the exception is propagated, so the request fails. A check that the decision model refuses to answer fails, so the response is rejected.
Since:
1.21.0
  • Constructor Details

    • DecisionModelOutputGuardrail

      public DecisionModelOutputGuardrail(DecisionModel decisionModel, Map<String,String> checks)
      Creates a guardrail with the given checks and the default threshold.
      Parameters:
      decisionModel - the decision model that answers the checks.
      checks - the checks, as yes/no questions keyed by check name, where "yes" means the response must be rejected.
    • DecisionModelOutputGuardrail

      protected DecisionModelOutputGuardrail(DecisionModelOutputGuardrail.Builder builder)
  • Method Details