From 68eb2fc946d30db373ac32a940902d04d40b2048 Mon Sep 17 00:00:00 2001 From: Asankilp Date: Fri, 10 Jan 2025 21:47:13 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=9B=B4=E6=96=B0Caller=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E7=B1=BB=E5=9E=8B=E5=92=8C=E6=A8=A1=E5=9D=97=E5=90=8D?= =?UTF-8?q?=E9=80=89=E9=A1=B9,support=20moonshot=20AI=20builtin=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_marshoai/marsho.py | 14 +++++--- .../plugin/func_call/caller.py | 33 ++++++++++++++++--- .../plugins/marshoai_basic/__init__.py | 1 + 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/nonebot_plugin_marshoai/marsho.py b/nonebot_plugin_marshoai/marsho.py index 0cdeafba..05a21a1c 100644 --- a/nonebot_plugin_marshoai/marsho.py +++ b/nonebot_plugin_marshoai/marsho.py @@ -323,10 +323,14 @@ async def marsho( # 需要获取额外信息,调用函数工具 tool_msg = [] while choice.message.tool_calls != None: - tool_msg.append( - AssistantMessage(tool_calls=response.choices[0].message.tool_calls) - ) - for tool_call in choice.message.tool_calls: + # await UniMessage(str(response)).send() + tool_calls = choice.message.tool_calls + if tool_calls[0]["function"]["name"].startswith("$"): + choice.message.tool_calls[0][ + "type" + ] = "builtin_function" # 兼容 moonshot AI 内置函数的临时方案 + tool_msg.append(choice.message.as_dict()) + for tool_call in tool_calls: if isinstance( tool_call, ChatCompletionsToolCall ): # 循环调用工具直到不需要调用 @@ -376,6 +380,8 @@ async def marsho( tool_msg.append( ToolMessage(tool_call_id=tool_call.id, content=func_return).as_dict() # type: ignore ) + # tool_msg[0]["tool_calls"][0]["type"] = "builtin_function" + # await UniMessage(str(tool_msg)).send() request_msg = context_msg + [UserMessage(content=usermsg).as_dict()] + tool_msg # type: ignore response = await make_chat( client=client, diff --git a/nonebot_plugin_marshoai/plugin/func_call/caller.py b/nonebot_plugin_marshoai/plugin/func_call/caller.py index 2f5fbda4..cf634e3a 100644 --- a/nonebot_plugin_marshoai/plugin/func_call/caller.py +++ b/nonebot_plugin_marshoai/plugin/func_call/caller.py @@ -17,11 +17,21 @@ _caller_data: dict[str, "Caller"] = {} class Caller: - def __init__(self, name: str = "", description: str | None = None): + def __init__( + self, + name: str = "", + description: str | None = None, + func_type: str = "function", + no_module_name: bool = False, + ): self._name: str = name """函数名称""" self._description = description """函数描述""" + self._func_type = func_type + """函数类型""" + self.no_module_name = no_module_name + """是否不包含模块名""" self._plugin: Plugin | None = None """所属插件对象,装饰时声明""" self.func: ASYNC_FUNCTION_CALL_FUNC | None = None @@ -162,7 +172,7 @@ class Caller: "description": "占位符,用于显示在对话框中", # 为保证兼容性而设置的无用参数 } return { - "type": "function", + "type": self._func_type, "function": { "name": self.aifc_name, "description": self._description, @@ -237,6 +247,8 @@ class Caller: @property def aifc_name(self) -> str: """AI调用名,没有点""" + if self.no_module_name: + return self._name return self.full_name.replace(".", "-") @property @@ -249,16 +261,29 @@ class Caller: return f"{self.full_name}({self._description})" -def on_function_call(name: str = "", description: str | None = None) -> Caller: +def on_function_call( + name: str = "", + description: str | None = None, + func_type: str = "function", + no_module_name: bool = False, +) -> Caller: """返回一个Caller类,可用于装饰一个函数,使其注册为一个可被AI调用的function call函数 Args: + name: 函数名称,若为空则从函数的__name__属性获取 description: 函数描述,若为None则从函数的docstring中获取 + func_type: 函数类型,默认为function,若要注册为 Moonshot AI 的内置函数则为builtin_function + no_module_name: 是否不包含模块名,当注册为 Moonshot AI 的内置函数时为True Returns: Caller: Caller对象 """ - caller = Caller(name=name, description=description) + caller = Caller( + name=name, + description=description, + func_type=func_type, + no_module_name=no_module_name, + ) return caller diff --git a/nonebot_plugin_marshoai/plugins/marshoai_basic/__init__.py b/nonebot_plugin_marshoai/plugins/marshoai_basic/__init__.py index 7a548109..ef51a019 100755 --- a/nonebot_plugin_marshoai/plugins/marshoai_basic/__init__.py +++ b/nonebot_plugin_marshoai/plugins/marshoai_basic/__init__.py @@ -4,6 +4,7 @@ from zhDateTime import DateTime # type: ignore from nonebot_plugin_marshoai.plugin import PluginMetadata, String, on_function_call +# from .web import * # 定义插件元数据 __marsho_meta__ = PluginMetadata( name="基本功能",