Class DecisionModelOutputGuardrail
java.lang.Object
dev.langchain4j.guardrails.DecisionModelOutputGuardrail
- All Implemented Interfaces:
Guardrail<OutputGuardrailRequest, OutputGuardrailResult>, 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 response and the last user message:
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.- 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, 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
-