feat: 修剪用户输入并清理大模型响应以优化输出格式
This commit is contained in:
parent
f8f0186665
commit
7bc5003674
@ -45,7 +45,7 @@ const agent = async (ctx: Context.Data) => {
|
|||||||
logger.debug(`Chat history: ${JSON.stringify(chatHistory)}`)
|
logger.debug(`Chat history: ${JSON.stringify(chatHistory)}`)
|
||||||
|
|
||||||
// 根据Mention,拼装原始消息
|
// 根据Mention,拼装原始消息
|
||||||
let userInput = rawMsgText
|
let userInput = rawMsgText.trim()
|
||||||
for (const mention of mentions ?? []) {
|
for (const mention of mentions ?? []) {
|
||||||
if (mention.name !== appInfo.app_name) {
|
if (mention.name !== appInfo.app_name) {
|
||||||
userInput = userInput.replace(mention.key, `@${mention.name}`)
|
userInput = userInput.replace(mention.key, `@${mention.name}`)
|
||||||
@ -57,7 +57,7 @@ const agent = async (ctx: Context.Data) => {
|
|||||||
// 调用大模型
|
// 调用大模型
|
||||||
try {
|
try {
|
||||||
await updateCard(cardGender.genPendingCard("LLM激情输出中,请稍等..."))
|
await updateCard(cardGender.genPendingCard("LLM激情输出中,请稍等..."))
|
||||||
const llmRes = await llm.invoke(
|
const llmRes = (await llm.invoke(
|
||||||
"groupAgent",
|
"groupAgent",
|
||||||
{
|
{
|
||||||
userName: historyMentions.get(openId || "") ?? "用户",
|
userName: historyMentions.get(openId || "") ?? "用户",
|
||||||
@ -66,11 +66,14 @@ const agent = async (ctx: Context.Data) => {
|
|||||||
time: new Date().toLocaleString("zh-CN", { timeZone: "Asia/Shanghai" }),
|
time: new Date().toLocaleString("zh-CN", { timeZone: "Asia/Shanghai" }),
|
||||||
},
|
},
|
||||||
requestId
|
requestId
|
||||||
)
|
)) as string
|
||||||
logger.info(
|
logger.info(
|
||||||
`LLM invoked successfully, see detail: http://langfuse.ai.srv/project/cm1j2tkj9001gukrgdvc1swuw/sessions/${requestId}`
|
`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) {
|
} catch (error: any) {
|
||||||
logger.error(`Failed to invoke llm: ${error.message}`)
|
logger.error(`Failed to invoke llm: ${error.message}`)
|
||||||
await updateCard(cardGender.genErrorCard("LLM调用失败: " + error.message))
|
await updateCard(cardGender.genErrorCard("LLM调用失败: " + error.message))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user