Class MessageModeratorInputGuardrail
java.lang.Object
dev.langchain4j.guardrails.MessageModeratorInputGuardrail
- All Implemented Interfaces:
dev.langchain4j.guardrail.Guardrail<dev.langchain4j.guardrail.InputGuardrailRequest, dev.langchain4j.guardrail.InputGuardrailResult>, dev.langchain4j.guardrail.InputGuardrail
public class MessageModeratorInputGuardrail
extends Object
implements dev.langchain4j.guardrail.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(dev.langchain4j.model.moderation.ModerationModel moderationModel) Constructs a newMessageModeratorInputGuardrailwith the specified moderation model. -
Method Summary
Modifier and TypeMethodDescriptiondev.langchain4j.guardrail.InputGuardrailResultvalidate(dev.langchain4j.data.message.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 dev.langchain4j.guardrail.InputGuardrail
failure, failure, fatal, fatal, success, successWith, validate
-
Constructor Details
-
MessageModeratorInputGuardrail
public MessageModeratorInputGuardrail(dev.langchain4j.model.moderation.ModerationModel moderationModel) Constructs a newMessageModeratorInputGuardrailwith the specified moderation model.- Parameters:
moderationModel- theModerationModelto use for validating user messages. Must not be null.
-
-
Method Details
-
validate
public dev.langchain4j.guardrail.InputGuardrailResult validate(dev.langchain4j.data.message.UserMessage userMessage) 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 interfacedev.langchain4j.guardrail.InputGuardrail- 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
-