PHP code example of marcinwolnyeu / slopshape-php

1. Go to this page and download the library: Download marcinwolnyeu/slopshape-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

marcinwolnyeu / slopshape-php example snippets


$score = (new Detector())->detect($text);   // a 720-word text

$score->probabilityAi;     // float, e.g. 0.926
$score->confidence;        // float, e.g. 0.498

use function SlopShape\probabilityAi;

if (probabilityAi($text, $bridge) > 0.8) {
    // ...
}

use SlopShape\Detector;

$client = OpenAI::client(getenv('OPENAI_API_KEY'));

$detector = new Detector(static function (string $prompt) use ($client): string {
    $response = $client->responses()->create([
        'model' => 'gpt-6-luna',   // swap in any model you prefer
        'instructions' => 'You are a precise annotator. You answer with JSON only.',
        'input' => $prompt,
        'max_output_tokens' => 4000,
    ]);

    return $response->outputText ?? '';
});

$score = $detector->detect($text);

echo $score, "\n";
// 91/100  P[AI]=0.913  likely AI-generated  confidence 0.50  9/10 core values read

use SlopShape\Bridge\LlmBridge;

final readonly class OpenRouterBridge implements LlmBridge
{
    public function __construct(
        private string $apiKey,
        private string $model = 'openai/gpt-6-luna',
        private int $timeoutSeconds = 120,
    ) {}

    public function complete(string $prompt): string
    {
        $curl = curl_init('https://openrouter.ai/api/v1/chat/completions');

        curl_setopt_array($curl, [
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => $this->timeoutSeconds,
            CURLOPT_HTTPHEADER => [
                'Authorization: Bearer ' . $this->apiKey,
                'Content-Type: application/json',
            ],
            CURLOPT_POSTFIELDS => json_encode([
                'model' => $this->model,
                'messages' => [
                    ['role' => 'system', 'content' => 'You are a precise annotator. You answer with JSON only.'],
                    ['role' => 'user', 'content' => $prompt],
                ],
                'max_tokens' => 4000,
            ], JSON_THROW_ON_ERROR),
        ]);

        $body = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);

        if (false === $body || 200 !== $status) {
            throw new RuntimeException(sprintf(
                'OpenRouter request failed (HTTP %d): %s',
                $status,
                false === $body ? curl_error($curl) : $body,
            ));
        }

        $data = json_decode($body, true, flags: JSON_THROW_ON_ERROR);

        return $data['choices'][0]['message']['content']
            ?? throw new RuntimeException('OpenRouter returned no answer: ' . $body);
    }
}

use SlopShape\Detector;

$detector = new Detector(new OpenRouterBridge(getenv('OPENROUTER_API_KEY')));

$score = $detector->detect($text);

printf("P[AI] = %.3f\n", $score->probabilityAi);

echo "\nEvidence for AI:\n";
foreach ($score->positiveFactors() as $factor) {
    printf("  %+0.4f  %s: %s\n", $factor->contribution, $factor->feature, $factor->value);
}

echo "\nEvidence for human:\n";
foreach ($score->negativeFactors() as $factor) {
    printf("  %+0.4f  %s: %s\n", $factor->contribution, $factor->feature, $factor->value);
}
bash
composer 
bash
composer