mirror of
https://github.com/jie65535/JChatGPT.git
synced 2025-07-28 18:59:20 +08:00
Add Memory Agent
This commit is contained in:
parent
f822999ab4
commit
ea4e6123b3
@ -61,6 +61,7 @@ object JChatGPT : KotlinPlugin(
|
|||||||
// 注册聊天权限
|
// 注册聊天权限
|
||||||
PermissionService.INSTANCE.register(chatPermission, "JChatGPT Chat Permission")
|
PermissionService.INSTANCE.register(chatPermission, "JChatGPT Chat Permission")
|
||||||
PluginConfig.reload()
|
PluginConfig.reload()
|
||||||
|
PluginData.reload()
|
||||||
|
|
||||||
// 设置Token
|
// 设置Token
|
||||||
LargeLanguageModels.reload()
|
LargeLanguageModels.reload()
|
||||||
@ -144,6 +145,14 @@ object JChatGPT : KotlinPlugin(
|
|||||||
"与 \"${event.senderName}\" 私聊中"
|
"与 \"${event.senderName}\" 私聊中"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
replace("{memory}") {
|
||||||
|
val memoryText = PluginData.contactMemory[event.subject.id]
|
||||||
|
if (memoryText.isNullOrEmpty()) {
|
||||||
|
"暂无相关记忆"
|
||||||
|
} else memoryText
|
||||||
|
}
|
||||||
|
|
||||||
return prompt.toString()
|
return prompt.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -663,6 +672,9 @@ object JChatGPT : KotlinPlugin(
|
|||||||
// 发送组合消息
|
// 发送组合消息
|
||||||
SendCompositeMessage(),
|
SendCompositeMessage(),
|
||||||
|
|
||||||
|
// 记忆代理
|
||||||
|
MemoryAgent(),
|
||||||
|
|
||||||
// 结束循环
|
// 结束循环
|
||||||
StopLoopAgent(),
|
StopLoopAgent(),
|
||||||
|
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
package top.jie65535.mirai
|
package top.jie65535.mirai
|
||||||
|
|
||||||
import net.mamoe.mirai.console.data.AutoSavePluginData
|
import net.mamoe.mirai.console.data.AutoSavePluginData
|
||||||
|
import net.mamoe.mirai.console.data.value
|
||||||
|
|
||||||
object PluginData : AutoSavePluginData("data") {
|
object PluginData : AutoSavePluginData("data") {
|
||||||
|
/**
|
||||||
|
* 联系人记忆
|
||||||
|
*/
|
||||||
|
val contactMemory by value(mutableMapOf<Long, String>())
|
||||||
}
|
}
|
42
src/main/kotlin/tools/MemoryAgent.kt
Normal file
42
src/main/kotlin/tools/MemoryAgent.kt
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
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 MemoryAgent : BaseAgent(
|
||||||
|
tool = Tool.function(
|
||||||
|
name = "remember",
|
||||||
|
description = "更新当前记忆块。新增记忆时请带上原记忆,否则会被覆盖!",
|
||||||
|
parameters = Parameters.buildJsonObject {
|
||||||
|
put("type", "object")
|
||||||
|
putJsonObject("properties") {
|
||||||
|
putJsonObject("memory") {
|
||||||
|
put("type", "string")
|
||||||
|
put("description", "新记忆内容,无序列表文本块。")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
putJsonArray("required") {
|
||||||
|
add("memory")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
override suspend fun execute(args: JsonObject?, event: MessageEvent): String {
|
||||||
|
requireNotNull(args)
|
||||||
|
val contactId = event.subject.id
|
||||||
|
val memoryText = args.getValue("memory").jsonPrimitive.content
|
||||||
|
JChatGPT.logger.info("Remember ($contactId): $memoryText")
|
||||||
|
PluginData.contactMemory[contactId] = memoryText
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user