@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class SyntheticAiAssessment
|
||||
{
|
||||
/** @param array<int,array<string,mixed>> $claims
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function assess(array $claims): array
|
||||
{
|
||||
$facts = app(SyntheticAiRehearsal::class)->preview()['facts'];
|
||||
$ids = array_column($facts, 'id');
|
||||
Validator::make(['claims' => $claims], ['claims' => 'required|array|list|min:1|max:3', 'claims.*' => 'required|array:text,source_ids',
|
||||
'claims.*.text' => 'required|string|max:1000', 'claims.*.source_ids' => 'required|array|list|min:1|max:3',
|
||||
'claims.*.source_ids.*' => ['required', 'string', Rule::in($ids)]])->validate();
|
||||
$byId = array_column($facts, 'text', 'id');
|
||||
$cited = [];
|
||||
$exact = 0;
|
||||
foreach ($claims as $claim) {
|
||||
$sourceIds = array_values(array_unique($claim['source_ids']));
|
||||
$cited = [...$cited, ...$sourceIds];
|
||||
$exact += in_array(trim($claim['text']), array_map(fn ($id) => $byId[$id], $sourceIds), true) ? 1 : 0;
|
||||
}
|
||||
$missing = array_values(array_diff($ids, $cited));
|
||||
|
||||
return ['method' => 'synthetic_ai_assessment_v1', 'facts' => count($facts), 'claims' => count($claims),
|
||||
'cited_facts' => count(array_unique($cited)), 'uncited_fact_ids' => $missing, 'citation_coverage_complete' => $missing === [],
|
||||
'exact_source_text_matches' => $exact, 'requires_human_review' => true, 'semantic_accuracy' => 'not_automatically_verified'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user