Interface OutputGuardrail
Implementation should be exposed as a CDI bean, and the class name configured in OutputGuardrails.value()
annotation.
Implementation should throw a OutputGuardrail.ValidationException
when the validation fails. The exception can indicate whether the
request should be retried and provide a reprompt
message.
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.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final record
Represents the parameter passed tovalidate(OutputGuardrailParams)
.static class
An exception thrown when the validation fails. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
validate
(dev.langchain4j.data.message.AiMessage responseFromLLM) Validates the response from the LLM.default void
Validates the response from the LLM.
-
Method Details
-
validate
default void validate(dev.langchain4j.data.message.AiMessage responseFromLLM) throws OutputGuardrail.ValidationException Validates the response from the LLM.If the validation fails with an exception that is not a
OutputGuardrail.ValidationException
, no retry will be attempted.- Parameters:
responseFromLLM
- the response from the LLM- Throws:
OutputGuardrail.ValidationException
- the exception throws if the validation fails.
-
validate
default void validate(OutputGuardrail.OutputGuardrailParams params) throws OutputGuardrail.ValidationException 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).If the validation fails with an exception that is not a
OutputGuardrail.ValidationException
, no retry will be attempted.Implementation must not attempt to write to the memory or the augmentation result.
- Parameters:
params
- the parameters, including the response from the LLM, the memory (maybe null), and the augmentation result (maybe null). Cannot benull
- Throws:
OutputGuardrail.ValidationException
- the exception throws if the validation fails.
-