Class OutputGuardrailExecutor
java.lang.Object
dev.langchain4j.guardrail.AbstractGuardrailExecutor<OutputGuardrailsConfig, OutputGuardrailRequest, OutputGuardrailResult, OutputGuardrail, OutputGuardrailExecutedEvent, OutputGuardrailResult.Failure>
dev.langchain4j.guardrail.OutputGuardrailExecutor
- All Implemented Interfaces:
GuardrailExecutor<OutputGuardrailsConfig, OutputGuardrailRequest, OutputGuardrailResult, OutputGuardrail, OutputGuardrailExecutedEvent>
public non-sealed class OutputGuardrailExecutor
extends AbstractGuardrailExecutor<OutputGuardrailsConfig, OutputGuardrailRequest, OutputGuardrailResult, OutputGuardrail, OutputGuardrailExecutedEvent, OutputGuardrailResult.Failure>
The
GuardrailExecutor for OutputGuardrails.
When executing output guardrails, if any OutputGuardrail triggers a reprompt or retry,
the new response has to go back through the entire chain of output guardrails to ensure the new response
passes all the output guardrails.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class for constructing instances ofOutputGuardrailExecutor.Nested classes/interfaces inherited from class AbstractGuardrailExecutor
AbstractGuardrailExecutor.GuardrailExecutorBuilder<C,R, P, G, E, B> -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedOutputGuardrailExecutor(OutputGuardrailsConfig config, List<OutputGuardrail> guardrails) -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Creates a new instance ofOutputGuardrailExecutor.OutputGuardrailExecutorBuilder.Creates an empty instance ofGuardrailExecutedEvent.GuardrailExecutedEventBuilderused for constructing observability event objects.protected OutputGuardrailResultcreateFailure(List<OutputGuardrailResult.Failure> failures) Creates a failure result from someOutputGuardrailResult.Failures.protected OutputGuardrailExceptioncreateGuardrailException(String message, Throwable cause) Creates aGuardrailExceptionusing the provided message and optional cause.protected OutputGuardrailResultCreates a success result.execute(OutputGuardrailRequest request) Executes theOutputGuardrails on the givenOutputGuardrailRequest.protected OutputGuardrailResulthandleFatalResult(OutputGuardrailResult accumulatedResult, OutputGuardrailResult result) Handles a fatal result.Methods inherited from class AbstractGuardrailExecutor
composeResult, config, executeGuardrails, fireObservabilityEvent, guardrails, validate
-
Field Details
-
MAX_RETRIES_MESSAGE_TEMPLATE
- See Also:
-
-
Constructor Details
-
OutputGuardrailExecutor
-
-
Method Details
-
execute
Executes theOutputGuardrails on the givenOutputGuardrailRequest.- Parameters:
request- TheOutputGuardrailRequestto validate- Returns:
- The
OutputGuardrailResultof the validation
-
createFailure
Creates a failure result from someOutputGuardrailResult.Failures.- Specified by:
createFailurein classAbstractGuardrailExecutor<OutputGuardrailsConfig, OutputGuardrailRequest, OutputGuardrailResult, OutputGuardrail, OutputGuardrailExecutedEvent, OutputGuardrailResult.Failure>- Parameters:
failures- The failures- Returns:
- A
OutputGuardrailResultcontaining the failures
-
createSuccess
Creates a success result.- Specified by:
createSuccessin classAbstractGuardrailExecutor<OutputGuardrailsConfig, OutputGuardrailRequest, OutputGuardrailResult, OutputGuardrail, OutputGuardrailExecutedEvent, OutputGuardrailResult.Failure>- Returns:
- A
OutputGuardrailResultrepresenting success
-
createGuardrailException
Description copied from class:AbstractGuardrailExecutorCreates aGuardrailExceptionusing the provided message and optional cause.- Specified by:
createGuardrailExceptionin classAbstractGuardrailExecutor<OutputGuardrailsConfig, OutputGuardrailRequest, OutputGuardrailResult, OutputGuardrail, OutputGuardrailExecutedEvent, OutputGuardrailResult.Failure>- 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.
-
handleFatalResult
protected OutputGuardrailResult handleFatalResult(OutputGuardrailResult accumulatedResult, OutputGuardrailResult result) Description copied from class:AbstractGuardrailExecutorHandles a fatal result.- Overrides:
handleFatalResultin classAbstractGuardrailExecutor<OutputGuardrailsConfig, OutputGuardrailRequest, OutputGuardrailResult, OutputGuardrail, OutputGuardrailExecutedEvent, OutputGuardrailResult.Failure>- Parameters:
accumulatedResult- The accumulated resultresult- The fatal result- Returns:
- The fatal result, possibly wrapped/modified in some way
-
createEmptyObservabilityEventBuilderInstance
protected OutputGuardrailExecutedEvent.OutputGuardrailExecutedEventBuilder createEmptyObservabilityEventBuilderInstance()Description copied from class:AbstractGuardrailExecutorCreates an empty instance ofGuardrailExecutedEvent.GuardrailExecutedEventBuilderused for constructing observability event objects.- Specified by:
createEmptyObservabilityEventBuilderInstancein classAbstractGuardrailExecutor<OutputGuardrailsConfig, OutputGuardrailRequest, OutputGuardrailResult, OutputGuardrail, OutputGuardrailExecutedEvent, OutputGuardrailResult.Failure>- Returns:
- An initialized instance of
GuardrailExecutedEvent.GuardrailExecutedEventBuilderwith the appropriate type parameters.
-
builder
Creates a new instance ofOutputGuardrailExecutor.OutputGuardrailExecutorBuilder. The builder is used to construct and configure instances ofOutputGuardrailExecutor.OutputGuardrailExecutorBuilder.- Returns:
- A new
OutputGuardrailExecutor.OutputGuardrailExecutorBuilderinstance.
-