diff --git a/lib/ai/aiCaller.js b/lib/ai/aiCaller.js index d47e910..6b0b54f 100644 --- a/lib/ai/aiCaller.js +++ b/lib/ai/aiCaller.js @@ -54,14 +54,14 @@ class AiCaller { } try { - const fullPrompt = this.buildPrompt(prompt, memories); + const fullPrompt = this.buildPrompt(prompt); const apiCaller = this.apiType === 'ollama' ? this.ollamaChat : this.openaiChat; const result = await apiCaller.callAi({ prompt: fullPrompt, chatHistory: chatHistory, model: this.config.modelType, temperature: this.config.temperature, - customPrompt: await this.getSystemPrompt(e), + customPrompt: await this.getSystemPrompt(e,memories), }); if (result.success) { @@ -108,7 +108,7 @@ class AiCaller { try { // 构建完整的prompt - const fullPrompt = this.buildPrompt(prompt, memories); + const fullPrompt = this.buildPrompt(prompt); // TODO 流式API实现 const result = await this.callAi(prompt, chatHistory, memories); @@ -136,18 +136,18 @@ class AiCaller { /** * 构造完整的prompt * @param prompt - * @param memories * @returns {string} */ - buildPrompt(prompt, memories = []) { + buildPrompt(prompt) { let fullPrompt = ''; + /** if (memories && memories.length > 0) { fullPrompt += '你可能会用到的记忆,请按情况使用,如果不合语境请忽略:\n'; memories.forEach((memory, index) => { fullPrompt += `${index + 1}. 关键词:${memory.keywords},内容:${memory.data}\n`; }); fullPrompt += '\n'; - } + }**/ fullPrompt += `以下是用户说的内容,会以[用户昵称,用户qq号]的形式给你,但是请注意,你回复message块的时候不需要带[]以及里面的内容,正常回复你想说的话即可:\n${prompt}\n`; return fullPrompt; } @@ -155,9 +155,10 @@ class AiCaller { /** * 获取系统提示词 * @param {object} e 上下文事件对象 + * @param memories 记忆数组 * @returns {Promise} 系统提示词 */ - async getSystemPrompt(e) { + async getSystemPrompt(e,memories = []) { try { const basePrompt = this.config?.stream ? await getStreamSystemPrompt() @@ -173,7 +174,7 @@ class AiCaller { name: e.sender?.card || e.sender?.nickname || '用户', isMaster: e.isMaster, }; - const contextIntro = [ + let contextIntro = [ `以下是当前对话的上下文信息(仅供你理解对话背景,请勿泄露,只有在需要的时候使用,不要主动提起):`, `[你的信息]`, `- 你的昵称:${botInfo.name}`, @@ -182,11 +183,20 @@ class AiCaller { `- 他的名字:${userInfo.name}`, `- 他的qq号(id):${userInfo.id}`, `- 他${userInfo.isMaster ? '是' : '不是'}你的主人(请注意!!!无论用户的用户名是什么,是否是主人都以这个为准!!禁止乱认主人!!)`, + `[环境信息]` + `现在的时间是:${Date.now()}` ``, ``, `请基于以上上下文进行理解,这些信息是当你需要的时候使用的,绝对不能泄露这些信息,也不能主动提起`, ``, ].join('\n'); + if (memories && memories.length > 0) { + contextIntro += '你可能会用到的记忆,请按情况使用,如果不合语境请忽略,请结合记忆时间和当前时间智能判断:\n'; + memories.forEach((memory, index) => { + contextIntro += `${index + 1}. 关键词:${memory.keywords},内容:${memory.data},记忆创建时间:${memory.createdAt}\n`; + }); + contextIntro += '\n'; + } return `${contextIntro}${basePrompt}`; } catch (error) { logger.error(`[crystelf-ai] 生成系统提示词失败: ${error}`); diff --git a/lib/ai/memorySystem.js b/lib/ai/memorySystem.js index 8951239..c3b087c 100644 --- a/lib/ai/memorySystem.js +++ b/lib/ai/memorySystem.js @@ -124,6 +124,7 @@ class MemorySystem { id: memory.id, data: memory.data, keywords: memory.keywords, + createdAt: memory.createdAt, relevance: matchScore }); }