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 OutputGuardrail
s.
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 class
Builder 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
ConstructorsModifierConstructorDescriptionprotected
OutputGuardrailExecutor
(OutputGuardrailsConfig config, List<OutputGuardrail> guardrails) -
Method Summary
Modifier and TypeMethodDescriptionbuilder()
Creates a new instance ofOutputGuardrailExecutor.OutputGuardrailExecutorBuilder
.Creates an empty instance ofGuardrailExecutedEvent.GuardrailExecutedEventBuilder
used for constructing observability event objects.protected OutputGuardrailResult
createFailure
(List<OutputGuardrailResult.Failure> failures) Creates a failure result from someOutputGuardrailResult.Failure
s.protected OutputGuardrailException
createGuardrailException
(String message, Throwable cause) Creates aGuardrailException
using the provided message and optional cause.protected OutputGuardrailResult
Creates a success result.execute
(OutputGuardrailRequest request) Executes theOutputGuardrail
s on the givenOutputGuardrailRequest
.protected OutputGuardrailResult
handleFatalResult
(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 theOutputGuardrail
s on the givenOutputGuardrailRequest
.- Parameters:
request
- TheOutputGuardrailRequest
to validate- Returns:
- The
OutputGuardrailResult
of the validation
-
createFailure
Creates a failure result from someOutputGuardrailResult.Failure
s.- Specified by:
createFailure
in classAbstractGuardrailExecutor<OutputGuardrailsConfig, OutputGuardrailRequest, OutputGuardrailResult, OutputGuardrail, OutputGuardrailExecutedEvent, OutputGuardrailResult.Failure>
- Parameters:
failures
- The failures- Returns:
- A
OutputGuardrailResult
containing the failures
-
createSuccess
Creates a success result.- Specified by:
createSuccess
in classAbstractGuardrailExecutor<OutputGuardrailsConfig, OutputGuardrailRequest, OutputGuardrailResult, OutputGuardrail, OutputGuardrailExecutedEvent, OutputGuardrailResult.Failure>
- Returns:
- A
OutputGuardrailResult
representing success
-
createGuardrailException
Description copied from class:AbstractGuardrailExecutor
Creates aGuardrailException
using the provided message and optional cause.- Specified by:
createGuardrailException
in 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
GuardrailException
constructed with the provided message and cause.
-
handleFatalResult
protected OutputGuardrailResult handleFatalResult(OutputGuardrailResult accumulatedResult, OutputGuardrailResult result) Description copied from class:AbstractGuardrailExecutor
Handles a fatal result.- Overrides:
handleFatalResult
in 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:AbstractGuardrailExecutor
Creates an empty instance ofGuardrailExecutedEvent.GuardrailExecutedEventBuilder
used for constructing observability event objects.- Specified by:
createEmptyObservabilityEventBuilderInstance
in classAbstractGuardrailExecutor<OutputGuardrailsConfig, OutputGuardrailRequest, OutputGuardrailResult, OutputGuardrail, OutputGuardrailExecutedEvent, OutputGuardrailResult.Failure>
- Returns:
- An initialized instance of
GuardrailExecutedEvent.GuardrailExecutedEventBuilder
with 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.OutputGuardrailExecutorBuilder
instance.
-