AI text analysis with context: introducing contextr
I’ve been working on something lately, and I’m excited to let you in on it. Meet contextr, a PHP package that uses AI to analyze text. Super handy if you want to spot spam, determine sentiment, or enforce moderation rules. It even throws in confidence scores and reasoning, so you’re not left guessing why it flagged something.
Another AI wrapper
You might be thinking: "Ah, just another AI wrapper." And to be totally honest, you'd be right. But... I provide a nice interface to build a solid prompt and give you a normalized response.
Plenty of tools exist for spam detection and moderation, but they often don't allow for providing additional context or use fixed set of rules.
Alright, with that out of the way, let’s try it out!
Getting it running
Installing contextr is very easy. Just grab it with Composer:
composer require jeffreyvanrossum/contextr
Then, hook it up with an AI provider. Currently, contextr supports OpenAI or Grok; you can grab your API key by opening up account at the provider you choose.
In this example, we'll go with OpenAI:
$contextr = new Contextr\Contextr(provider: new Contextr\Providers\OpenAi(apiKey: 'YOUR_API_KEY'));
By default, it will use the gpt-4o-mini
model as this is the fastest and cheapest model while still providing pretty good results. If you go with Grok, by default, it will use the latest version of Grok 2. But, you're free to choose a a different model - as long as it supports structured response formats.
Detect spam
Here is an example of how you could check a text for spam. It's up to you to provide relevant context. For example, in a forum setting, you might also specify if this is a user's first post.
$check = $contextr->spam() ->text('Buy cheap viagra now!!! Click here: shady.link') ->context(['topic' => 'Health Forum Discussion']) ->withReasoning() ->analyze(); echo $check->spam(); // true echo $check->confidence(); // 0.95 echo $check->reasoning(); // "Contains promotional content and suspicious link"
Enforce policies
Automate content moderation with your custom rules.
$check = $contextr->moderation() ->text('These morons don’t even know how to kick a ball properly!') ->context([ 'platform' => 'sports news website', 'topic' => 'Premier League match review' ]) ->withReasoning() ->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"
By default, it will check for hate speech, profanity, and civility. You can set your own custom rules as well by adding the rules
method before analyze
is called.
$contextr->rules(['hate speech', 'profanity', 'civility', 'your own rule']);
Analyze sentiment
Detect the emotional tone of user feedback, reviews, or support tickets while considering context to improve accuracy.
$check = $contextr->sentiment() ->text('This blu ray was great, too bad it did not include Project A.') ->context([ 'product' => 'Jackie Chan Collection Vol 1983', 'category' => '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)
Written by AI?
Determine the likelihood that a text is AI-generated.
$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."
In closing
I think it's a pretty cool package, and though there might be room for improvement, I think it's a solid starting point to build upon. And, of course, feel free to share ideas and contribute over at GitHub yourself!