1. Go to this page and download the library: Download neuron-core/llm-classifier 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/ */
neuron-core / llm-classifier example snippets
use NeuronAI\Providers\OpenAI\OpenAI;
use NeuronCore\Classifier\Calibration\Calibrator;
use NeuronCore\Classifier\Calibration\Grader\ExactMatchGrader;
use NeuronCore\Classifier\Calibration\Grader\LlmJudgeGrader;
use NeuronCore\Classifier\Calibration\GraderResolver;
use NeuronCore\Classifier\Calibration\SeedCorpus;
// The models we want to route BETWEEN — they take the test.
$panel = [
new OpenAI(apiKey: $cheapKey, model: 'gpt-4o-mini'),
new OpenAI(apiKey: $premiumKey, model: 'gpt-4o'),
];
// A separate "judge" model grades the answers. Keep it OUT of the panel.
$judge = new OpenAI(apiKey: $key, model: 'gpt-4o');
$artifact = (new Calibrator(
panel: $panel,
corpus: SeedCorpus::fromFile('seed.csv'),
graders: new GraderResolver([
// Mechanical check: the answer must match exactly.
'math' => new ExactMatchGrader(),
// No single right answer: let the judge compare to a rubric.
'writing' => new LlmJudgeGrader($judge),
]),
language: 'en',
fasttext: 'cc.en.300.vec', // download once from https://fasttext.cc/docs/en/crawl-vectors.html#models
))->run();
$artifact->writeTo('storage/model.script'); // ship this file with your app
use NeuronCore\Classifier\Classifier;
use NeuronAI\Chat\Messages\UserMessage;
use NeuronAI\Providers\OpenAI\OpenAI;
// Load ONCE — e.g. on app boot, or under Octane/RoadRunner/FrankenPHP workers.
$scorer = Classifier::load('storage/model.script');
// Use on every request:
// 1) Guard first: how much of this prompt does the classifier actually recognize?
// Low coverage = out-of-domain → don't trust the score, send to the strong model.
if ($scorer->coverage($userPrompt) < 0.4) {
$model = 'o1'; // unfamiliar territory → safest, most capable model
} else {
// 2) In-domain: route by difficulty. overall() returns ONE score in 0..1.
$score = $scorer->overall($userPrompt);
// Pick the model that's cheap enough for how easy this prompt is.
$model = match (true) {
$score < 0.33 => 'gpt-4o-mini', // easy → cheap & fast
$score < 0.70 => 'gpt-4o', // medium → solid all-rounder
default => 'o1', // hard → most capable
};
}
$provider = new OpenAI(apiKey: $key, model: $model);
$answer = $provider->chat(new UserMessage($userPrompt))->getContent();
if ($scorer->coverage($userPrompt) < 0.4) { // too many unknown words
$model = 'o1'; // don't trust the score → strongest model
} else {
$score = $scorer->classify($userPrompt)['math'] ?? 0.0;
// …route by score…
}
use NeuronCore\Classifier\Classifier;
$scorer = Classifier::load('storage/model.bin');
$score = $scorer->overall($userPrompt); // 0 = easy, 1 = hard
bash
# 1) one-time: download the fastText vectors
curl -O https://dl.fbaipublicfiles.com/fasttext/vectors-crawl/cc.en.300.vec.gz
gunzip cc.en.300.vec.gz
mv cc.en.300.vec storage/
# 2) Run calibration will generate the model file -> storage/model.bin
php script/routerbench.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.