2024-10-23 13:44:01 +08:00
|
|
|
|
import traceback
|
|
|
|
|
from azure.ai.inference.aio import ChatCompletionsClient
|
|
|
|
|
from azure.ai.inference.models import UserMessage, TextContentItem, ImageContentItem, ImageUrl, CompletionsFinishReason
|
|
|
|
|
from melobot import Plugin, send_text
|
2024-10-24 16:45:06 +08:00
|
|
|
|
from melobot.protocols.onebot.v11 import on_start_match, on_message, on_command, on_notice, on_event, Adapter
|
2024-10-24 14:19:33 +08:00
|
|
|
|
from melobot.protocols.onebot.v11.handle import Args
|
2024-10-24 16:45:06 +08:00
|
|
|
|
from melobot.protocols.onebot.v11.utils import MsgChecker, LevelRole, MsgCheckerFactory, StartMatcher, ParseArgs, Parser
|
|
|
|
|
from melobot.protocols.onebot.v11.adapter.event import MessageEvent, PokeNotifyEvent, GroupMessageEvent, PrivateMessageEvent
|
|
|
|
|
from melobot.protocols.onebot.v11.adapter.segment import PokeSegment
|
2024-10-23 13:44:01 +08:00
|
|
|
|
from azure.core.credentials import AzureKeyCredential
|
2024-10-24 16:45:06 +08:00
|
|
|
|
from typing import Union
|
2024-10-23 13:44:01 +08:00
|
|
|
|
from .constants import *
|
2024-10-23 17:07:34 +08:00
|
|
|
|
from .config import Config
|
2024-10-23 13:44:01 +08:00
|
|
|
|
from .util import *
|
|
|
|
|
from .models import MarshoContext
|
2024-10-24 16:45:06 +08:00
|
|
|
|
from .checkers import superuser_checker, PokeMarshoChecker
|
2024-10-23 17:07:34 +08:00
|
|
|
|
config = Config()
|
|
|
|
|
model_name = config.marshoai_default_model
|
2024-10-23 13:44:01 +08:00
|
|
|
|
context = MarshoContext()
|
2024-10-23 17:07:34 +08:00
|
|
|
|
token = config.marshoai_token
|
|
|
|
|
endpoint = config.marshoai_azure_endpoint
|
2024-10-23 13:44:01 +08:00
|
|
|
|
client = ChatCompletionsClient(
|
|
|
|
|
endpoint=endpoint,
|
|
|
|
|
credential=AzureKeyCredential(token)
|
|
|
|
|
)
|
2024-10-24 16:45:06 +08:00
|
|
|
|
|
|
|
|
|
|
2024-10-24 14:19:33 +08:00
|
|
|
|
|
|
|
|
|
@on_command(checker=superuser_checker, cmd_start="/", cmd_sep=" ", targets="changemodel")
|
|
|
|
|
async def changemodel(args: ParseArgs = Args()):
|
|
|
|
|
global model_name
|
|
|
|
|
model_name = args.vals[0]
|
|
|
|
|
await send_text("已切换")
|
2024-10-23 13:44:01 +08:00
|
|
|
|
|
2024-10-24 17:05:58 +08:00
|
|
|
|
@on_command(checker=superuser_checker, cmd_start="/", cmd_sep=" ", targets="contexts")
|
|
|
|
|
async def contexts(event: Union[GroupMessageEvent, PrivateMessageEvent]):
|
|
|
|
|
try:
|
|
|
|
|
await send_text(str(context.build(event.group_id, event.is_private)[1:]))
|
|
|
|
|
except AttributeError:
|
|
|
|
|
await send_text(str(context.build(event.user_id, event.is_private)[1:]))
|
2024-10-24 16:45:06 +08:00
|
|
|
|
|
|
|
|
|
@on_start_match("reset")
|
2024-10-24 17:05:58 +08:00
|
|
|
|
async def reset(event: Union[GroupMessageEvent, PrivateMessageEvent]):
|
|
|
|
|
try:
|
|
|
|
|
context.reset(event.group_id, event.is_private)
|
|
|
|
|
except AttributeError:
|
|
|
|
|
context.reset(event.user_id, event.is_private)
|
2024-10-24 17:52:05 +08:00
|
|
|
|
await send_text("上下文已重置")
|
2024-10-23 13:44:01 +08:00
|
|
|
|
|
2024-10-24 16:45:06 +08:00
|
|
|
|
|
|
|
|
|
@on_start_match("marsho")
|
2024-10-24 17:05:58 +08:00
|
|
|
|
async def marsho(event: Union[GroupMessageEvent, PrivateMessageEvent]):
|
|
|
|
|
await marsho_main(event, event.is_group)
|
2024-10-24 16:45:06 +08:00
|
|
|
|
|
|
|
|
|
async def marsho_main(event: Union[GroupMessageEvent, PrivateMessageEvent], is_group: bool):
|
2024-10-23 13:44:01 +08:00
|
|
|
|
if event.text.lstrip("marsho") == "":
|
2024-10-24 14:19:33 +08:00
|
|
|
|
await send_text(USAGE)
|
2024-10-23 13:44:01 +08:00
|
|
|
|
await send_text(INTRODUCTION)
|
|
|
|
|
return
|
|
|
|
|
# await UniMessage(str(text)).send()
|
|
|
|
|
try:
|
|
|
|
|
is_support_image_model = model_name.lower() in SUPPORT_IMAGE_MODELS
|
|
|
|
|
usermsg = [] if is_support_image_model else ""
|
|
|
|
|
user_id = event.sender.user_id
|
2024-10-24 17:05:58 +08:00
|
|
|
|
try:
|
|
|
|
|
target_id = event.group_id
|
|
|
|
|
except AttributeError:
|
|
|
|
|
target_id = event.user_id
|
2024-10-23 13:44:01 +08:00
|
|
|
|
nickname_prompt = ""
|
|
|
|
|
marsho_string_removed = False
|
|
|
|
|
for i in event.get_segments("image"):
|
|
|
|
|
if is_support_image_model:
|
|
|
|
|
imgurl = str(i.data["url"])
|
|
|
|
|
picmsg = ImageContentItem(
|
|
|
|
|
image_url=ImageUrl(url=str(await get_image_b64(imgurl)))
|
|
|
|
|
)
|
|
|
|
|
usermsg.append(picmsg)
|
|
|
|
|
else:
|
|
|
|
|
await send_text("*此模型不支持图片处理。")
|
|
|
|
|
for i in event.get_segments("text"):
|
|
|
|
|
if not marsho_string_removed:
|
|
|
|
|
# 去掉最前面的"marsho "字符串
|
|
|
|
|
clean_text = i.data["text"].lstrip("marsho ")
|
|
|
|
|
marsho_string_removed = True # 标记文本已处理
|
|
|
|
|
else:
|
|
|
|
|
clean_text = i.data["text"]
|
|
|
|
|
if is_support_image_model:
|
|
|
|
|
usermsg.append(TextContentItem(text=clean_text+nickname_prompt))
|
|
|
|
|
else:
|
|
|
|
|
usermsg += str(clean_text+nickname_prompt)
|
|
|
|
|
response = await make_chat(
|
|
|
|
|
client=client,
|
|
|
|
|
model_name=model_name,
|
2024-10-24 16:45:06 +08:00
|
|
|
|
msg=context.build(target_id, event.is_private)+[UserMessage(content=usermsg)])
|
2024-10-23 13:44:01 +08:00
|
|
|
|
choice = response.choices[0]
|
|
|
|
|
if choice["finish_reason"] == CompletionsFinishReason.STOPPED: # 当对话成功时,将dict的上下文添加到上下文类中
|
2024-10-24 16:45:06 +08:00
|
|
|
|
context.append(UserMessage(content=usermsg).as_dict(), target_id, event.is_private)
|
|
|
|
|
context.append(choice.message.as_dict(), target_id, event.is_private)
|
2024-10-23 13:44:01 +08:00
|
|
|
|
elif choice["finish_reason"] == CompletionsFinishReason.CONTENT_FILTERED:
|
|
|
|
|
await send_text("*已被内容过滤器过滤。请调整聊天内容后重试。")
|
|
|
|
|
return
|
|
|
|
|
await send_text(str(choice.message.content))
|
|
|
|
|
except Exception as e:
|
|
|
|
|
await send_text(str(e)+suggest_solution(str(e)))
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
return
|
|
|
|
|
|
2024-10-24 16:45:06 +08:00
|
|
|
|
@on_event(checker=PokeMarshoChecker())
|
|
|
|
|
async def poke(event: PokeNotifyEvent, adapter: Adapter): # 尚未实现私聊戳一戳 QwQ
|
|
|
|
|
#await adapter.send_custom(str(event.user_id),group_id=event.group_id)
|
|
|
|
|
user_id = event.user_id
|
|
|
|
|
# nicknames = await get_nicknames()
|
|
|
|
|
# nickname = nicknames.get(user_id, "")
|
|
|
|
|
nickname = ""
|
|
|
|
|
try:
|
|
|
|
|
if config.marshoai_poke_suffix != "":
|
|
|
|
|
response = await make_chat(
|
|
|
|
|
client=client,
|
|
|
|
|
model_name=model_name,
|
|
|
|
|
msg=[get_prompt(),UserMessage(content=f"*{nickname}{config.marshoai_poke_suffix}")]
|
|
|
|
|
)
|
|
|
|
|
choice = response.choices[0]
|
|
|
|
|
if choice["finish_reason"] == CompletionsFinishReason.STOPPED:
|
|
|
|
|
await adapter.send_custom(" "+str(choice.message.content),group_id=event.group_id)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
await adapter.send_custom(str(e)+suggest_solution(str(e)),group_id=event.group_id)
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
return
|
|
|
|
|
|
2024-10-23 13:44:01 +08:00
|
|
|
|
class MarshoAI(Plugin):
|
2024-10-24 14:19:33 +08:00
|
|
|
|
version = VERSION
|
2024-10-24 17:05:58 +08:00
|
|
|
|
flows = [changemodel,marsho,reset,poke,contexts]
|