perf: 优化 llm tool 返回值处理
This commit is contained in:
@@ -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}!")
|
||||
|
||||
"""
|
||||
指令处理函数。
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user