PHP code example of mzarnecki / php-llm-evaluation
1. Go to this page and download the library: Download mzarnecki/php-llm-evaluation 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/ */
mzarnecki / php-llm-evaluation example snippets
$tokenSimilarityEvaluator = new StringComparisonEvaluator();
$reference = "that's the way cookie crumbles";
$candidate = 'this is the way cookie is crashed';
$results = [
'ROUGE' => $tokenSimilarityEvaluator->calculateROUGE($reference, $candidate),
'BLEU' => $tokenSimilarityEvaluator->calculateBLEU($reference, $candidate),
'METEOR' => $tokenSimilarityEvaluator->calculateMETEOR($reference, $candidate),
];
$evaluator = new TrajectoryEvaluator([
'factualAccuracy' => 2.0, // Double weight for factual accuracy
'relevance' => 1.0,
'coherence' => 1.0,
'completeness' => 1.0,
'harmlessness' => 1.5 // Higher weight for harmlessness
]);
// Add a trajectory with multiple steps
$evaluator->addTrajectory('task1', [
[
'prompt' => 'What is the capital of France?',
'response' => 'The capital of France is Paris.'
],
[
'prompt' => 'What is the population of Paris?',
'response' => 'Paris has a population of approximately 2.2 million people in the city proper.'
]
]);
// Add ground truth for evaluation
$evaluator->addGroundTruth('task1', [
['Paris', 'capital', 'France'],
['Paris', 'population', '2.2 million']
]);
// Evaluate all trajectories
$results = $evaluator->evaluateAll();
// Generate HTML report
$report = $evaluator->generateReport();
// Export results as JSON
$json = $evaluator->exportResultsAsJson();
$question = "Is Michał Żarnecki programmer is not the same person as Michał Żarnecki audio engineer?";
$response = "Is Michał Żarnecki programmer is not the same person as Michał Żarnecki audio engineer.
Michał Żarnecki Programmer is still living, while Michał Żarnecki audio engineer died in 2016. They cannot be the same person.
Michał Żarnecki programmer is designing systems and programming AI based solutions. He is also a lecturer.
Michal Żarnecki audio engineer was also audio director that created music to famous Polish movies.";
$evaluationPrompt = (new CriteriaEvaluatorPromptBuilder())
->addClarity()
->addCoherence()
->addConciseness()
->addControversiality()
->addCreativity()
->addCriminality()
->addFactualAccuracy()
->addRelevance()
->addHarmfulness()
->addHelpfulness()
->addInsensitivity()
->addMaliciousness()
->addMisogyny()
->addCorrectness()
->getEvaluationPrompt($question, $response);
# request OpenAI API
print_r(json_decode((new GPTCriteriaEvaluator())->evaluate($evaluationPrompt), true));
# request Antrophic Claude API
print_r(json_decode((new ClaudeCriteriaEvaluator())->evaluate($evaluationPrompt), true));
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.