Enhance AIClient and OpenAIModel to support tool names in messages and improve tool capability detection. Updated message formatting to include tool names for better clarity in error handling and results.

This commit is contained in:
Mimikko-zeus
2026-03-03 01:37:00 +08:00
parent ae208af6a9
commit 0b356d1fef
2 changed files with 201 additions and 91 deletions

View File

@@ -254,7 +254,8 @@ class AIClient:
messages.append(Message(
role="tool",
content=f"工具参数解析失败: {str(e)}",
tool_call_id=fallback_id
tool_call_id=fallback_id,
name="tool"
))
continue
if not tool_name:
@@ -265,31 +266,31 @@ class AIClient:
if not tool_def:
error_msg = f"未找到工具: {tool_name}"
logger.warning(error_msg)
if tool_call_id:
messages.append(Message(
role="tool",
content=error_msg,
tool_call_id=tool_call_id
))
messages.append(Message(
role="tool",
name=tool_name,
content=error_msg,
tool_call_id=tool_call_id
))
continue
try:
result = tool_def.function(**tool_args)
if inspect.isawaitable(result):
result = await result
if tool_call_id:
messages.append(Message(
role="tool",
content=str(result),
tool_call_id=tool_call_id
))
messages.append(Message(
role="tool",
name=tool_name,
content=str(result),
tool_call_id=tool_call_id
))
except Exception as e:
if tool_call_id:
messages.append(Message(
role="tool",
content=f"工具执行失败: {str(e)}",
tool_call_id=tool_call_id
))
messages.append(Message(
role="tool",
name=tool_name,
content=f"工具执行失败: {str(e)}",
tool_call_id=tool_call_id
))
# 再次调用模型获取最终响应
return await self.model.chat(messages, tools, **kwargs)