mirror of
https://github.com/jie65535/JChatGPT.git
synced 2025-07-28 18:59:20 +08:00
Split memory agent
This commit is contained in:
parent
c479282fe4
commit
3a5caf86a1
@ -548,7 +548,10 @@ object JChatGPT : KotlinPlugin(
|
|||||||
SendCompositeMessage(),
|
SendCompositeMessage(),
|
||||||
|
|
||||||
// 记忆代理
|
// 记忆代理
|
||||||
MemoryAgent(),
|
MemoryAppend(),
|
||||||
|
|
||||||
|
// 记忆修改
|
||||||
|
MemoryReplace(),
|
||||||
|
|
||||||
// 结束循环
|
// 结束循环
|
||||||
StopLoopAgent(),
|
StopLoopAgent(),
|
||||||
|
@ -8,4 +8,28 @@ object PluginData : AutoSavePluginData("data") {
|
|||||||
* 联系人记忆
|
* 联系人记忆
|
||||||
*/
|
*/
|
||||||
val contactMemory by value(mutableMapOf<Long, String>())
|
val contactMemory by value(mutableMapOf<Long, String>())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加对话记忆
|
||||||
|
*/
|
||||||
|
fun appendContactMemory(contactId: Long, newMemory: String) {
|
||||||
|
val memory = contactMemory[contactId]
|
||||||
|
if (memory.isNullOrEmpty()) {
|
||||||
|
contactMemory[contactId] = "* $newMemory"
|
||||||
|
} else {
|
||||||
|
contactMemory[contactId] = "$memory\n* $newMemory"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 替换对话记忆
|
||||||
|
*/
|
||||||
|
fun replaceContactMemory(contactId: Long, oldMemory: String, newMemory: String) {
|
||||||
|
val memory = contactMemory[contactId]
|
||||||
|
if (memory.isNullOrEmpty()) {
|
||||||
|
contactMemory[contactId] = "* $newMemory"
|
||||||
|
} else {
|
||||||
|
contactMemory[contactId] = memory.replace(oldMemory, newMemory)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -12,16 +12,16 @@ import net.mamoe.mirai.event.events.MessageEvent
|
|||||||
import top.jie65535.mirai.JChatGPT
|
import top.jie65535.mirai.JChatGPT
|
||||||
import top.jie65535.mirai.PluginData
|
import top.jie65535.mirai.PluginData
|
||||||
|
|
||||||
class MemoryAgent : BaseAgent(
|
class MemoryAppend : BaseAgent(
|
||||||
tool = Tool.function(
|
tool = Tool.function(
|
||||||
name = "remember",
|
name = "memoryAppend",
|
||||||
description = "用于更新当前记忆块,可以用于记住网友的特征和事实等。新增记忆时请带上原记忆,否则会被覆盖!",
|
description = "新增记忆项",
|
||||||
parameters = Parameters.buildJsonObject {
|
parameters = Parameters.buildJsonObject {
|
||||||
put("type", "object")
|
put("type", "object")
|
||||||
putJsonObject("properties") {
|
putJsonObject("properties") {
|
||||||
putJsonObject("memory") {
|
putJsonObject("memory") {
|
||||||
put("type", "string")
|
put("type", "string")
|
||||||
put("description", "新记忆内容,无序列表文本块。")
|
put("description", "记忆项")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
putJsonArray("required") {
|
putJsonArray("required") {
|
||||||
@ -34,9 +34,8 @@ class MemoryAgent : BaseAgent(
|
|||||||
requireNotNull(args)
|
requireNotNull(args)
|
||||||
val contactId = event.subject.id
|
val contactId = event.subject.id
|
||||||
val memoryText = args.getValue("memory").jsonPrimitive.content
|
val memoryText = args.getValue("memory").jsonPrimitive.content
|
||||||
JChatGPT.logger.info("Remember ($contactId): $memoryText")
|
JChatGPT.logger.info("Remember ($contactId): \"$memoryText\"")
|
||||||
PluginData.contactMemory[contactId] = memoryText
|
PluginData.appendContactMemory(contactId, memoryText)
|
||||||
|
|
||||||
return "OK"
|
return "OK"
|
||||||
}
|
}
|
||||||
}
|
}
|
47
src/main/kotlin/tools/MemoryReplace.kt
Normal file
47
src/main/kotlin/tools/MemoryReplace.kt
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package top.jie65535.mirai.tools
|
||||||
|
|
||||||
|
import com.aallam.openai.api.chat.Tool
|
||||||
|
import com.aallam.openai.api.core.Parameters
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import kotlinx.serialization.json.add
|
||||||
|
import kotlinx.serialization.json.jsonPrimitive
|
||||||
|
import kotlinx.serialization.json.put
|
||||||
|
import kotlinx.serialization.json.putJsonArray
|
||||||
|
import kotlinx.serialization.json.putJsonObject
|
||||||
|
import net.mamoe.mirai.event.events.MessageEvent
|
||||||
|
import top.jie65535.mirai.JChatGPT
|
||||||
|
import top.jie65535.mirai.PluginData
|
||||||
|
|
||||||
|
class MemoryReplace : BaseAgent(
|
||||||
|
tool = Tool.Companion.function(
|
||||||
|
name = "memoryReplace",
|
||||||
|
description = "替换记忆项",
|
||||||
|
parameters = Parameters.Companion.buildJsonObject {
|
||||||
|
put("type", "object")
|
||||||
|
putJsonObject("properties") {
|
||||||
|
putJsonObject("oldMemory") {
|
||||||
|
put("type", "string")
|
||||||
|
put("description", "原记忆项")
|
||||||
|
}
|
||||||
|
putJsonObject("newMemory") {
|
||||||
|
put("type", "string")
|
||||||
|
put("description", "新记忆项")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
putJsonArray("required") {
|
||||||
|
add("oldMemory")
|
||||||
|
add("newMemory")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
override suspend fun execute(args: JsonObject?, event: MessageEvent): String {
|
||||||
|
requireNotNull(args)
|
||||||
|
val contactId = event.subject.id
|
||||||
|
val oldMemoryText = args.getValue("oldMemory").jsonPrimitive.content
|
||||||
|
val newMemoryText = args.getValue("newMemory").jsonPrimitive.content
|
||||||
|
JChatGPT.logger.info("Replace memory ($contactId): \"$oldMemoryText\" -> \"$newMemoryText\"")
|
||||||
|
PluginData.replaceContactMemory(contactId, oldMemoryText, newMemoryText)
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user