Tool Selection
Tool Selection 是 LLM 根据用户意图和工具描述选择合适工具的过程。工具数量、描述质量和上下文都会影响选择准确率。
#type / concept
#status / evergreen
#tech / ai
[!info] related notes
- 所属 MOC: Tool Calling Engineering MOC
- 相关: Tool Description, Function Schema
Tool Selection
一句话定义
Tool Selection 是 LLM 根据用户意图和工具描述选择合适工具的过程。它不是”模型随便选”,而是受工具数量、描述质量和上下文影响的决策过程。
核心原理
影响选择的因素
| 因素 | 影响 | 优化方式 |
|---|---|---|
| 工具数量 | 越多准确率越低 | 控制在 <20 个 |
| 描述质量 | 越清晰准确率越高 | 写好 description |
| 职责边界 | 越清晰越不容易选错 | 避免工具功能重叠 |
| 上下文 | 越相关选择越准 | 提供足够上下文 |
动态工具加载
class DynamicToolLoader:
def __init__(self, all_tools: dict):
self.all_tools = all_tools
def get_tools_for_context(self, context: dict) -> list:
"""根据上下文动态加载工具"""
tools = []
# 基础工具(始终加载)
tools.extend(self.get_base_tools())
# 根据用户角色加载
if context.get("user_role") == "admin":
tools.extend(self.get_admin_tools())
# 根据任务类型加载
if context.get("task_type") == "data_analysis":
tools.extend(self.get_data_tools())
return tools
工具分组
tool_groups = {
"search": ["search_knowledge", "search_web", "search_database"],
"data": ["query_database", "create_chart", "export_data"],
"communication": ["send_email", "create_ticket", "notify_user"],
"admin": ["delete_record", "update_config", "manage_user"],
}
常见坑
- 工具太多: 50 个工具全部暴露给 LLM,准确率下降
- 职责重叠: 两个工具功能类似,LLM 不知道选哪个
- 不做动态加载: 所有场景都加载所有工具
- 不测试选择准确率: 不知道 LLM 选对了没有