71 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Context } from "../../types"
import llm from "../../utils/llm"
/**
* 生成网页简报
* @param {Context} ctx - 上下文对象
* @param {string} userDescription - 用户描述
* @param {any} content - 网页内容
* @returns {Promise<string>} - 返回简报内容
* @throws {Error} - 当生成简报失败时抛出错误
*/
export const generateAnswer = async (
ctx: Context,
webSearchResults:
| {
siteName: string
summary: string
}[]
| null
): Promise<string> => {
const {
requestId,
larkBody: { msgText },
} = ctx
const llmRes = (await llm.invoke(
"searchAgent",
{
webSearchResults,
time: new Date().toLocaleString("zh-CN", { timeZone: "Asia/Shanghai" }),
userInput: msgText,
},
requestId,
1
)) as string
if (!llmRes) throw new Error("模型侧错误")
return llmRes
}
const agent = async (ctx: Context, query: string) => {
const {
larkService: { message },
attachService,
larkCard,
logger,
} = ctx
const cardGender = larkCard.child("searchAgent")
try {
// 发送一个loading卡片
await message.updateOrReply(
cardGender.genPendingCard("正在检索网络信息,请稍等片刻~")
)
const searchRes = await attachService.webSearch(query)
await message.updateOrReply(
cardGender.genPendingCard("LLM输出中请稍等...")
)
const answer = await generateAnswer(ctx, searchRes)
await message.updateOrReply(cardGender.genSuccessCard(answer))
} catch (error: any) {
logger.error("检索Agent调用失败", { error: error.message })
await message.updateOrReply(
cardGender.genErrorCard(`检索失败: ${error.message}`)
)
}
}
const searchAgent = {
agent,
}
export default searchAgent