From bce94f52a006813c02c5729e54bd619a28f5768b Mon Sep 17 00:00:00 2001 From: abearxiong Date: Wed, 25 Jun 2025 11:35:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=A4=B8=E4=B8=80?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agent/analyze/cmd.ts | 13 +++--- src/agent/fix/prompt.ts | 6 ++- src/agent/test/cmd.ts | 2 +- src/agent/test/prompt-fix.ts | 3 +- src/agent/test/tools/kuaren.ts | 16 ++++++++ src/agent/tools/kuaren.ts | 72 +++++++++++++++++++++++++++++++++- src/agent/xhs.ts | 63 +++++++++++------------------ src/task/worker.ts | 2 +- 8 files changed, 124 insertions(+), 53 deletions(-) create mode 100644 src/agent/test/tools/kuaren.ts diff --git a/src/agent/analyze/cmd.ts b/src/agent/analyze/cmd.ts index 8d3ec8e..f40575f 100644 --- a/src/agent/analyze/cmd.ts +++ b/src/agent/analyze/cmd.ts @@ -6,7 +6,7 @@ import { logger } from '../logger.ts'; export const cmdList: { category: string; description?: string; - callPath?: { + action?: { path?: string; key?: string; }; @@ -14,7 +14,7 @@ export const cmdList: { { category: '指令夸人', description: `进行夸奖`, - callPath: { + action: { path: 'tools', key: 'good-job', }, @@ -28,8 +28,10 @@ agent description: '分析文本内容,意图分析,判断对应的指令内容', }) .define(async (ctx) => { - const text = ctx.query?.text || ''; - + let text = ctx.query?.text || ''; + if (text.length > 40) { + text = text.slice(0, 40).trim(); + } let result = { category: 'default', }; @@ -51,8 +53,6 @@ ${cmdList.map((item) => `- ${item.category}: ${item.description}`).join('\n')} ${text} `; - const now = Date.now(); - console.log('start'); const res = await ai .chat( [ @@ -72,7 +72,6 @@ ${text} return err; }); - console.log('end', Date.now() - now, 'ms'); const ans = res.choices[0]?.message?.content || ''; if (!ans) { logger.error('Empty response from AI:', res); diff --git a/src/agent/fix/prompt.ts b/src/agent/fix/prompt.ts index 72f0f7e..946bdf2 100644 --- a/src/agent/fix/prompt.ts +++ b/src/agent/fix/prompt.ts @@ -22,7 +22,7 @@ agent { role: 'user', content: ` -你是一个提示词优化的专家,请根据用户提供的提示词进行修正和优化,其中用户的提示词返回的要求如果没有或者不明确,请你都修正为要求返回的文本在500字以内,且内容是纯文本格式,不能是markdown模式,也不包含任何HTML标签或其他格式化内容。 +你是一个提示词优化的专家,请根据用户提供的提示词进行修正和优化,其中用户的提示词返回的要求如果没有或者不明确,请你都修正为要求返回的文本在500字以内,如果有,则要求在500字内。同时要求内容是纯文本格式,不能是markdown模式,也不包含任何HTML标签或其他格式化内容。 只对提示词进行优化,并且不需要对内容进行分析或总结。并返回修改后的总的提示词内容。 @@ -34,6 +34,10 @@ agent 分析一下这个图片 优化后的提示词 请分析一下这个图片,要求返回的内容是纯文本格式,字数不超过500字。 +示例3. 用户提示词 +分析一下这个笔记,300字内 +优化后的提示词 +请分析一下这个笔记,要求返回的内容是纯文本格式,字数不超过300字。 用户的提示词是 diff --git a/src/agent/test/cmd.ts b/src/agent/test/cmd.ts index afda872..0cb68ce 100644 --- a/src/agent/test/cmd.ts +++ b/src/agent/test/cmd.ts @@ -9,7 +9,7 @@ const main = async () => { path: 'analyze', key: 'cmd', payload: { - text: text4 + text: text3 }, }); console.log('analyze category res', res.code, 'content', res.body); diff --git a/src/agent/test/prompt-fix.ts b/src/agent/test/prompt-fix.ts index 1513cb8..f6b978d 100644 --- a/src/agent/test/prompt-fix.ts +++ b/src/agent/test/prompt-fix.ts @@ -6,11 +6,12 @@ const main = async () => { const text2 = '告诉我1+1的值'; const text3 = 'html和css的大纲是什么?'; const text4 = '1+1='; + const text5 = '请分析一下这个图片,300字内'; const res = await agent.call({ path: 'fix', key: 'xhs', payload: { - text: text, + text: text5, }, }); console.log('fix xhs res', res.code, 'content', res.body); diff --git a/src/agent/test/tools/kuaren.ts b/src/agent/test/tools/kuaren.ts new file mode 100644 index 0000000..68ad20c --- /dev/null +++ b/src/agent/test/tools/kuaren.ts @@ -0,0 +1,16 @@ +import { agent } from '../../index.ts'; + +const main = async () => { + const text1 = '你长得真好看啊'; + const text2 = '你说话是撒了魔法金粉吗?听一句我灵魂都被净化了!'; + + const res = await agent.call({ + path: 'tools', + key: 'good-job', + payload: { + text: text2, + }, + }); + console.log('good job res', res.code, 'content', res.body); +}; +main(); diff --git a/src/agent/tools/kuaren.ts b/src/agent/tools/kuaren.ts index 6453a7b..7e88b71 100644 --- a/src/agent/tools/kuaren.ts +++ b/src/agent/tools/kuaren.ts @@ -71,6 +71,58 @@ const kuarenPrompt = `### 浮夸夸人 #### 当前的场景是 `; +const pickGoodJobPrompt = `对提供的文字,提取单个的夸奖内容,并丰富为纯口语化模式,同时在括号中添加对应的姿态语言描述,同时添加了括号中的姿态语言描述,使其更具临场感和情感色彩。 + +要求: +1. 只返回单个的一条夸奖内容,不能有其他内容。 +2. 夸奖内容要口语化,富有情感色彩。 +3. 姿态语言描述要符合夸奖内容,且要在括号中描述。 +4. 夸奖内容要有夸张的比喻和形容词,突出对方的优点和成就。 +5. 夸奖内容要让人感到被认可和赞赏,能够激励对方。 +6. 不要返回任何其他内容或解释,只返回夸奖内容。 +7. 夸奖内容要简洁明了,易于理解,篇幅不易过长。 + +当前文字是: + +`; + +agent + .route({ + path: 'tools', + key: 'pick-good-job', + description: '对用户的内容进行夸奖后,提取出夸奖的内容', + }) + .define(async (ctx) => { + let { text } = ctx.query; + if (!text) { + text = '真厉害啊'; + } + + const prompt = `${pickGoodJobPrompt} ${text}`; + const res = await ai + .chat( + [ + { + role: 'user', + content: prompt, + }, + ], + { + enable_thinking: false, + }, + ) + .catch((err) => { + console.error('AI service error:', err.status); + ctx.throw(500, 'AI service error: ' + err.status); + return err; + }); + const ans = res.choices[0]?.message?.content || ''; + if (!ans) { + ctx.throw(500, 'AI response is empty'); + } + ctx.body = ans; + }) + .addTo(agent); agent .route({ path: 'tools', @@ -103,7 +155,23 @@ agent if (!ans) { ctx.throw(500, 'AI response is empty'); } - ctx.body = ans; - console.log('AI response:', ans); + // ctx.body = ans; + // console.log('AI response:', ans); + const resPick = await agent.call({ + path: 'tools', + key: 'pick-good-job', + payload: { + text: ans, + }, + }); + if (resPick.code !== 200) { + ctx.throw(500, 'AI pick good job error: ' + resPick.message); + return; + } + const pickAns = resPick.body || ''; + if (!pickAns) { + ctx.throw(500, 'AI pick good job response is empty'); + } + ctx.body = pickAns; }) .addTo(agent); diff --git a/src/agent/xhs.ts b/src/agent/xhs.ts index c8cfc43..1f53cb1 100644 --- a/src/agent/xhs.ts +++ b/src/agent/xhs.ts @@ -1,6 +1,7 @@ import { nanoid } from 'nanoid'; import { agent } from './agent.ts'; import { ai } from './ai.ts'; +import { cmdList } from './analyze/cmd.ts'; /** * 清除文本中的@信息 * @param text @@ -10,33 +11,6 @@ const clearAtInfo = (text: string = '') => { return newText.trim(); }; -const cmdList: { - cmd: string; - description?: string; - callPath?: { - path?: string; - key?: string; - }; -}[] = [ - { - cmd: '指令夸人', - description: `进行夸奖`, - callPath: { - path: 'tools', - key: 'good-job', - }, - }, -]; -const getTextCmd = (text: string, cmdList = []) => { - const text20 = text.length > 20 ? text.slice(0, 20) : text; - const cmd = cmdList.find((item) => text20.includes(item.cmd)); - if (cmd) { - return cmd; - } - return { - cmd: undefined, - }; -}; agent .route({ path: 'xhs', @@ -45,20 +19,29 @@ agent const { text = '' } = ctx.query || {}; const id = nanoid(); const no_at_text = clearAtInfo(text); - const cmd = getTextCmd(no_at_text, cmdList); - if (cmd.cmd) { - const newText = no_at_text.replace(cmd.cmd, '').trim(); - if (cmd.callPath) { - const res = await agent.call({ - path: cmd.callPath.path, - key: cmd.callPath.key, - payload: { - text: newText, - }, - }); - ctx.body = res.body || ''; + const some_text = no_at_text.length > 20 ? no_at_text.slice(0, 20) : no_at_text; + const hasCmd = some_text.includes('指令'); + if (hasCmd) { + const analyzeRes = await agent.call({ + path: 'analyze', + key: 'cmd', + payload: { + text: no_at_text, + }, + }); + if (analyzeRes.code === 200) { + const cmd = analyzeRes.body?.cmd; + if (cmd) { + const res = await agent.call({ + ...cmd.action, + payload: { + text: no_at_text, + }, + }); + ctx.body = res.body || ''; + return; + } } - return; } const resFix = await agent.call({ path: 'fix', diff --git a/src/task/worker.ts b/src/task/worker.ts index 56cdf60..b504def 100644 --- a/src/task/worker.ts +++ b/src/task/worker.ts @@ -96,7 +96,7 @@ export const worker = new Worker( worker.on('completed', async (job) => { const jobCounts = await queue.getJobCounts('waiting', 'wait', 'delayed'); if (job.name !== 'unread') { - console.log('job completed', job.name, job.id, job.returnvalue, jobCounts.delayed, jobCounts.wait); + console.log('job completed', job.name, 'run id', job.id, job.returnvalue, jobCounts.delayed, jobCounts.wait); } if (jobCounts.delayed + jobCounts.wait > 0) { // console.log('======has jobs, no need to add new job');