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 Summary
Modifier and TypeMethodDescriptiondefault OutputGuardrailResult
default OutputGuardrailResult
default OutputGuardrailResult
default OutputGuardrailResult
default OutputGuardrailResult
default OutputGuardrailResult
default OutputGuardrailResult
default OutputGuardrailResult
default OutputGuardrailResult
success()
default OutputGuardrailResult
successWith
(String successfulText) default OutputGuardrailResult
successWith
(String successfulText, Object successfulResult) default OutputGuardrailResult
validate
(dev.langchain4j.data.message.AiMessage responseFromLLM) Validates the response from the LLM.default OutputGuardrailResult
validate
(OutputGuardrailParams params) Validates the response from the LLM.
-
Method Details
-
validate
Validates the response from the LLM.- Parameters:
responseFromLLM
- the response from the LLM
-
validate
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 interfaceGuardrail<OutputGuardrailParams,
OutputGuardrailResult> - Parameters:
params
- the parameters, including the response from the LLM, the memory (maybe null), and the augmentation result (maybe null). Cannot benull
- Returns:
- The result of this validation.
-
success
- Returns:
- The result of a successful output guardrail validation.
-
successWith
- Parameters:
successfulText
- The text of the successful result.- Returns:
- The result of a successful output guardrail validation with a specific text.
-
successWith
- 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
- Parameters:
message
- A message describing the failure.- Returns:
- The result of a failed output guardrail validation.
-
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
- 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
- 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
- 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
- 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
- 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
- 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.
-