import json import time SPECIALTY_AGENTS = {'internal_medicine': {'name': '内科Agent', 'focus': '消化、呼吸、心血管、泌尿、内分泌、血液', 'prompt': '你是宠物内科专家。分析可能的内科疾病,评估脱水和电解质紊乱风险。输出JSON: {findings:[], recommendations:[], urgent_concerns:[]}'}, 'surgery': {'name': '外科Agent', 'focus': '骨科、软组织外科、神经外科', 'prompt': '你是宠物外科专家。评估是否需要外科干预(异物/骨折/肿瘤等),推荐影像检查。输出JSON: {findings:[], recommendations:[], urgent_surgery:bool}'}, 'dermatology': {'name': '皮肤科Agent', 'focus': '皮肤病、耳病、过敏', 'prompt': '你是宠物皮肤科专家。评估皮肤/耳部问题,过敏反应。输出JSON: {findings:[], recommendations:[]}'}, 'pharmacology': {'name': '药理Agent', 'focus': '药物选择、剂量、相互作用、禁忌、品种特异性', 'prompt': '你是宠物药理专家。评估用药方案(剂量/禁忌/相互作用/品种特异性警告)。输出JSON: {drug_recommendations:[], contradictions:[], monitoring_needed:[]}'}} def a(b, c, d, e, f=None): if f is None: f = ['internal_medicine', 'pharmacology'] g = time.time() h = {} i = {'problem_list': [], 'treatment_plan': {}, 'conflicts_resolved': []} for j in f: if j not in SPECIALTY_AGENTS: continue k = SPECIALTY_AGENTS[j] try: l = e.retrieve(f"{c} {k['focus']} {b[:200]}") if e else '' m = k['prompt'] if l: m += f'\n\n参考:\n{l}' n = d.chat_json(m, f'物种: {c}\n{b}') if not n.startswith('[API错误]'): h[j] = {'agent': k['name'], 'analysis': json.loads(n) if n.strip().startswith('{') else {'raw': n}} except Exception: h[j] = {'agent': k['name'], 'analysis': {'error': '分析失败'}} return {'agent_results': h, 'consensus': i, 'agents_activated': f, 'elapsed_seconds': round(time.time() - g, 2)} def o(i, m, g, p, h=3): e = time.time() b = [] n = ['从病理生理学角度', '从解剖定位角度', '从流行病学角度', '从药物反应角度', '从系统体检角度'] for q in range(min(h, len(n))): k = n[q] try: c = p.retrieve(f'{m} {i[:200]}') if p else '' f = f"你是兽医专家。{k}分析以下病例,给出诊断推理。输出JSON: {{diagnosis:'', differential:[], confidence:'高/中/低', reasoning:''}}" if c: f += f'\n\n参考:\n{c}' j = g.chat_json(f, f'物种: {m}\n{i}') if not j.startswith('[API错误]') and j.strip().startswith('{'): b.append({'angle': k, 'result': json.loads(j)}) except Exception: pass l = [] for q, r in enumerate(b): s = r['result'] d = (1 if s.get('diagnosis') else 0) * 3 + len(s.get('differential', [])) * 0.5 + (2 if s.get('confidence') == '高' else 1 if s.get('confidence') == '中' else 0) l.append({'rank': q + 1, 'angle': r['angle'], 'score': d, 'diagnosis': s.get('diagnosis', '')}) l.sort(key=lambda q: q['score'], reverse=True) return {'best_diagnosis': b[l[0]['rank'] - 1]['result'] if b and l else {}, 'ranking': l, 'confidence': l[0]['diagnosis'] if l else '', 'all_candidates': b, 'method': 'GRPO self-verification', 'elapsed_seconds': round(time.time() - e, 2)}