LLM Output Guard (Message/EnhanceOutputGuard.php)

synaplan · backend/src/Service/Message/EnhanceOutputGuard.php · php · Apache-2.0
ai, security, lang:php, tag:validation

Validates LLM responses by checking length ratios and specific tokens to detect refusals. Use this to ensure model outputs adhere to expected formatting constraints.

// Source: https://github.com/metadist/synaplan | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
<?php

declare(strict_types=1);

namespace App\Service\Message;

/**
 * Detects when an LLM answered with commentary instead of a rewritten message.
 *
 * Design: no keyword lists and no regular expressions — only input/output length
 * ratios and a simple markdown-prefix check. Any refusal phrasing in any language
 * that explodes length vs. a short chat line is caught. The prompt contract
 * (see PromptCatalog tools:enhance) requires __UNENHANCEABLE__ when no rewrite exists.
 */
final class EnhanceOutputGuard
{
    private const UNENHANCEABLE_TOKEN = '__UNENHANCEABLE__';

    /** Abov

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key