Files
zyt/artifacts/prescription-ai-runtime/probe_real_comparison.php
T
2026-09-10 15:19:17 +08:00

52 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
// Read-only end-to-end rehearsal: runs one model branch on a frozen batch context with the
// current code and compares the fresh candidate against the frozen doctor prescription.
// Writes nothing. Prints identity mapping outcomes and scores; herb names only, no patient data.
$serverRoot = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'server' . DIRECTORY_SEPARATOR;
chdir($serverRoot);
require $serverRoot . 'vendor/autoload.php';
$app = new \think\App($serverRoot);
$app->initialize();
use app\common\model\doctor\Medicine;
use app\common\service\prescriptionai\PrescriptionAiCipher;
use app\common\service\prescriptionai\PrescriptionAiComparison;
use app\common\service\prescriptionai\PrescriptionAiGenerator;
use app\common\service\prescriptionai\PrescriptionAiPolicy;
use think\facade\Db;
$batchId = (int) ($argv[1] ?? 7);
$model = (string) ($argv[2] ?? 'qwen');
$cipher = new PrescriptionAiCipher();
$batch = Db::name('prescription_ai_batch')->where('id', $batchId)->find();
$context = $cipher->decrypt((string) $batch['context_cipher'], 'context');
$catalog = Medicine::where('status', 1)->whereNull('delete_time')->field(['id', 'name', 'unit'])->order('id')->select()->toArray();
$context['_comparison_catalog'] = $catalog;
$progress = [];
$result = PrescriptionAiGenerator::generate($model, $context, static function (array $state) use (&$progress): bool {
$progress = $state;
return true;
});
$doctor = $cipher->decrypt((string) $batch['prescription_cipher'], 'prescription');
$doctor['herbs'] = PrescriptionAiPolicy::decode($doctor['herbs'] ?? []);
$doctor['aux_usage'] = PrescriptionAiPolicy::decode($doctor['aux_usage'] ?? []);
$comparison = PrescriptionAiComparison::compare($doctor, (array) ($result['candidate'] ?? []), $catalog);
echo json_encode([
'batch' => $batchId, 'model' => $model, 'ok' => (bool) ($result['ok'] ?? false),
'error_code' => $result['error_code'] ?? '',
'candidate_names' => array_map(static fn (array $h): string => (string) $h['name'], (array) ($result['candidate']['herbs'] ?? [])),
'comparison' => [
'status' => $comparison['status'], 'reason_code' => $comparison['reason_code'],
'score' => $comparison['score'], 'herb_score' => $comparison['herb_score'],
'doctor_count' => $comparison['doctor_count'], 'candidate_count' => $comparison['candidate_count'],
'matched' => $comparison['matched_count'], 'issues' => count($comparison['normalization']['issues']),
],
'total_calls' => $progress['usage']['total_calls'] ?? null,
'stages' => array_column((array) ($progress['usage']['calls'] ?? []), 'stage'),
'format_rejects' => $progress['format_rejects'] ?? [],
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";