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";