添加了at时响应

This commit is contained in:
Rikka-desu 2024-11-24 15:00:44 +08:00
parent 8221fa7928
commit ce1d2f21ae
3 changed files with 24 additions and 9 deletions

View File

@ -15,10 +15,11 @@ from azure.ai.inference.models import (
ChatCompletionsToolCall, ChatCompletionsToolCall,
) )
from azure.core.credentials import AzureKeyCredential from azure.core.credentials import AzureKeyCredential
from nonebot import on_command, logger from nonebot import on_command, on_message, logger
from nonebot.adapters import Message, Event from nonebot.adapters import Message, Event
from nonebot.params import CommandArg from nonebot.params import CommandArg
from nonebot.permission import SUPERUSER from nonebot.permission import SUPERUSER
from nonebot.rule import Rule, to_me
from nonebot_plugin_alconna import on_alconna, MsgTarget from nonebot_plugin_alconna import on_alconna, MsgTarget
from nonebot_plugin_alconna.uniseg import UniMessage, UniMsg from nonebot_plugin_alconna.uniseg import UniMessage, UniMsg
import nonebot_plugin_localstore as store import nonebot_plugin_localstore as store
@ -29,6 +30,11 @@ from .metadata import metadata
from .models import MarshoContext, MarshoTools from .models import MarshoContext, MarshoTools
from .util import * from .util import *
async def at_enable():
return config.marshoai_at
driver = get_driver() driver = get_driver()
changemodel_cmd = on_command("changemodel", permission=SUPERUSER) changemodel_cmd = on_command("changemodel", permission=SUPERUSER)
@ -47,6 +53,7 @@ marsho_cmd = on_alconna(
), ),
aliases=config.marshoai_aliases, aliases=config.marshoai_aliases,
) )
marsho_at = on_message(rule=to_me()&at_enable)
nickname_cmd = on_alconna( nickname_cmd = on_alconna(
Alconna( Alconna(
"nickname", "nickname",
@ -63,6 +70,7 @@ endpoint = config.marshoai_azure_endpoint
client = ChatCompletionsClient(endpoint=endpoint, credential=AzureKeyCredential(token)) client = ChatCompletionsClient(endpoint=endpoint, credential=AzureKeyCredential(token))
target_list = [] # 记录需保存历史上下文的列表 target_list = [] # 记录需保存历史上下文的列表
@driver.on_startup @driver.on_startup
async def _preload_tools(): async def _preload_tools():
tools_dir = store.get_plugin_data_dir() / "tools" tools_dir = store.get_plugin_data_dir() / "tools"
@ -71,6 +79,7 @@ async def _preload_tools():
tools.load_tools(Path(__file__).parent / "tools") tools.load_tools(Path(__file__).parent / "tools")
tools.load_tools(store.get_plugin_data_dir() / "tools") tools.load_tools(store.get_plugin_data_dir() / "tools")
@add_usermsg_cmd.handle() @add_usermsg_cmd.handle()
async def add_usermsg(target: MsgTarget, arg: Message = CommandArg()): async def add_usermsg(target: MsgTarget, arg: Message = CommandArg()):
if msg := arg.extract_plain_text(): if msg := arg.extract_plain_text():
@ -89,7 +98,7 @@ async def add_assistantmsg(target: MsgTarget, arg: Message = CommandArg()):
@praises_cmd.handle() @praises_cmd.handle()
async def praises(): async def praises():
#await UniMessage(await tools.call("marshoai-weather.get_weather", {"location":"杭州"})).send() # await UniMessage(await tools.call("marshoai-weather.get_weather", {"location":"杭州"})).send()
await praises_cmd.finish(build_praises()) await praises_cmd.finish(build_praises())
@ -114,7 +123,7 @@ async def save_context(target: MsgTarget, arg: Message = CommandArg()):
@load_context_cmd.handle() @load_context_cmd.handle()
async def load_context(target: MsgTarget, arg: Message = CommandArg()): async def load_context(target: MsgTarget, arg: Message = CommandArg()):
if msg := arg.extract_plain_text(): if msg := arg.extract_plain_text():
await get_backup_context(target.id, target.private) # 为了将当前会话添加到"已恢复过备份"的列表而添加防止上下文被覆盖好奇怪QwQ await get_backup_context(target.id, target.private) # 为了将当前会话添加到"已恢复过备份"的列表而添加防止上下文被覆盖好奇怪QwQ
context.set_context( context.set_context(
await load_context_from_json(msg, "contexts"), target.id, target.private await load_context_from_json(msg, "contexts"), target.id, target.private
) )
@ -160,9 +169,12 @@ async def refresh_data():
await refresh_data_cmd.finish("已刷新数据") await refresh_data_cmd.finish("已刷新数据")
@marsho_at.handle()
@marsho_cmd.handle() @marsho_cmd.handle()
async def marsho(target: MsgTarget, event: Event, text: Optional[UniMsg] = None): async def marsho(target: MsgTarget, event: Event, text: Optional[UniMsg] = None):
global target_list global target_list
if event.get_message() and not text:
text = event.get_message()
if not text: if not text:
# 发送说明 # 发送说明
await UniMessage(metadata.usage + "\n当前使用的模型:" + model_name).send() await UniMessage(metadata.usage + "\n当前使用的模型:" + model_name).send()
@ -175,9 +187,9 @@ async def marsho(target: MsgTarget, event: Event, text: Optional[UniMsg] = None)
nickname_prompt = f"\n*此消息的说话者:{user_nickname}*" nickname_prompt = f"\n*此消息的说话者:{user_nickname}*"
else: else:
nickname_prompt = "" nickname_prompt = ""
#用户名无法获取,暂时注释 # 用户名无法获取,暂时注释
#user_nickname = event.sender.nickname # 未设置昵称时获取用户名 # user_nickname = event.sender.nickname # 未设置昵称时获取用户名
#nickname_prompt = f"\n*此消息的说话者:{user_nickname}" # nickname_prompt = f"\n*此消息的说话者:{user_nickname}"
if config.marshoai_enable_nickname_tip: if config.marshoai_enable_nickname_tip:
await UniMessage( await UniMessage(
"*你未设置自己的昵称。推荐使用'nickname [昵称]'命令设置昵称来获得个性化(可能)回答。" "*你未设置自己的昵称。推荐使用'nickname [昵称]'命令设置昵称来获得个性化(可能)回答。"
@ -242,14 +254,14 @@ async def marsho(target: MsgTarget, event: Event, text: Optional[UniMsg] = None)
response = await make_chat( response = await make_chat(
client=client, client=client,
model_name=model_name, model_name=model_name,
msg = context_msg + [UserMessage(content=usermsg)] + tool_msg, msg=context_msg + [UserMessage(content=usermsg)] + tool_msg,
tools=tools.get_tools_list() tools=tools.get_tools_list()
) )
choice = response.choices[0] choice = response.choices[0]
context.append( context.append(
UserMessage(content=usermsg).as_dict(), target.id, target.private UserMessage(content=usermsg).as_dict(), target.id, target.private
) )
#context.append(tool_msg, target.id, target.private) # context.append(tool_msg, target.id, target.private)
context.append(choice.message.as_dict(), target.id, target.private) context.append(choice.message.as_dict(), target.id, target.private)
await UniMessage(str(choice.message.content)).send(reply_to=True) await UniMessage(str(choice.message.content)).send(reply_to=True)
except Exception as e: except Exception as e:

View File

@ -12,6 +12,7 @@ class ConfigModel(BaseModel):
marshoai_token: str = "" marshoai_token: str = ""
# marshoai_support_image_models: list = ["gpt-4o","gpt-4o-mini"] # marshoai_support_image_models: list = ["gpt-4o","gpt-4o-mini"]
marshoai_default_name: str = "marsho" marshoai_default_name: str = "marsho"
marshoai_at: bool = False
marshoai_aliases: set[str] = { marshoai_aliases: set[str] = {
"小棉", "小棉",
} }

View File

@ -6,6 +6,8 @@ marshoai_default_name: "marsho" # 默认名称设定为marsho。
marshoai_aliases: marshoai_aliases:
- 小棉 - 小棉
marshoai_at: false # 决定是否开启at响应
marshoai_default_model: "gpt-4o-mini" # 默认模型设定为gpt-4o-mini。 marshoai_default_model: "gpt-4o-mini" # 默认模型设定为gpt-4o-mini。
# 主提示词定义了Marsho的性格和行为包含多语言名字翻译规则和对特定问题的回答约束。 # 主提示词定义了Marsho的性格和行为包含多语言名字翻译规则和对特定问题的回答约束。