Class MessageModeratorInputGuardrail
java.lang.Object
dev.langchain4j.guardrails.MessageModeratorInputGuardrail
- All Implemented Interfaces:
Guardrail<InputGuardrailRequest, InputGuardrailResult>, InputGuardrail
An
InputGuardrail that validates user messages using a ModerationModel to detect
potentially harmful, inappropriate, or policy-violating content.
This guardrail checks incoming user messages for content that should be moderated, such as hate speech, violence, self-harm, sexual content, or other categories defined by the moderation model. If the message is flagged by the moderation model, the validation fails with a fatal result, preventing the message from being processed further.
This is useful for ensuring that user inputs comply with content policies before being sent to an LLM or processed by the application.
-
Constructor Summary
ConstructorsConstructorDescriptionMessageModeratorInputGuardrail(ModerationModel moderationModel) Constructs a newMessageModeratorInputGuardrailwith the specified moderation model. -
Method Summary
Modifier and TypeMethodDescriptionvalidate(UserMessage userMessage) Validates the provided user message using the configuredModerationModel.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface InputGuardrail
failure, failure, fatal, fatal, success, successWith, validate
-
Constructor Details
-
MessageModeratorInputGuardrail
Constructs a newMessageModeratorInputGuardrailwith the specified moderation model.- Parameters:
moderationModel- theModerationModelto use for validating user messages. Must not be null.
-
-
Method Details
-
validate
Validates the provided user message using the configuredModerationModel.If the moderation model flags the message as inappropriate or policy-violating, this method returns a fatal result with a
ModerationException. Otherwise, it returns a successful validation result.- Specified by:
validatein interfaceInputGuardrail- Parameters:
userMessage- theUserMessageto validate. Must not be null.- Returns:
- an
InputGuardrailResultindicating success if the message passes moderation, or a fatal result with aModerationExceptionif the message is flagged
-