Class AbstractGuardrailService

java.lang.Object
dev.langchain4j.service.guardrail.AbstractGuardrailService
All Implemented Interfaces:
GuardrailService

public abstract class AbstractGuardrailService extends Object implements GuardrailService
Responsible for managing and applying input and output guardrails to methods of a specified AI service class. Guardrails are defined through annotations at either the class or method level and are used to enforce constraints, validation, or transformation logic on inputs and outputs of AI service methods. This class handles the initialization, configuration, and execution of input and output guardrail logic for the methods of the associated AI service class, ensuring both input and output constraints are applied automatically when methods are invoked.
  • Constructor Details

  • Method Details

    • aiServiceClass

      public Class<?> aiServiceClass()
      Description copied from interface: GuardrailService
      Retrieves the class representing the AI service to which the guardrails apply.
      Specified by:
      aiServiceClass in interface GuardrailService
      Returns:
      The Class object representing the AI service.
    • executeInputGuardrails

      public <MethodKey> InputGuardrailResult executeInputGuardrails(MethodKey method, InputGuardrailRequest params)
      Description copied from interface: GuardrailService
      Executes the input guardrails associated with a given Method
      Specified by:
      executeInputGuardrails in interface GuardrailService
      Type Parameters:
      MethodKey - > The type of the method key, representing a unique identifier for methods.
      Parameters:
      method - The method whose input guardrails are to be executed.
      params - The parameters to validate against the input guardrails. Must not be null.
      Returns:
      The result of executing the input guardrails, encapsulated in an InputGuardrailResult. If no guardrails are associated with the method, a successful result is returned by default.
    • executeOutputGuardrails

      public <MethodKey> OutputGuardrailResult executeOutputGuardrails(MethodKey method, OutputGuardrailRequest params)
      Description copied from interface: GuardrailService
      Executes the output guardrails associated with a given Method.
      Specified by:
      executeOutputGuardrails in interface GuardrailService
      Type Parameters:
      MethodKey - > The type of the method key, representing a unique identifier for methods.
      Parameters:
      method - The method whose output guardrails are to be executed.
      params - The parameters to validate against the output guardrails. Must not be null.
      Returns:
      The result of executing the output guardrails, encapsulated in an OutputGuardrailResult. If no guardrails are associated with the method, a successful result is returned by default.
    • hasInputGuardrails

      public <MethodKey> boolean hasInputGuardrails(MethodKey method)
      Description copied from interface: GuardrailService
      Whether or not a method has any input guardrails associated with it
      Specified by:
      hasInputGuardrails in interface GuardrailService
      Type Parameters:
      MethodKey - > The type of the method key, representing a unique identifier for methods.
      Parameters:
      method - The method
      Returns:
      true If method has input guardrails. false otherwise
    • hasOutputGuardrails

      public <MethodKey> boolean hasOutputGuardrails(MethodKey method)
      Description copied from interface: GuardrailService
      Whether or not a method has any output guardrails associated with it
      Specified by:
      hasOutputGuardrails in interface GuardrailService
      Type Parameters:
      MethodKey - > The type of the method key, representing a unique identifier for methods.
      Parameters:
      method - The method
      Returns:
      true If method has output guardrails. false otherwise