v0.2,支持保存/读取上下文

This commit is contained in:
2024-10-03 15:16:32 +08:00
parent aa605afc9e
commit e4facb65f7
6 changed files with 57 additions and 24 deletions

View File

@ -20,6 +20,8 @@ praises_cmd = on_command("praises",permission=SUPERUSER)
add_usermsg_cmd = on_command("usermsg",permission=SUPERUSER)
add_assistantmsg_cmd = on_command("assistantmsg",permission=SUPERUSER)
contexts_cmd = on_command("contexts",permission=SUPERUSER)
save_context_cmd = on_command("savecontext",permission=SUPERUSER)
load_context_cmd = on_command("loadcontext",permission=SUPERUSER)
marsho_cmd = on_alconna(
Alconna(
"marsho",
@ -32,14 +34,14 @@ context = MarshoContext()
@add_usermsg_cmd.handle()
async def add_usermsg(target: MsgTarget, arg: Message = CommandArg()):
if msg := arg.extract_plain_text():
context.append(UserMessage(content=msg), target.id, target.private)
await UniMessage("已添加用户消息").send()
context.append(UserMessage(content=msg).as_dict(), target.id, target.private)
await add_usermsg_cmd.finish("已添加用户消息")
@add_assistantmsg_cmd.handle()
async def add_assistantmsg(target: MsgTarget, arg: Message = CommandArg()):
if msg := arg.extract_plain_text():
context.append(AssistantMessage(content=msg), target.id, target.private)
await UniMessage("已添加助手消息").send()
context.append(AssistantMessage(content=msg).as_dict(), target.id, target.private)
await add_assistantmsg_cmd.finish("已添加助手消息")
@praises_cmd.handle()
async def praises():
@ -49,17 +51,18 @@ async def praises():
async def contexts(target: MsgTarget):
await UniMessage(str(context.build(target.id, target.private)[1:])).send()
# @setprompt_cmd.handle() #用不了了
# async def setprompt(arg: Message = CommandArg()):
# global spell, context
# if prompt := arg.extract_plain_text():
# spell = SystemMessage(content=prompt)
# await setprompt_cmd.finish("已设置提示词")
# else:
# spell = SystemMessage(content="")
# context = []
# await setprompt_cmd.finish("已清除提示词")
@save_context_cmd.handle()
async def save_context(target: MsgTarget, arg: Message = CommandArg()):
contexts = context.build(target.id, target.private)[1:]
if msg := arg.extract_plain_text():
await save_context_to_json(msg, contexts)
await save_context_cmd.finish("已保存上下文")
@load_context_cmd.handle()
async def load_context(target: MsgTarget, arg: Message = CommandArg()):
if msg := arg.extract_plain_text():
context.set_context(await load_context_from_json(msg), target.id, target.private)
await load_context_cmd.finish("已加载并覆盖上下文")
@resetmem_cmd.handle()
async def resetmem(target: MsgTarget):
@ -72,6 +75,7 @@ async def changemodel(arg : Message = CommandArg()):
if model := arg.extract_plain_text():
model_name = model
await changemodel_cmd.finish("已切换")
@marsho_cmd.handle()
async def marsho(
target: MsgTarget,
@ -121,9 +125,9 @@ async def marsho(
)
#await UniMessage(str(response)).send()
choice = response.choices[0]
if choice["finish_reason"] == CompletionsFinishReason.STOPPED:
context.append(UserMessage(content=usermsg), target.id, target.private)
context.append(choice.message, target.id, target.private)
if choice["finish_reason"] == CompletionsFinishReason.STOPPED: # 当对话成功时将dict的上下文添加到上下文类中
context.append(UserMessage(content=usermsg).as_dict(), target.id, target.private)
context.append(choice.message.as_dict(), target.id, target.private)
elif choice["finish_reason"] == CompletionsFinishReason.CONTENT_FILTERED:
await UniMessage("*已被内容过滤器过滤。*").send()
#await UniMessage(str(choice)).send()