feat(escort): 病历与诉求支持多材料解析为四段摘要
新增独立病历整理 Agent 与异步解析任务,支持 PDF 文本、图片 OCR、 语音 ASR 与文字输入,统一整理为基础健康、检查治疗、用药和问诊诉求。 Agent 只产出草稿,订单本人在编辑页核对确认后才写入当前订单; 原件在任务结束、失败或取消时立即清除。 inquiry 增加三段规范字段,medicalHistory 保留为旧版兼容摘要, 并兼容旧客户端只写 medicalHistory 的更新方式。
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { ChatDeepSeek } from "@langchain/deepseek";
|
||||
import { z } from "zod";
|
||||
import config from "../../../config.js";
|
||||
import { normalizeInquiry, cleanInquiryText } from "../../../utils/inquiry.js";
|
||||
import { MEDICAL_INQUIRY_SYSTEM_PROMPT, buildMedicalInquiryPrompt } from "./prompts.js";
|
||||
|
||||
export const MedicalInquiryResultSchema = z.object({
|
||||
basicHealth: z.string().max(2000),
|
||||
treatmentHistory: z.string().max(2000),
|
||||
medications: z.string().max(2000),
|
||||
demand: z.string().max(2000),
|
||||
uncertainties: z.array(z.string().max(200)).max(10),
|
||||
});
|
||||
|
||||
export class MedicalInquiryAgent {
|
||||
constructor(options = {}) {
|
||||
this.structuredModel = options.structuredModel || null;
|
||||
}
|
||||
|
||||
_getStructuredModel() {
|
||||
if (this.structuredModel) return this.structuredModel;
|
||||
const model = new ChatDeepSeek({
|
||||
model: config.agent.deepseek.flashModel,
|
||||
apiKey: config.agent.deepseek.apiKey,
|
||||
temperature: 0,
|
||||
maxRetries: 1,
|
||||
});
|
||||
this.structuredModel = model.withStructuredOutput(MedicalInquiryResultSchema, {
|
||||
name: "medical_inquiry_summary",
|
||||
});
|
||||
return this.structuredModel;
|
||||
}
|
||||
|
||||
async parse({ materials, current = {} }) {
|
||||
if (!Array.isArray(materials) || materials.length === 0) {
|
||||
throw new Error("没有可整理的病历材料");
|
||||
}
|
||||
const result = await this._getStructuredModel().invoke([
|
||||
{ role: "system", content: MEDICAL_INQUIRY_SYSTEM_PROMPT },
|
||||
{ role: "user", content: buildMedicalInquiryPrompt({ materials, current: normalizeInquiry(current) }) },
|
||||
]);
|
||||
const parsed = MedicalInquiryResultSchema.parse(result);
|
||||
return {
|
||||
...normalizeInquiry(parsed),
|
||||
uncertainties: parsed.uncertainties.map((item) => cleanInquiryText(item, 200)).filter(Boolean),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const medicalInquiryAgent = new MedicalInquiryAgent();
|
||||
export default medicalInquiryAgent;
|
||||
Reference in New Issue
Block a user