feat: 第四轮功能增强 - LLM 思维记忆优化、DevTools 记忆UI、9个新工具、5分钟自我思考
- 优化 LLM 思维方式和记忆方法(类别/重要性/关键词/相似度合并/衰减) - DevTools 记忆查询 UI 重新设计(类别筛选/排序/星标/搜索) - 新增 9 个 LLM 工具:calculator, datetime, file_ops, http_request, json_ops, text, random, crypto, markdown - 管理员主对话 5 分钟自我思考增强(工具调用/记忆提取/记忆维护)
This commit is contained in:
@@ -125,12 +125,63 @@ func (b *Builder) Build(ctx context.Context, params BuildParams) ([]model.LLMMes
|
||||
Content: systemPrompt,
|
||||
})
|
||||
|
||||
// 2. 记忆注入 —— 相关记忆以系统消息形式注入
|
||||
// 2. 记忆注入 —— 相关记忆以系统消息形式注入,按重要性排序并分类标注
|
||||
if len(params.Memories) > 0 {
|
||||
memoryPrompt := "【以下是关于开拓者的一些重要记忆,请在合适的时机自然地提及】\n"
|
||||
for _, m := range params.Memories {
|
||||
memoryPrompt += fmt.Sprintf("- %s\n", m.Content)
|
||||
// 按 Importance 排序
|
||||
sortedMems := make([]memory.MemoryEntry, len(params.Memories))
|
||||
copy(sortedMems, params.Memories)
|
||||
sortMemoriesByImportance(sortedMems)
|
||||
|
||||
// 分离核心记忆和最近记忆
|
||||
var coreMems, recentMems, otherMems []memory.MemoryEntry
|
||||
for _, m := range sortedMems {
|
||||
if m.Importance >= 8 {
|
||||
coreMems = append(coreMems, m)
|
||||
} else if m.Importance >= 5 {
|
||||
recentMems = append(recentMems, m)
|
||||
} else {
|
||||
otherMems = append(otherMems, m)
|
||||
}
|
||||
}
|
||||
|
||||
// 限制每类记忆数量
|
||||
if len(coreMems) > 5 {
|
||||
coreMems = coreMems[:5]
|
||||
}
|
||||
if len(recentMems) > 8 {
|
||||
recentMems = recentMems[:8]
|
||||
}
|
||||
if len(otherMems) > 3 {
|
||||
otherMems = otherMems[:3]
|
||||
}
|
||||
|
||||
var memoryPrompt string
|
||||
memoryPrompt += "【以下是关于开拓者的重要记忆,请在合适的时机自然地提及】\n\n"
|
||||
|
||||
if len(coreMems) > 0 {
|
||||
memoryPrompt += "★ 核心记忆(非常重要,务必优先参考):\n"
|
||||
for _, m := range coreMems {
|
||||
memoryPrompt += formatMemoryLine(m)
|
||||
}
|
||||
memoryPrompt += "\n"
|
||||
}
|
||||
|
||||
if len(recentMems) > 0 {
|
||||
memoryPrompt += "● 常用记忆:\n"
|
||||
for _, m := range recentMems {
|
||||
memoryPrompt += formatMemoryLine(m)
|
||||
}
|
||||
memoryPrompt += "\n"
|
||||
}
|
||||
|
||||
if len(otherMems) > 0 {
|
||||
memoryPrompt += "○ 其他记忆:\n"
|
||||
for _, m := range otherMems {
|
||||
memoryPrompt += formatMemoryLine(m)
|
||||
}
|
||||
memoryPrompt += "\n"
|
||||
}
|
||||
|
||||
messages = append(messages, model.LLMMessage{
|
||||
Role: "system",
|
||||
Content: memoryPrompt,
|
||||
@@ -248,3 +299,32 @@ func acModeLabel(mode string) string {
|
||||
return mode
|
||||
}
|
||||
}
|
||||
|
||||
// sortMemoriesByImportance 按 Importance 降序排列记忆
|
||||
func sortMemoriesByImportance(mems []memory.MemoryEntry) {
|
||||
for i := 0; i < len(mems); i++ {
|
||||
for j := i + 1; j < len(mems); j++ {
|
||||
if mems[j].Importance > mems[i].Importance ||
|
||||
(mems[j].Importance == mems[i].Importance && mems[j].Priority > mems[i].Priority) {
|
||||
mems[i], mems[j] = mems[j], mems[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// formatMemoryLine 格式化单条记忆为展示行
|
||||
func formatMemoryLine(m model.MemoryEntry) string {
|
||||
content := m.Content
|
||||
runes := []rune(content)
|
||||
if len(runes) > 80 {
|
||||
content = string(runes[:80]) + "…"
|
||||
}
|
||||
stars := ""
|
||||
for i := 0; i < m.Importance/2; i++ {
|
||||
stars += "★"
|
||||
}
|
||||
if m.Importance%2 != 0 {
|
||||
stars += "☆"
|
||||
}
|
||||
return fmt.Sprintf("- [%s%s] %s\n", m.Category.DisplayName(), stars, content)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user