mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-marshoai.git
synced 2025-08-01 03:59:51 +00:00
✨ 移动记忆保存插件的相关代码
This commit is contained in:
@ -1,97 +0,0 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from azure.ai.inference.models import UserMessage
|
||||
from nonebot import get_driver, logger, require
|
||||
from nonebot_plugin_localstore import get_plugin_data_file
|
||||
|
||||
require("nonebot_plugin_apscheduler")
|
||||
require("nonebot_plugin_marshoai")
|
||||
from nonebot_plugin_apscheduler import scheduler
|
||||
|
||||
from nonebot_plugin_marshoai.instances import client
|
||||
from nonebot_plugin_marshoai.plugin import PluginMetadata, on_function_call
|
||||
from nonebot_plugin_marshoai.plugin.func_call.params import String
|
||||
|
||||
from .command import *
|
||||
from .config import plugin_config
|
||||
|
||||
__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)
|
||||
driver = get_driver()
|
||||
|
||||
|
||||
@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:
|
||||
memory_data_ = "\n".join(memory_data[i])
|
||||
msg = f"这是一些大模型记忆信息,请你保留重要内容,尽量减少无用的记忆后重新输出记忆内容,浓缩为一行:\n{memory_data_}"
|
||||
res = await client.complete(UserMessage(content=msg))
|
||||
try:
|
||||
memory = res.choices[0].message.content # type: ignore
|
||||
memory_data[i] = memory
|
||||
except AttributeError:
|
||||
logger.error(f"整理关于{i}的记忆时出错:{res}")
|
||||
|
||||
with open(memory_path, "w", encoding="utf-8") as f:
|
||||
json.dump(memory_data, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
if plugin_config.marshoai_plugin_memory_scheduler:
|
||||
|
||||
@driver.on_startup
|
||||
async def _():
|
||||
logger.info("小棉定时记忆整理已启动!")
|
||||
scheduler.add_job(
|
||||
organize_memories,
|
||||
"cron",
|
||||
hour="0",
|
||||
minute="0",
|
||||
second="0",
|
||||
day="*",
|
||||
id="organize_memories",
|
||||
)
|
@ -1,47 +0,0 @@
|
||||
import json
|
||||
|
||||
from arclet.alconna import Alconna, Args, Subcommand
|
||||
from nonebot import logger
|
||||
from nonebot.adapters import Bot, Event
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.typing import T_State
|
||||
from nonebot_plugin_alconna import on_alconna
|
||||
from nonebot_plugin_localstore import get_plugin_data_file
|
||||
|
||||
from nonebot_plugin_marshoai.config import config
|
||||
|
||||
marsho_memory_cmd = on_alconna(
|
||||
Alconna(
|
||||
f"{config.marshoai_default_name}.memory",
|
||||
Subcommand("view", alias={"v"}),
|
||||
Subcommand("reset", alias={"r"}),
|
||||
),
|
||||
priority=10,
|
||||
block=True,
|
||||
)
|
||||
|
||||
memory_path = get_plugin_data_file("memory.json")
|
||||
|
||||
|
||||
@marsho_memory_cmd.assign("view")
|
||||
async def view_memory(matcher: Matcher, state: T_State, event: Event):
|
||||
user_id = str(event.get_user_id())
|
||||
with open(memory_path, "r", encoding="utf-8") as f:
|
||||
memory_data = json.load(f)
|
||||
memorys = memory_data.get(user_id, [])
|
||||
if not memorys:
|
||||
await matcher.finish("好像对ta还没有任何记忆呢~")
|
||||
await matcher.finish("这些是有关ta的记忆:" + "\n".join(memorys))
|
||||
|
||||
|
||||
@marsho_memory_cmd.assign("reset")
|
||||
async def reset_memory(matcher: Matcher, state: T_State, event: Event):
|
||||
user_id = str(event.get_user_id())
|
||||
with open(memory_path, "r", encoding="utf-8") as f:
|
||||
memory_data = json.load(f)
|
||||
if user_id in memory_data:
|
||||
del memory_data[user_id]
|
||||
with open(memory_path, "w", encoding="utf-8") as f:
|
||||
json.dump(memory_data, f, ensure_ascii=False, indent=4)
|
||||
await matcher.finish("记忆已重置~")
|
||||
await matcher.finish("没有找到该用户的记忆~")
|
@ -1,9 +0,0 @@
|
||||
from nonebot import get_plugin_config, logger
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class ConfigModel(BaseModel):
|
||||
marshoai_plugin_memory_scheduler: bool = True
|
||||
|
||||
|
||||
plugin_config: ConfigModel = get_plugin_config(ConfigModel)
|
@ -1,17 +0,0 @@
|
||||
[
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "marshoai_memory__write_memory",
|
||||
"description": "如果在上下中你看见并觉得应该记住的人的行为与事件,请调用这个函数,并将记忆内容写入。请尽量每次都调用,总结ta的习惯、爱好和性格,以及你对ta的印象和ta对你的印象;比如用户喜欢干什么吃什么。"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "marshoai_memory__read_memory",
|
||||
"description": "每当你想要获取更多有关某人的信息的时候,请调用这个函数。"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
Reference in New Issue
Block a user