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 ofGuardrailsConfigto use for configurationP- The type ofGuardrailRequestto validateR- The type ofGuardrailResultto returnG- The type ofGuardrails being executedE- The type ofGuardrailExecutedEventto be firedF- The type ofGuardrailResult.Failureto 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
GuardrailExecutors.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAbstractGuardrailExecutor.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 RcomposeResult(R oldResult, R newResult) config()TheGuardrailsConfigto use for configuration of the guardrail executionprotected abstract GuardrailExecutedEvent.GuardrailExecutedEventBuilder<P, R, G, E> Creates an empty instance ofGuardrailExecutedEvent.GuardrailExecutedEventBuilderused for constructing observability event objects.protected abstract RcreateFailure(List<F> failures) Creates a failure result from someGuardrailResult.Failures.protected abstract GuardrailExceptioncreateGuardrailException(String message, Throwable cause) Creates aGuardrailExceptionusing the provided message and optional cause.protected abstract RCreates a success result.protected RexecuteGuardrails(P request) protected CompletableFuture<R> executeGuardrailsAsync(P request, CancellationChain chain) Non-blocking counterpart ofexecuteGuardrails(GuardrailRequest): runs the configured guardrails sequentially (each guardrail sees the result of any rewrite by the previous one), firing the observability event and short-circuiting on a fatal result, without blocking the calling thread.protected voidfireObservabilityEvent(InvocationContext invocationContext, P request, R result, G guardrail, Duration duration) Retrieves the guardrails associated with the implementation.protected RhandleFatalResult(R accumulatedResult, R result) Handles a fatal result.protected RValidates a guardrail against a set of request.protected CompletableFuture<R> validateAsync(P request, G guardrail, CancellationChain chain) Non-blocking counterpart ofvalidate(GuardrailRequest, Guardrail): runs a single guardrail'sGuardrail.validateAsync(GuardrailRequest)and wraps any failure in aGuardrailException, mirroring the synchronous error handling.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface GuardrailExecutor
execute, executeAsync
-
Constructor Details
-
AbstractGuardrailExecutor
-
-
Method Details
-
createFailure
Creates a failure result from someGuardrailResult.Failures.- Parameters:
failures- The failures- Returns:
- A
GuardrailResultcontaining the failures
-
createSuccess
Creates a success result.- Returns:
- A
GuardrailResultrepresenting success
-
createGuardrailException
Creates aGuardrailExceptionusing 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
GuardrailExceptionconstructed with the provided message and cause.
-
createEmptyObservabilityEventBuilderInstance
protected abstract GuardrailExecutedEvent.GuardrailExecutedEventBuilder<P,R, createEmptyObservabilityEventBuilderInstance()G, E> Creates an empty instance ofGuardrailExecutedEvent.GuardrailExecutedEventBuilderused for constructing observability event objects.- Returns:
- An initialized instance of
GuardrailExecutedEvent.GuardrailExecutedEventBuilderwith the appropriate type parameters.
-
config
Description copied from interface:GuardrailExecutorTheGuardrailsConfigto use for configuration of the guardrail execution- Specified by:
configin 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
GuardrailsConfigto use for configuration of the guardrail execution
-
guardrails
Description copied from interface:GuardrailExecutorRetrieves the guardrails associated with the implementation.- Specified by:
guardrailsin 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
Exceptionis thrown during validation, it will be wrapped in aGuardrailException.- Parameters:
request- TheGuardrailRequestto validateguardrail- TheGuardrailto evaluate against- Returns:
- The
GuardrailResultof the validation - Throws:
GuardrailException- If any kind ofExceptionis thrown during validation
-
handleFatalResult
-
fireObservabilityEvent
protected void fireObservabilityEvent(InvocationContext invocationContext, P request, R result, G guardrail, Duration duration) -
executeGuardrails
-
validateAsync
Non-blocking counterpart ofvalidate(GuardrailRequest, Guardrail): runs a single guardrail'sGuardrail.validateAsync(GuardrailRequest)and wraps any failure in aGuardrailException, mirroring the synchronous error handling. -
executeGuardrailsAsync
Non-blocking counterpart ofexecuteGuardrails(GuardrailRequest): runs the configured guardrails sequentially (each guardrail sees the result of any rewrite by the previous one), firing the observability event and short-circuiting on a fatal result, without blocking the calling thread. -
composeResult
-