From 9cca629b87ba71a3d6ae05afd15174c53414c835 Mon Sep 17 00:00:00 2001 From: XuChenXu <91937041+ChenXu233@users.noreply.github.com> Date: Tue, 17 Dec 2024 22:56:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=B0=E5=BF=86=E7=B3=BB=E7=BB=9F=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=20(#29)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :sparkles: 添加记忆系统 * :art: black优化格式 * :bug: 删除apscheduler * :sparkles: 将记忆插件转换为插件形式 --- .gitignore | 4 +- nonebot_plugin_marshoai/azure.py | 4 +- .../plugins/marshoai_memory/__init__.py | 60 +++++++++++++++++++ .../plugins/marshoai_memory/tools.json | 17 ++++++ .../tools/marshoai_memory/__init__.py | 45 ++++++++++++++ .../tools/marshoai_memory/tools.json | 46 ++++++++++++++ .../tools_wip/marshoai_memory/__init__.py | 2 - .../tools_wip/marshoai_memory/tools.json | 21 ------- package.json | 23 +++---- pyproject.toml | 5 +- 10 files changed, 187 insertions(+), 40 deletions(-) create mode 100644 nonebot_plugin_marshoai/plugins/marshoai_memory/__init__.py create mode 100644 nonebot_plugin_marshoai/plugins/marshoai_memory/tools.json create mode 100644 nonebot_plugin_marshoai/tools/marshoai_memory/__init__.py create mode 100644 nonebot_plugin_marshoai/tools/marshoai_memory/tools.json delete mode 100755 nonebot_plugin_marshoai/tools_wip/marshoai_memory/__init__.py delete mode 100755 nonebot_plugin_marshoai/tools_wip/marshoai_memory/tools.json diff --git a/.gitignore b/.gitignore index 2dd6d7d9..1904f119 100755 --- a/.gitignore +++ b/.gitignore @@ -187,4 +187,6 @@ docs/.vitepress/cache docs/.vitepress/dist # viztracer -result.json \ No newline at end of file +result.json + +data/* \ No newline at end of file diff --git a/nonebot_plugin_marshoai/azure.py b/nonebot_plugin_marshoai/azure.py index 51ade472..09a1142b 100644 --- a/nonebot_plugin_marshoai/azure.py +++ b/nonebot_plugin_marshoai/azure.py @@ -252,7 +252,9 @@ async def marsho( nicknames = await get_nicknames() user_nickname = nicknames.get(user_id, "") if user_nickname != "": - nickname_prompt = f"\n*此消息的说话者:{user_nickname}*" + nickname_prompt = ( + f"\n*此消息的说话者id为:{user_id},名字为:{user_nickname}*" + ) else: nickname_prompt = "" # 用户名无法获取,暂时注释 diff --git a/nonebot_plugin_marshoai/plugins/marshoai_memory/__init__.py b/nonebot_plugin_marshoai/plugins/marshoai_memory/__init__.py new file mode 100644 index 00000000..9c28db07 --- /dev/null +++ b/nonebot_plugin_marshoai/plugins/marshoai_memory/__init__.py @@ -0,0 +1,60 @@ +from pathlib import Path +from nonebot import require +require("nonebot_plugin_localstore") +from nonebot_plugin_localstore import get_plugin_data_file +import json + +from nonebot_plugin_marshoai.plugin import PluginMetadata, on_function_call +from nonebot_plugin_marshoai.plugin.func_call.params import String + +__marsho_meta__ = PluginMetadata( + name="记忆保存", + author="MarshoAI", + description="这个插件可以帮助AI记住一些事情", +) + +memory_path = get_plugin_data_file("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) + + +@on_function_call(description="存储记忆内容").params( + memory=String(description="你想记住的内容,概括并保留关键内容"), + user_id=String(description="你想记住的人的id"), +) +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 "记忆已经保存啦~" + + +@on_function_call(description="读取记忆内容").params( + user_id=String(description="你想读取记忆的人的id") +) +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 用大模型对记忆进行整理 diff --git a/nonebot_plugin_marshoai/plugins/marshoai_memory/tools.json b/nonebot_plugin_marshoai/plugins/marshoai_memory/tools.json new file mode 100644 index 00000000..62758939 --- /dev/null +++ b/nonebot_plugin_marshoai/plugins/marshoai_memory/tools.json @@ -0,0 +1,17 @@ +[ + { + "type": "function", + "function": { + "name": "marshoai_memory__write_memory", + "description": "如果在上下中你看见并觉得应该记住的人的行为与事件,请调用这个函数,并将记忆内容写入。请尽量每次都调用,总结ta的习惯、爱好和性格,以及你对ta的印象和ta对你的印象;比如用户喜欢干什么吃什么。" + } + }, + { + "type": "function", + "function": { + "name": "marshoai_memory__read_memory", + "description": "每当你想要获取更多有关某人的信息的时候,请调用这个函数。" + } + } +] + diff --git a/nonebot_plugin_marshoai/tools/marshoai_memory/__init__.py b/nonebot_plugin_marshoai/tools/marshoai_memory/__init__.py new file mode 100644 index 00000000..556b1c19 --- /dev/null +++ b/nonebot_plugin_marshoai/tools/marshoai_memory/__init__.py @@ -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 用大模型对记忆进行整理 diff --git a/nonebot_plugin_marshoai/tools/marshoai_memory/tools.json b/nonebot_plugin_marshoai/tools/marshoai_memory/tools.json new file mode 100644 index 00000000..70dc9e96 --- /dev/null +++ b/nonebot_plugin_marshoai/tools/marshoai_memory/tools.json @@ -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" + ] + } + } +] + diff --git a/nonebot_plugin_marshoai/tools_wip/marshoai_memory/__init__.py b/nonebot_plugin_marshoai/tools_wip/marshoai_memory/__init__.py deleted file mode 100755 index 11a7891c..00000000 --- a/nonebot_plugin_marshoai/tools_wip/marshoai_memory/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -async def write_memory(memory: str): - return "" diff --git a/nonebot_plugin_marshoai/tools_wip/marshoai_memory/tools.json b/nonebot_plugin_marshoai/tools_wip/marshoai_memory/tools.json deleted file mode 100755 index 73e1c22b..00000000 --- a/nonebot_plugin_marshoai/tools_wip/marshoai_memory/tools.json +++ /dev/null @@ -1,21 +0,0 @@ -[ - { - "type": "function", - "function": { - "name": "marshoai_memory__write_memory", - "description": "当你想记住有关与你对话的人的一些信息的时候,调用此函数。", - "parameters": { - "type": "object", - "properties": { - "memory": { - "type": "string", - "description": "你想记住的内容,概括并保留关键内容。" - } - } - }, - "required": [ - "memory" - ] - } - } -] diff --git a/package.json b/package.json index bfa40ee3..2a53dad9 100755 --- a/package.json +++ b/package.json @@ -1,15 +1,10 @@ { - "type": "module", - "devDependencies": { - "vitepress": "^1.5.0", - "vitepress-sidebar": "^1.30.2" - }, - "scripts": { - "docs:dev": "vitepress dev docs --host", - "docs:build": "vitepress build docs", - "docs:preview": "vitepress preview docs" - }, - "dependencies": { - "vue": "^3.5.13" - } -} \ No newline at end of file + "type": "module", + "devDependencies": {"vitepress": "^1.5.0", "vitepress-sidebar": "^1.30.2"}, + "scripts": { + "docs:dev": "vitepress dev docs --host", + "docs:build": "vitepress build docs", + "docs:preview": "vitepress preview docs", + }, + "dependencies": {"vue": "^3.5.13"}, +} diff --git a/pyproject.toml b/pyproject.toml index ee13748a..5d2f8b71 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ authors = [ { name="LiteyukiStudio", email = "support@liteyuki.icu"} ] dependencies = [ - "nonebot2>=2.2.0", + "nonebot2>=2.4.0", "nonebot-plugin-alconna>=0.48.0", "nonebot-plugin-localstore>=0.7.1", "zhDatetime>=1.1.1", @@ -76,3 +76,6 @@ dev = [ test = [ "nonebug>=0.4.3", ] + +[tool.ruff.lint] +ignore = ["E402"]