initialize(); use app\common\service\prescriptionai\PrescriptionAiGenerator; $model = (string) ($argv[1] ?? 'qwen'); $context = [ 'source_hash' => hash('sha256', 'synthetic-probe-' . $model), 'missing' => [], 'files' => [], 'source' => [ 'dispensing' => ['formulation' => '浓缩水丸', 'unit' => 'g', 'dose_basis' => 'per_dose'], 'patient' => ['age' => 52, 'gender' => 1], 'records' => [[ 'source_id' => 'diagnoses:1', 'kind' => 'diagnoses', 'data' => [ 'chief_complaint' => '示例:乏力、口干三个月,无发热', 'allergy_history' => '示例:明确否认药物过敏', 'current_medications' => '示例:未使用中西药', 'tongue' => '示例:舌淡红苔薄白', ], ]], ], '_comparison_catalog' => [ ['id' => 1, 'name' => '生黄芪'], ['id' => 2, 'name' => '党参'], ['id' => 3, 'name' => '麸炒白术'], ['id' => 4, 'name' => '茯苓'], ['id' => 5, 'name' => '生麦冬'], ['id' => 6, 'name' => '五味子'], ], ]; $progress = []; $started = microtime(true); $result = PrescriptionAiGenerator::generate($model, $context, static function (array $state) use (&$progress): bool { $progress = $state; return true; }); echo json_encode([ 'model' => $model, 'ok' => (bool) ($result['ok'] ?? false), 'error_code' => $result['error_code'] ?? '', 'retryable' => $result['retryable'] ?? null, 'candidate_status' => $result['candidate']['status'] ?? null, 'candidate_herb_count' => is_array($result['candidate']['herbs'] ?? null) ? count($result['candidate']['herbs']) : null, 'prompt_version' => $result['prompt_version'] ?? '', 'wall_seconds' => round(microtime(true) - $started, 1), 'total_calls' => $progress['usage']['total_calls'] ?? null, 'format_rejects' => $progress['format_rejects'] ?? [], 'calls' => array_map(static fn (array $call): array => [ 'stage' => $call['stage'], 'ok' => $call['ok'], 'latency_ms' => $call['latency_ms'], 'input_bytes' => $call['input_token_upper_bound'], 'error_code' => $call['error_code'], 'completion_tokens' => $call['usage']['completion_tokens'] ?? null, ], (array) ($progress['usage']['calls'] ?? [])), ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";