From d8e70c4d7fe06e57ebe591162bb0918b5aede7b4 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sun, 8 Sep 2024 08:41:26 -0400 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20llm=20tool=20?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/plugins/helloworld/main.py | 8 ++++++++ astrbot/message/handler.py | 20 ++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/addons/plugins/helloworld/main.py b/addons/plugins/helloworld/main.py index 56eb99162..25f8e6974 100644 --- a/addons/plugins/helloworld/main.py +++ b/addons/plugins/helloworld/main.py @@ -21,6 +21,14 @@ class HelloWorldPlugin: def __init__(self, context: Context) -> None: self.context = context self.context.register_commands("helloworld", "helloworld", "内置测试指令。", 1, self.helloworld) + self.context.register_llm_tool("welcome_somebody", [{ + "type": "string", + "name": "name", + "description": "要欢迎的人的名字" + }], "给一个用户发送欢迎文本。", self.welcome_somebody) + + async def welcome_somebody(self, name: str): + return CommandResult().message(f"欢迎{name}!") """ 指令处理函数。 diff --git a/astrbot/message/handler.py b/astrbot/message/handler.py index a20726435..c09cd6321 100644 --- a/astrbot/message/handler.py +++ b/astrbot/message/handler.py @@ -216,8 +216,24 @@ class MessageHandler(): args = json.loads(llm_result.arguments) args['ame'] = message args['context'] = self.context - llm_result = await func_obj(**args) - has_func = True + try: + cmd_res = await func_obj(**args) + except TypeError as e: + args.pop('ame') + args.pop('context') + cmd_res = await func_obj(**args) + if isinstance(cmd_res, CommandResult): + return MessageResult( + cmd_res.message_chain, + is_command_call=True, + use_t2i=cmd_res.is_use_t2i + ) + elif isinstance(cmd_res, str): + return MessageResult(cmd_res) + elif not cmd_res: + return + else: + return MessageResult(f"AstrBot Function-calling 异常:调用:{llm_result} 时,返回了未知的返回值类型。") except BaseException as e: traceback.print_exc() return MessageResult("AstrBot Function-calling 异常:" + str(e))