Class DecisionModelOutputGuardrail
- All Implemented Interfaces:
Guardrail<OutputGuardrailRequest, OutputGuardrailResult>, OutputGuardrail
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
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDecisionModelOutputGuardrail(DecisionModel decisionModel, Map<String, String> checks) Creates a guardrail with the given checks and the default threshold. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()protected StringfailureMessage(List<String> failedChecks) The message of the failure when checks fail.validate(OutputGuardrailRequest request) Validates the response from the LLM.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Guardrail
name, validateAsyncMethods inherited from interface OutputGuardrail
failure, failure, failureWithMessageRemoval, failureWithMessageRemoval, fatal, fatal, fatalWithMessageRemoval, fatalWithMessageRemoval, reprompt, reprompt, retry, retry, success, successWith, successWith, successWith, successWith, successWithResult, validate
-
Constructor Details
-
DecisionModelOutputGuardrail
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
-
-
Method Details
-
validate
Description copied from interface:OutputGuardrailValidates the response from the LLM.Unlike
OutputGuardrail.validate(AiMessage), this method allows you to access the memory and the augmentation result (in the case of a RAG).Implementation must not attempt to write to the memory or the augmentation result.
- Specified by:
validatein interfaceGuardrail<OutputGuardrailRequest, OutputGuardrailResult>- Specified by:
validatein interfaceOutputGuardrail- Parameters:
request- the parameters, including the response from the LLM, the memory, and the augmentation result.- Returns:
- The result of the validation
-
failureMessage
-
builder
-