Files
2026-09-10 15:19:17 +08:00

42 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
// Row-level comparison diagnostic: prints normalized identity keys and match types so a
// scoring mismatch can be traced. Herb identities only; no patient data or report text.
$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\PrescriptionAiPolicy;
use think\facade\Db;
$batchId = (int) ($argv[1] ?? 6);
$model = (string) ($argv[2] ?? 'qwen');
$cipher = new PrescriptionAiCipher();
$batch = Db::name('prescription_ai_batch')->where('id', $batchId)->find();
$doctor = $cipher->decrypt((string) $batch['prescription_cipher'], 'prescription');
$doctor['herbs'] = PrescriptionAiPolicy::decode($doctor['herbs'] ?? []);
$doctor['aux_usage'] = PrescriptionAiPolicy::decode($doctor['aux_usage'] ?? []);
$row = Db::name('prescription_ai_result')->where('batch_id', $batchId)->where('model_key', $model)->find();
$body = $cipher->decrypt((string) $row['body_cipher'], 'result:' . $row['batch_id'] . ':' . $row['model_key']);
$catalog = Medicine::where('status', 1)->whereNull('delete_time')->field(['id', 'name', 'unit'])->order('id')->select()->toArray();
$comparison = PrescriptionAiComparison::compare($doctor, (array) ($body['candidate'] ?? []), $catalog);
$rows = array_map(static fn (array $r): array => [
'key' => $r['key'], 'name' => $r['name'], 'role' => $r['formula_type'], 'match' => $r['match_type'],
'doctor' => $r['doctor_dosage'], 'candidate' => $r['candidate_dosage'], 'contribution' => $r['contribution'],
], $comparison['rows']);
echo json_encode([
'batch' => $batchId, 'model' => $model, 'score' => $comparison['score'], 'herb_score' => $comparison['herb_score'],
'doctor_count' => $comparison['doctor_count'], 'candidate_count' => $comparison['candidate_count'],
'matched' => $comparison['matched_count'],
'doctor_defaults' => count($comparison['normalization']['doctor']['defaults']),
'doctor_merges' => $comparison['normalization']['doctor']['merges'],
'rows' => $rows,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";