71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
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
|