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 of GuardrailsConfig to use for configuration
P - The type of GuardrailRequest to validate
R - The type of GuardrailResult to return
G - The type of Guardrails being executed
F - The type of GuardrailResult.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 GuardrailExecutors.
  • Constructor Details

    • AbstractGuardrailExecutor

      protected AbstractGuardrailExecutor(C config, List<G> guardrails)
  • Method Details

    • createFailure

      protected abstract R createFailure(List<F> failures)
      Creates a failure result from some GuardrailResult.Failures.
      Parameters:
      failures - The failures
      Returns:
      A GuardrailResult containing the failures
    • createSuccess

      protected abstract R createSuccess()
      Creates a success result.
      Returns:
      A GuardrailResult representing success
    • createGuardrailException

      protected abstract GuardrailException createGuardrailException(String message, Throwable cause)
      Creates a GuardrailException 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

      public C config()
      Description copied from interface: GuardrailExecutor
      The GuardrailsConfig to use for configuration of the guardrail execution
      Specified by:
      config in interface GuardrailExecutor<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

      public List<G> guardrails()
      Description copied from interface: GuardrailExecutor
      Retrieves the guardrails associated with the implementation.
      Specified by:
      guardrails in interface GuardrailExecutor<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

      protected R validate(P params, G guardrail)
      Validates a guardrail against a set of params.

      If any kind of Exception is thrown during validation, it will be wrapped in a GuardrailException.

      Parameters:
      params - The GuardrailRequest to validate
      guardrail - The Guardrail to evaluate against
      Returns:
      The GuardrailResult of the validation
      Throws:
      GuardrailException - If any kind of Exception is thrown during validation
    • handleFatalResult

      protected R handleFatalResult(R accumulatedResult, R result)
      Handles a fatal result.
      Parameters:
      accumulatedResult - The accumulated result
      result - The fatal result
      Returns:
      The fatal result, possibly wrapped/modified in some way
    • executeGuardrails

      protected R executeGuardrails(P params)
    • composeResult

      protected R composeResult(R oldResult, R newResult)