Interface OutputGuardrail

All Superinterfaces:
Guardrail<OutputGuardrailParams,OutputGuardrailResult>
All Known Implementing Classes:
AbstractJsonExtractorOutputGuardrail

@Experimental("This feature is experimental and the API is subject to change") public interface OutputGuardrail extends Guardrail<OutputGuardrailParams,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.

Implementation should be exposed as a CDI bean, and the class name configured in OutputGuardrails.value() annotation.

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 using quarkus.langchain4j.guardrails.max-retries, defaulting to 3.

  • Method Details

    • validate

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

      default OutputGuardrailResult validate(OutputGuardrailParams 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<OutputGuardrailParams,OutputGuardrailResult>
      Parameters:
      params - the parameters, including the response from the LLM, the memory (maybe null), and the augmentation result (maybe null). Cannot be null
      Returns:
      The result of this validation.
    • success

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

      default OutputGuardrailResult successWith(String successfulText)
      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)
      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)
      Parameters:
      message - A message describing the failure.
      Returns:
      The result of a failed output guardrail validation.
    • failure

      default OutputGuardrailResult failure(String message, Throwable cause)
      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)
      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)
      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.