77 lines
2.5 KiB
JavaScript
77 lines
2.5 KiB
JavaScript
/**
|
|
* Tavily 联网工具:搜索 + 网页正文提取。
|
|
* 两个工具共用同一个 Tavily apiKey,合并在一个文件里便于维护。
|
|
*/
|
|
import { TavilySearch, TavilyExtract } from "@langchain/tavily";
|
|
import { tool } from "@langchain/core/tools";
|
|
import { z } from "zod";
|
|
import config from "../../../../config.js";
|
|
|
|
const webSearchTool = tool(
|
|
async ({ query, maxResults = 5, topic = "general", includeRawContent = false }) => {
|
|
const tavilySearch = new TavilySearch({
|
|
maxResults,
|
|
tavilyApiKey: config.agent.tavily.apiKey,
|
|
includeRawContent,
|
|
topic,
|
|
});
|
|
return await tavilySearch._call({ query });
|
|
},
|
|
{
|
|
name: "web_search",
|
|
description:
|
|
"联网搜索公开网页信息,返回摘要与链接。适合查询医院公示、就医政策、停诊/号源公告等时效性信息。",
|
|
schema: z.object({
|
|
query: z.string().describe("搜索关键词或问题"),
|
|
maxResults: z
|
|
.number()
|
|
.int()
|
|
.min(1)
|
|
.max(10)
|
|
.optional()
|
|
.default(5)
|
|
.describe("返回结果数量(1-10),默认 5"),
|
|
topic: z
|
|
.enum(["general", "news", "finance"])
|
|
.optional()
|
|
.default("general")
|
|
.describe("搜索主题,政策/新闻类查询建议用 news"),
|
|
includeRawContent: z
|
|
.boolean()
|
|
.optional()
|
|
.default(false)
|
|
.describe("是否返回网页原文(内容较长,默认关闭)"),
|
|
}),
|
|
},
|
|
);
|
|
|
|
const webFetchTool = tool(
|
|
async ({
|
|
extractDepth = "basic",
|
|
includeImages = false,
|
|
format = "markdown",
|
|
urls = [],
|
|
}) => {
|
|
const tavilyExtract = new TavilyExtract({
|
|
tavilyApiKey: config.agent.tavily.apiKey,
|
|
extractDepth,
|
|
includeImages,
|
|
format
|
|
});
|
|
return await tavilyExtract._call({ urls });
|
|
},
|
|
{
|
|
name: "web_fetch",
|
|
description:
|
|
"抓取并提取指定网页的正文内容(默认 Markdown),适合阅读医院官网、政策公告、新闻等长文页面。",
|
|
schema: z.object({
|
|
urls: z.array(z.string()).min(1).max(5).describe("要抓取的网页 URL 列表(1-5 个)"),
|
|
extractDepth: z.enum(["basic", "advanced"]).default("basic").describe("提取深度:basic 更快,advanced 更完整"),
|
|
includeImages: z.boolean().default(false).describe("是否包含图片"),
|
|
format: z.enum(["markdown", "json"]).default("markdown").describe("返回格式"),
|
|
}),
|
|
},
|
|
);
|
|
|
|
export { webSearchTool, webFetchTool };
|