refactor(memory): 优化系统提示词构建

This commit is contained in:
Jerry 2025-11-09 01:48:40 +08:00
parent a275763d4a
commit 26a76e8ca0
2 changed files with 19 additions and 8 deletions

View File

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

View File

@ -124,6 +124,7 @@ class MemorySystem {
id: memory.id, id: memory.id,
data: memory.data, data: memory.data,
keywords: memory.keywords, keywords: memory.keywords,
createdAt: memory.createdAt,
relevance: matchScore relevance: matchScore
}); });
} }