All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkiverse.langchain4j.guardrails.GuardrailResult Maven / Gradle / Ivy

There is a newer version: 0.21.0
Show newest version
package io.quarkiverse.langchain4j.guardrails;

import java.util.List;

/**
 * The result of the validation of an interaction between a user and the LLM.
 */
public interface GuardrailResult {

    /**
     * The possible results of a guardrails validation.
     */
    enum Result {
        /**
         * A successful validation.
         */
        SUCCESS,
        /**
         * A successful validation with a specific result.
         */
        SUCCESS_WITH_RESULT,
        /**
         * A failed validation not preventing the subsequent validations eventually registered to be evaluated.
         */
        FAILURE,
        /**
         * A fatal failed validation, blocking the evaluation of any other validations eventually registered.
         */
        FATAL
    }

    Result getResult();

    default boolean isSuccess() {
        return getResult() == Result.SUCCESS || getResult() == Result.SUCCESS_WITH_RESULT;
    }

    default boolean hasRewrittenResult() {
        return getResult() == Result.SUCCESS_WITH_RESULT;
    }

    default GuardrailResult blockRetry() {
        throw new UnsupportedOperationException();
    }

    default String successfulText() {
        throw new UnsupportedOperationException();
    }

    default Object successfulResult() {
        throw new UnsupportedOperationException();
    }

    boolean isFatal();

    /**
     * @return The list of failures eventually resulting from a set of validations.
     */
    List failures();

    default Throwable getFirstFailureException() {
        if (!isSuccess()) {
            for (Failure failure : failures()) {
                if (failure.cause() != null) {
                    return failure.cause();
                }
            }
        }
        return null;
    }

    GR validatedBy(Class guardrailClass);

    /**
     * The message and the cause of the failure of a single validation.
     */
    interface Failure {
        Failure withGuardrailClass(Class guardrailClass);

        String message();

        Throwable cause();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy