Guardrails

Guardrails are lightweight, programmable checkpoints that sit between application and the LLM. After each model response they run an evaluator of your choice (e.g. JSON-syntax checker, “no fallback” detector). Based on the result, either pass the answer through, retry the call, block it, or route it to a custom callback.

$llm = new OpenAIChat();

$guardrails = new Guardrails(llm: $llm);
$guardrails->addStrategy(new JSONFormatEvaluator(), GuardrailStrategy::STRATEGY_RETRY);

$response = $guardrails->generateText('generate answer in JSON format with object that consists of "correctKey" as a key and "correctVal" as a value');

result without retry:

{some invalid JSON}

result after retry:

{
    "correctKey":"correctVal"
}

use multiple guardrails evaluators

 $llm = new OpenAIChat();

$guardrails = new Guardrails(llm: $llm);

 $guardrails->addStrategy(
         evaluator: new NoFallbackAnswerEvaluator(),
         strategy: GuardrailStrategy::STRATEGY_BLOCK
     )->addStrategy(
         evaluator: (new WordLimitEvaluator())->setWordLimit(1),
         strategy: GuardrailStrategy::STRATEGY_BLOCK,
         defaultMessage: "I'm unable to answer your question right now."
     );

 $response = $guardrails->generateText('some prompt message');

This is documentation for LLPhant.