Package dev.langchain4j.guardrail
Class AbstractGuardrailExecutor<C extends GuardrailsConfig,P extends GuardrailRequest<P>,R extends GuardrailResult<R>,G extends Guardrail<P,R>,F extends GuardrailResult.Failure>
java.lang.Object
dev.langchain4j.guardrail.AbstractGuardrailExecutor<C,P,R,G,F>
- Type Parameters:
C
- The type ofGuardrailsConfig
to use for configurationP
- The type ofGuardrailRequest
to validateR
- The type ofGuardrailResult
to returnG
- The type ofGuardrail
s being executedF
- The type ofGuardrailResult.Failure
to return
- All Implemented Interfaces:
GuardrailExecutor<C,
P, R, G>
- Direct Known Subclasses:
InputGuardrailExecutor
,OutputGuardrailExecutor
public abstract sealed class AbstractGuardrailExecutor<C extends GuardrailsConfig,P extends GuardrailRequest<P>,R extends GuardrailResult<R>,G extends Guardrail<P,R>,F extends GuardrailResult.Failure>
extends Object
implements GuardrailExecutor<C,P,R,G>
permits InputGuardrailExecutor, OutputGuardrailExecutor
Abstract base class for
GuardrailExecutor
s.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
AbstractGuardrailExecutor.GuardrailExecutorBuilder<C extends GuardrailsConfig,
R extends GuardrailResult<R>, P extends GuardrailRequest<P>, G extends Guardrail<P, R>, B extends AbstractGuardrailExecutor.GuardrailExecutorBuilder<C, R, P, G, B>> A generic abstract builder class for creating instances ofGuardrailExecutor
. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected R
composeResult
(R oldResult, R newResult) config()
TheGuardrailsConfig
to use for configuration of the guardrail executionprotected abstract R
createFailure
(List<F> failures) Creates a failure result from someGuardrailResult.Failure
s.protected abstract GuardrailException
createGuardrailException
(String message, Throwable cause) Creates aGuardrailException
using the provided message and optional cause.protected abstract R
Creates a success result.protected R
executeGuardrails
(P params) Retrieves the guardrails associated with the implementation.protected R
handleFatalResult
(R accumulatedResult, R result) Handles a fatal result.protected R
Validates a guardrail against a set of params.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface dev.langchain4j.guardrail.GuardrailExecutor
execute
-
Constructor Details
-
AbstractGuardrailExecutor
-
-
Method Details
-
createFailure
Creates a failure result from someGuardrailResult.Failure
s.- Parameters:
failures
- The failures- Returns:
- A
GuardrailResult
containing the failures
-
createSuccess
Creates a success result.- Returns:
- A
GuardrailResult
representing success
-
createGuardrailException
Creates aGuardrailException
using the provided message and optional cause.- Parameters:
message
- The detailed message for the exception.cause
- The underlying cause of the exception, or null if no cause is available.- Returns:
- A new instance of
GuardrailException
constructed with the provided message and cause.
-
config
Description copied from interface:GuardrailExecutor
TheGuardrailsConfig
to use for configuration of the guardrail execution- Specified by:
config
in interfaceGuardrailExecutor<C extends GuardrailsConfig,
P extends GuardrailRequest<P>, R extends GuardrailResult<R>, G extends Guardrail<P, R>> - Returns:
- The
GuardrailsConfig
to use for configuration of the guardrail execution
-
guardrails
Description copied from interface:GuardrailExecutor
Retrieves the guardrails associated with the implementation.- Specified by:
guardrails
in interfaceGuardrailExecutor<C extends GuardrailsConfig,
P extends GuardrailRequest<P>, R extends GuardrailResult<R>, G extends Guardrail<P, R>> - Returns:
- The guardrails which can be used for validating inputs or outputs against predefined rules.
-
validate
Validates a guardrail against a set of params.If any kind of
Exception
is thrown during validation, it will be wrapped in aGuardrailException
.- Parameters:
params
- TheGuardrailRequest
to validateguardrail
- TheGuardrail
to evaluate against- Returns:
- The
GuardrailResult
of the validation - Throws:
GuardrailException
- If any kind ofException
is thrown during validation
-
handleFatalResult
Handles a fatal result.- Parameters:
accumulatedResult
- The accumulated resultresult
- The fatal result- Returns:
- The fatal result, possibly wrapped/modified in some way
-
executeGuardrails
-
composeResult
-