Class AbstractGuardrailService
java.lang.Object
dev.langchain4j.service.guardrail.AbstractGuardrailService
- All Implemented Interfaces:
GuardrailService
Responsible for managing and applying input and output guardrails to methods
of a specified AI service class. Guardrails are defined through annotations at either the
class or method level and are used to enforce constraints, validation, or transformation
logic on inputs and outputs of AI service methods.
This class handles the initialization, configuration, and execution of input and output
guardrail logic for the methods of the associated AI service class, ensuring both input
and output constraints are applied automatically when methods are invoked.
-
Nested Class Summary
Nested classes/interfaces inherited from interface dev.langchain4j.service.guardrail.GuardrailService
GuardrailService.Builder
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractGuardrailService
(Class<?> aiServiceClass, Map<Object, InputGuardrailExecutor> inputGuardrails, Map<Object, OutputGuardrailExecutor> outputGuardrails) -
Method Summary
Modifier and TypeMethodDescriptionClass
<?> Retrieves the class representing the AI service to which the guardrails apply.<MethodKey>
InputGuardrailResultexecuteInputGuardrails
(MethodKey method, InputGuardrailRequest params) Executes the input guardrails associated with a givenMethod
<MethodKey>
OutputGuardrailResultexecuteOutputGuardrails
(MethodKey method, OutputGuardrailRequest params) Executes the output guardrails associated with a givenMethod
.<MethodKey>
booleanhasInputGuardrails
(MethodKey method) Whether or not a method has any input guardrails associated with it<MethodKey>
booleanhasOutputGuardrails
(MethodKey method) Whether or not a method has any output guardrails associated with itMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface dev.langchain4j.service.guardrail.GuardrailService
executeGuardrails, executeGuardrails
-
Constructor Details
-
AbstractGuardrailService
protected AbstractGuardrailService(Class<?> aiServiceClass, Map<Object, InputGuardrailExecutor> inputGuardrails, Map<Object, OutputGuardrailExecutor> outputGuardrails)
-
-
Method Details
-
aiServiceClass
Description copied from interface:GuardrailService
Retrieves the class representing the AI service to which the guardrails apply.- Specified by:
aiServiceClass
in interfaceGuardrailService
- Returns:
- The
Class
object representing the AI service.
-
executeInputGuardrails
public <MethodKey> InputGuardrailResult executeInputGuardrails(MethodKey method, InputGuardrailRequest params) Description copied from interface:GuardrailService
Executes the input guardrails associated with a givenMethod
- Specified by:
executeInputGuardrails
in interfaceGuardrailService
- Type Parameters:
MethodKey
- > The type of the method key, representing a unique identifier for methods.- Parameters:
method
- The method whose input guardrails are to be executed.params
- The parameters to validate against the input guardrails. Must not be null.- Returns:
- The result of executing the input guardrails, encapsulated in an
InputGuardrailResult
. If no guardrails are associated with the method, a successful result is returned by default.
-
executeOutputGuardrails
public <MethodKey> OutputGuardrailResult executeOutputGuardrails(MethodKey method, OutputGuardrailRequest params) Description copied from interface:GuardrailService
Executes the output guardrails associated with a givenMethod
.- Specified by:
executeOutputGuardrails
in interfaceGuardrailService
- Type Parameters:
MethodKey
- > The type of the method key, representing a unique identifier for methods.- Parameters:
method
- The method whose output guardrails are to be executed.params
- The parameters to validate against the output guardrails. Must not be null.- Returns:
- The result of executing the output guardrails, encapsulated in an
OutputGuardrailResult
. If no guardrails are associated with the method, a successful result is returned by default.
-
hasInputGuardrails
public <MethodKey> boolean hasInputGuardrails(MethodKey method) Description copied from interface:GuardrailService
Whether or not a method has any input guardrails associated with it- Specified by:
hasInputGuardrails
in interfaceGuardrailService
- Type Parameters:
MethodKey
- > The type of the method key, representing a unique identifier for methods.- Parameters:
method
- The method- Returns:
true
Ifmethod
has input guardrails.false
otherwise
-
hasOutputGuardrails
public <MethodKey> boolean hasOutputGuardrails(MethodKey method) Description copied from interface:GuardrailService
Whether or not a method has any output guardrails associated with it- Specified by:
hasOutputGuardrails
in interfaceGuardrailService
- Type Parameters:
MethodKey
- > The type of the method key, representing a unique identifier for methods.- Parameters:
method
- The method- Returns:
true
Ifmethod
has output guardrails.false
otherwise
-