perf: 优化 llm tool 返回值处理

This commit is contained in:
Soulter
2024-09-08 08:41:26 -04:00
parent fb52989d62
commit d8e70c4d7f
2 changed files with 26 additions and 2 deletions
+8
View File
@@ -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}")
"""
指令处理函数。
+18 -2
View File
@@ -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))