Interface OutputGuardrail

All Superinterfaces:
Guardrail<OutputGuardrailRequest,OutputGuardrailResult>
All Known Implementing Classes:
JsonExtractorOutputGuardrail

public interface OutputGuardrail extends Guardrail<OutputGuardrailRequest,OutputGuardrailResult>
An output guardrail is a rule that is applied to the output of the model to ensure that the output is safe and meets the expectations.

In the case of reprompting, the reprompt message is added to the LLM context and the request is retried.

The maximum number of retries is configurable, defaulting to OutputGuardrailsConfig.MAX_RETRIES_DEFAULT.

  • Method Details

    • validate

      default OutputGuardrailResult validate(AiMessage responseFromLLM)
      Validates the response from the LLM.
      Parameters:
      responseFromLLM - the response from the LLM
    • validate

      default OutputGuardrailResult validate(OutputGuardrailRequest params)
      Validates the response from the LLM.

      Unlike validate(AiMessage), this method allows to access the memory and the augmentation result (in the case of a RAG).

      Implementation must not attempt to write to the memory or the augmentation result.

      Specified by:
      validate in interface Guardrail<OutputGuardrailRequest,OutputGuardrailResult>
      Parameters:
      params - the parameters, including the response from the LLM, the memory, and the augmentation result.
      Returns:
      The result of the validation
    • success

      default OutputGuardrailResult success()
      Produces a successful result without any successful text
      Returns:
      The result of a successful output guardrail validation.
    • successWith

      default OutputGuardrailResult successWith(String successfulText)
      Produces a successful result with specific success text
      Parameters:
      successfulText - The text of the successful result.
      Returns:
      The result of a successful output guardrail validation with a specific text.
    • successWith

      default OutputGuardrailResult successWith(String successfulText, Object successfulResult)
      Produces a non-fatal failure
      Parameters:
      successfulText - The text of the successful result.
      successfulResult - The object generated by this successful result.
      Returns:
      The result of a successful output guardrail validation with a specific text.
    • failure

      default OutputGuardrailResult failure(String message)
      Produces a non-fatal failure
      Parameters:
      message - A message describing the failure.
      Returns:
      The result of a failed output guardrail validation.
    • failure

      default OutputGuardrailResult failure(String message, Throwable cause)
      Produces a non-fatal failure
      Parameters:
      message - A message describing the failure.
      cause - The exception that caused this failure.
      Returns:
      The result of a failed output guardrail validation.
    • fatal

      default OutputGuardrailResult fatal(String message)
      Produces a fatal failure
      Parameters:
      message - A message describing the failure.
      Returns:
      The result of a fatally failed output guardrail validation, blocking the evaluation of any other subsequent validation.
    • fatal

      default OutputGuardrailResult fatal(String message, Throwable cause)
      Produces a fatal failure
      Parameters:
      message - A message describing the failure.
      cause - The exception that caused this failure.
      Returns:
      The result of a fatally failed output guardrail validation, blocking the evaluation of any other subsequent validation.
    • retry

      default OutputGuardrailResult retry(String message)
      Parameters:
      message - A message describing the failure.
      Returns:
      The result of a fatally failed output guardrail validation, blocking the evaluation of any other subsequent validation and triggering a retry with the same user prompt.
    • retry

      default OutputGuardrailResult retry(String message, Throwable cause)
      Parameters:
      message - A message describing the failure.
      cause - The exception that caused this failure.
      Returns:
      The result of a fatally failed output guardrail validation, blocking the evaluation of any other subsequent validation and triggering a retry with the same user prompt.
    • reprompt

      default OutputGuardrailResult reprompt(String message, String reprompt)
      Parameters:
      message - A message describing the failure.
      reprompt - The new prompt to be used for the retry.
      Returns:
      The result of a fatally failed output guardrail validation, blocking the evaluation of any other subsequent validation and triggering a retry with a new user prompt.
    • reprompt

      default OutputGuardrailResult reprompt(String message, Throwable cause, String reprompt)
      Parameters:
      message - A message describing the failure.
      cause - The exception that caused this failure.
      reprompt - The new prompt to be used for the retry.
      Returns:
      The result of a fatally failed output guardrail validation, blocking the evaluation of any other subsequent validation and triggering a retry with a new user prompt.