feat: 修剪用户输入并清理大模型响应以优化输出格式

This commit is contained in:
zhaoyingbo 2024-12-02 09:00:44 +00:00
parent f8f0186665
commit 7bc5003674

View File

@ -45,7 +45,7 @@ const agent = async (ctx: Context.Data) => {
logger.debug(`Chat history: ${JSON.stringify(chatHistory)}`)
// 根据Mention拼装原始消息
let userInput = rawMsgText
let userInput = rawMsgText.trim()
for (const mention of mentions ?? []) {
if (mention.name !== appInfo.app_name) {
userInput = userInput.replace(mention.key, `@${mention.name}`)
@ -57,7 +57,7 @@ const agent = async (ctx: Context.Data) => {
// 调用大模型
try {
await updateCard(cardGender.genPendingCard("LLM激情输出中请稍等..."))
const llmRes = await llm.invoke(
const llmRes = (await llm.invoke(
"groupAgent",
{
userName: historyMentions.get(openId || "") ?? "用户",
@ -66,11 +66,14 @@ const agent = async (ctx: Context.Data) => {
time: new Date().toLocaleString("zh-CN", { timeZone: "Asia/Shanghai" }),
},
requestId
)
)) as string
logger.info(
`LLM invoked successfully, see detail: http://langfuse.ai.srv/project/cm1j2tkj9001gukrgdvc1swuw/sessions/${requestId}`
)
await updateCard(cardGender.genSuccessCard(llmRes))
const cleanedLlmRes = llmRes
.replace(/```(\w+)?\n([\s\S]*?)```/g, "$2")
.trim()
await updateCard(cardGender.genSuccessCard(cleanedLlmRes))
} catch (error: any) {
logger.error(`Failed to invoke llm: ${error.message}`)
await updateCard(cardGender.genErrorCard("LLM调用失败: " + error.message))