-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.py
More file actions
34 lines (24 loc) · 927 Bytes
/
Copy pathexecutor.py
File metadata and controls
34 lines (24 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Executor:将 LLM 返回的 tool_calls 分发给对应的 handler 执行。
错误不向上抛,而是返回错误字符串,让 LLM 自行决策下一步。
"""
import json
from tools.registry import HANDLERS
def execute(tool_call) -> str:
"""执行单个 tool_call,返回结果字符串。
Args:
tool_call: OpenAI ChatCompletionMessageToolCall 对象
Returns:
handler 的返回值,或错误描述字符串
"""
name = tool_call.function.name
if name not in HANDLERS:
return f"[错误] 未知工具: {name!r}。可用工具: {list(HANDLERS.keys())}"
try:
arguments = json.loads(tool_call.function.arguments)
except json.JSONDecodeError as e:
return f"[错误] 参数解析失败: {e}"
try:
return HANDLERS[name](arguments)
except Exception as e:
return f"[错误] 工具执行异常 ({type(e).__name__}): {e}"