64 lines
2.4 KiB
JavaScript
64 lines
2.4 KiB
JavaScript
import { tool } from "@langchain/core/tools";
|
|
import z from "zod";
|
|
import { DBModel } from "../../../../models/index.js";
|
|
|
|
const hospitalInfoQueryTool = tool(
|
|
async ({ hospitalName, department, doctorName }) => {
|
|
try {
|
|
if (!hospitalName) {
|
|
return { success: false, error: "hospitalName 不能为空" };
|
|
}
|
|
|
|
const filter = { name: { $regex: hospitalName, $options: "i" } };
|
|
const total = await DBModel.Organization.countDocuments(filter);
|
|
if (total === 0) {
|
|
return { success: false, error: `未找到名称包含「${hospitalName}」的机构` };
|
|
}
|
|
|
|
// 医院总量小,一次最多取 10 家,命中过多时由调用方细化名称
|
|
const orgs = await DBModel.Organization.find(filter).limit(10).lean();
|
|
|
|
const data = orgs.map((org) => {
|
|
const fa = org.forAgent ?? {};
|
|
let departments = fa.departments ?? [];
|
|
let doctors = fa.doctors ?? [];
|
|
if (department) {
|
|
departments = departments.filter((d) => d.name?.includes(department));
|
|
doctors = doctors.filter((d) => d.department?.includes(department));
|
|
}
|
|
if (doctorName) {
|
|
doctors = doctors.filter((d) => d.name?.includes(doctorName));
|
|
}
|
|
return {
|
|
_id: org._id,
|
|
name: org.name,
|
|
level: org.level ?? "",
|
|
address: org.address ?? {},
|
|
infoUpdatedAt: fa.updatedAt ?? null,
|
|
overview: fa.overview ?? "",
|
|
photos: fa.photos ?? [],
|
|
departments,
|
|
doctors,
|
|
guides: fa.guides ?? {},
|
|
};
|
|
});
|
|
|
|
return { success: true, total, data };
|
|
} catch (error) {
|
|
return { success: false, error: error.message };
|
|
}
|
|
},
|
|
{
|
|
name: "hospital_info_query",
|
|
description:
|
|
"查询医院就医信息:医院总览(门诊/挂号/缴费/医保/交通)、科室(位置/电话/就诊提示)、医生(职称/擅长/号别/坐班)、场景指南(初诊/住院/检查须知/老人服务)、照片。按医院名称模糊查询,可按科室、医生名过滤。最多返回 10 家医院信息。",
|
|
schema: z.object({
|
|
hospitalName: z.string().describe("医院名称,支持模糊匹配(如 协和)"),
|
|
department: z.string().optional().describe("科室名称过滤(如 风湿免疫科、放射科)"),
|
|
doctorName: z.string().optional().describe("医生姓名过滤"),
|
|
}),
|
|
}
|
|
);
|
|
|
|
export { hospitalInfoQueryTool };
|