PHP code example of jeffreyvanrossum / contextr

1. Go to this page and download the library: Download jeffreyvanrossum/contextr 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/ */

    

jeffreyvanrossum / contextr example snippets


$contextr = new Contextr\Contextr(provider: new Contextr\Providers\OpenAi(apiKey: 'API_KEY'));

// Or for Grok:

$contextr = new Contextr\Contextr(provider: new Contextr\Providers\Grok(apiKey: 'API_KEY'));

$check = $contextr->spam()
    ->text('Buy cheap viagra now!!! Click here: shady.link')
    ->context([
        'topic' => 'Health Forum Discussion',
        'user_history' => 'First time poster'
    ])
    ->withReasoning()
    ->analyze();

$check->data();         // full result array
$check->spam();         // true (boolean)
$check->confidence();   // 0.95 (float)
$check->reasoning();    // "Contains promotional content and suspicious link"

$check = $contextr->sentiment()
    ->text('This blu ray was great, too bad it did not ory' => 'Blu ray'
    ])
    ->withReasoning()
    ->analyze();

$check->data();         // full result array
$check->sentiment();    // neutral (string)
$check->confidence();   // 0.75 (float)
$check->reasoning();    // "Expresses enjoyment of the blu ray but also disappointment about the absence of a specific content." (string)

$check = $contextr->moderation()
    ->text('These morons don’t even know how to kick a ball properly!')
    ->rules(['hate speech', 'profanity', 'civility'])
    ->context([
        'platform' => 'sports news website',
        'topic' => 'Premier League match review'
    ])
    ->withReasoning()
    ->withViolations()
    ->analyze();

$check->data();         // full result array
$check->violates();     // true (boolean)
$check->confidence();   // 0.75 (float)
$check->violations();   // ['profanity', 'civility'] (array)
$check->reasoning();    // "Contains insulting language and lacks respectful tone"

$check = $contextr->ai()
    ->text('The strategic intricacies of modern football necessitate a comprehensive understanding of player positioning, tactical adaptability, and cohesive team synergy to achieve superior performance outcomes.')
    ->context([
        'platform' => 'football fan forum',
        'topic' => 'Post-match discussion: Manchester United vs. Liverpool',
        'user_history' => 'New account, posted 5 similar analyses in 24 hours'
    ])
    ->withReasoning()
    >analyze();

$check->data();         // full result array
$check->ai();           // true (boolean)
$check->confidence();   // 0.92 (float)
$check->reasoning();    // "Overly polished language and generic analysis typical of AI-generated text, especially given the user's pattern of similar posts."