Class AbstractGuardrailExecutor<C extends GuardrailsConfig, P extends GuardrailRequest<P>, R extends GuardrailResult<R>, G extends Guardrail<P,R>, E extends GuardrailExecutedEvent<P,R,G>, F extends GuardrailResult.Failure>
java.lang.Object
dev.langchain4j.guardrail.AbstractGuardrailExecutor<C,P,R,G,E,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 executedE
- The type ofGuardrailExecutedEvent
to be firedF
- The type ofGuardrailResult.Failure
to return
- All Implemented Interfaces:
GuardrailExecutor<C,
P, R, G, E>
- 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>, E extends GuardrailExecutedEvent<P,R,G>, F extends GuardrailResult.Failure>
extends Object
implements GuardrailExecutor<C,P,R,G,E>
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>, E extends GuardrailExecutedEvent<P, R, G>, B extends AbstractGuardrailExecutor.GuardrailExecutorBuilder<C, R, P, G, E, 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 GuardrailExecutedEvent.GuardrailExecutedEventBuilder
<P, R, G, E> Creates an empty instance ofGuardrailExecutedEvent.GuardrailExecutedEventBuilder
used for constructing observability event objects.protected 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 request) protected void
fireObservabilityEvent
(InvocationContext invocationContext, P request, R result, G guardrail) 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 request.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface 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.
-
createEmptyObservabilityEventBuilderInstance
protected abstract GuardrailExecutedEvent.GuardrailExecutedEventBuilder<P,R, createEmptyObservabilityEventBuilderInstance()G, E> Creates an empty instance ofGuardrailExecutedEvent.GuardrailExecutedEventBuilder
used for constructing observability event objects.- Returns:
- An initialized instance of
GuardrailExecutedEvent.GuardrailExecutedEventBuilder
with the appropriate type parameters.
-
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>, E extends GuardrailExecutedEvent<P, R, G>> - 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>, E extends GuardrailExecutedEvent<P, R, G>> - Returns:
- The guardrails which can be used for validating inputs or outputs against predefined rules.
-
validate
Validates a guardrail against a set of request.If any kind of
Exception
is thrown during validation, it will be wrapped in aGuardrailException
.- Parameters:
request
- TheGuardrailRequest
to validateguardrail
- TheGuardrail
to evaluate against- Returns:
- The
GuardrailResult
of the validation - Throws:
GuardrailException
- If any kind ofException
is thrown during validation
-
handleFatalResult
-
fireObservabilityEvent
protected void fireObservabilityEvent(InvocationContext invocationContext, P request, R result, G guardrail) -
executeGuardrails
-
composeResult
-