From 4b2676b9fc69320e5289c339f0013131447b10df Mon Sep 17 00:00:00 2001 From: Asankilp Date: Sun, 29 Dec 2024 06:33:52 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=9B=B4=E6=96=B0marsho=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E4=BB=A5=E5=A4=84=E7=90=86tool=5Fcalls=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=87=BD=E6=95=B0=E8=B0=83=E7=94=A8=E5=8F=82?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=8D=A0=E4=BD=8D=E7=AC=A6?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E4=BB=A5=E5=85=BC=E5=AE=B9=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=EF=BC=88=E5=A6=82GLM=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_marshoai/azure.py | 19 ++++++++++++++++--- .../plugin/func_call/caller.py | 8 +++++++- nonebot_plugin_marshoai/util.py | 1 + 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/nonebot_plugin_marshoai/azure.py b/nonebot_plugin_marshoai/azure.py index 96c2dc83..c55a6b42 100644 --- a/nonebot_plugin_marshoai/azure.py +++ b/nonebot_plugin_marshoai/azure.py @@ -337,6 +337,10 @@ async def marsho( ) # await UniMessage(str(response)).send() choice = response.choices[0] + # Sprint(choice) + # 当tool_calls非空时,将finish_reason设置为TOOL_CALLS + if choice.message.tool_calls != None: + choice["finish_reason"] = CompletionsFinishReason.TOOL_CALLS if choice["finish_reason"] == CompletionsFinishReason.STOPPED: # 当对话成功时,将dict的上下文添加到上下文类中 context.append( @@ -379,6 +383,9 @@ async def marsho( function_args = json.loads( tool_call.function.arguments.replace("'", '"') ) + # 删除args的placeholder参数 + if "placeholder" in function_args: + del function_args["placeholder"] logger.info( f"调用函数 {tool_call.function.name.replace('-', '.')}\n参数:" + "\n".join([f"{k}={v}" for k, v in function_args.items()]) @@ -414,15 +421,21 @@ async def marsho( ) func_return = f"未找到函数 {tool_call.function.name.replace('-', '.')}" tool_msg.append( - ToolMessage(tool_call_id=tool_call.id, content=func_return) # type: ignore + ToolMessage(tool_call_id=tool_call.id, content=func_return).as_dict() # type: ignore ) + request_msg = context_msg + [UserMessage(content=usermsg).as_dict()] + tool_msg # type: ignore response = await make_chat( client=client, model_name=model_name, - msg=context_msg + [UserMessage(content=usermsg)] + tool_msg, # type: ignore - tools=tools.get_tools_list(), + msg=request_msg, # type: ignore + tools=( + tools_lists if tools_lists else None + ), # TODO 临时追加函数,后期优化 ) choice = response.choices[0] + # 当tool_calls非空时,将finish_reason设置为TOOL_CALLS + if choice.message.tool_calls != None: + choice["finish_reason"] = CompletionsFinishReason.TOOL_CALLS if choice["finish_reason"] == CompletionsFinishReason.STOPPED: # 对话成功 添加上下文 diff --git a/nonebot_plugin_marshoai/plugin/func_call/caller.py b/nonebot_plugin_marshoai/plugin/func_call/caller.py index f9de05a5..1a05971e 100644 --- a/nonebot_plugin_marshoai/plugin/func_call/caller.py +++ b/nonebot_plugin_marshoai/plugin/func_call/caller.py @@ -163,7 +163,13 @@ def data(self) -> dict[str, Any]: "parameters": { "type": "object", "properties": { - key: value.data() for key, value in self._parameters.items() + **{ + key: value.data() for key, value in self._parameters.items() + }, + "placeholder": { + "type": "string", + "description": "占位符,用于显示在对话框中", # 为保证兼容性而设置的无用参数 + }, }, }, "required": [ diff --git a/nonebot_plugin_marshoai/util.py b/nonebot_plugin_marshoai/util.py index 9cda5e3e..929f694e 100755 --- a/nonebot_plugin_marshoai/util.py +++ b/nonebot_plugin_marshoai/util.py @@ -95,6 +95,7 @@ async def make_chat( messages=msg, model=model_name, tools=tools, + tool_choice="auto", temperature=config.marshoai_temperature, max_tokens=config.marshoai_max_tokens, top_p=config.marshoai_top_p,