mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-marshoai.git
synced 2025-08-02 11:40:04 +00:00
记忆系统实现 (#29)
* ✨ 添加记忆系统 * 🎨 black优化格式 * 🐛 删除apscheduler * ✨ 将记忆插件转换为插件形式
This commit is contained in:
45
nonebot_plugin_marshoai/tools/marshoai_memory/__init__.py
Normal file
45
nonebot_plugin_marshoai/tools/marshoai_memory/__init__.py
Normal file
@ -0,0 +1,45 @@
|
||||
from pathlib import Path
|
||||
from nonebot import require
|
||||
|
||||
require("nonebot_plugin_localstore")
|
||||
from nonebot_plugin_localstore import get_data_file
|
||||
import json
|
||||
|
||||
memory_path = get_data_file("marshoai", "memory.json")
|
||||
if not Path(memory_path).exists():
|
||||
with open(memory_path, "w", encoding="utf-8") as f:
|
||||
json.dump({}, f, ensure_ascii=False, indent=4)
|
||||
print(memory_path)
|
||||
|
||||
|
||||
async def write_memory(memory: str, user_id: str):
|
||||
|
||||
with open(memory_path, "r", encoding="utf-8") as f:
|
||||
memory_data = json.load(f)
|
||||
|
||||
memorys = memory_data.get(user_id, [])
|
||||
memorys.append(memory)
|
||||
memory_data[user_id] = memorys
|
||||
|
||||
with open(memory_path, "w", encoding="utf-8") as f:
|
||||
json.dump(memory_data, f, ensure_ascii=False, indent=4)
|
||||
|
||||
return "记忆已经保存啦~"
|
||||
|
||||
|
||||
async def read_memory(user_id: str):
|
||||
with open(memory_path, "r", encoding="utf-8") as f:
|
||||
memory_data = json.load(f)
|
||||
memorys = memory_data.get(user_id, [])
|
||||
if not memorys:
|
||||
return "好像对ta还没有任何记忆呢~"
|
||||
|
||||
return "这些是有关ta的记忆:" + "\n".join(memorys)
|
||||
|
||||
|
||||
async def organize_memories():
|
||||
with open(memory_path, "r", encoding="utf-8") as f:
|
||||
memory_data = json.load(f)
|
||||
for i in memory_data:
|
||||
...
|
||||
# TODO 用大模型对记忆进行整理
|
46
nonebot_plugin_marshoai/tools/marshoai_memory/tools.json
Normal file
46
nonebot_plugin_marshoai/tools/marshoai_memory/tools.json
Normal file
@ -0,0 +1,46 @@
|
||||
[
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "marshoai_memory__write_memory",
|
||||
"description": "如果在上下中你看见并觉得应该记住的人的行为与事件,请调用这个函数,并将记忆内容写入。请尽量每次都调用,总结ta的习惯、爱好和性格,以及你对ta的印象和ta对你的印象",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"memory": {
|
||||
"type": "string",
|
||||
"description": "你想记住的内容,概括并保留关键内容。"
|
||||
},
|
||||
"user_id": {
|
||||
"type": "string",
|
||||
"description": "你想记住的人的id。"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"memory",
|
||||
"user_id"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "marshoai_memory__read_memory",
|
||||
"description": "每当你想要获取更多有关某人的信息的时候,请调用这个函数。",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"user_id": {
|
||||
"type": "string",
|
||||
"description": "你想获取的人的id。"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"user_id"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
|
Reference in New Issue
Block a user