Package dev.langchain4j.guardrail
Interface OutputGuardrail
- All Superinterfaces:
Guardrail<OutputGuardrailRequest,
OutputGuardrailResult>
- All Known Implementing Classes:
JsonExtractorOutputGuardrail
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 Summary
Modifier and TypeMethodDescriptiondefault OutputGuardrailResult
Produces a non-fatal failuredefault OutputGuardrailResult
Produces a non-fatal failuredefault OutputGuardrailResult
Produces a fatal failuredefault OutputGuardrailResult
Produces a fatal failuredefault OutputGuardrailResult
default OutputGuardrailResult
default OutputGuardrailResult
default OutputGuardrailResult
default OutputGuardrailResult
success()
Produces a successful result without any successful textdefault OutputGuardrailResult
successWith
(String successfulText) Produces a successful result with specific success textdefault OutputGuardrailResult
successWith
(String successfulText, Object successfulResult) Produces a non-fatal failuredefault OutputGuardrailResult
Validates the response from the LLM.default OutputGuardrailResult
validate
(OutputGuardrailRequest 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<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
Produces a successful result without any successful text- Returns:
- The result of a successful output guardrail validation.
-
successWith
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
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
Produces a non-fatal failure- Parameters:
message
- A message describing the failure.- Returns:
- The result of a failed output guardrail validation.
-
failure
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
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
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
- 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.
-