mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
536 字
1 分钟
Toolformer 论文解读:让语言模型学会使用工具
2025-02-01

语言模型在计算和事实查询方面有时不如小模型。Toolformer 展示了语言模型可以通过简单的 API 自学使用外部工具,实现两者优势的最佳结合。

本文将详细解读 Toolformer 的自监督训练方法和工具调用能力。

本文要点#

  • 自监督的 API 调用学习
  • 工具调用的采样和过滤
  • 多种工具的集成
  • 零样本泛化能力

一、背景:语言模型使用工具的需求#

1.1 LLM 的短板#

flowchart TB A["语言模型使用工具的动机"] --> B["LLM 擅长"] A --> C["LLM 不擅长(相对小模型)"] A --> D["解决方案"] B --> B1["复杂推理和生成"] B --> B2["常识和世界知识"] B --> B3["代码编写"] C --> C1["精确算术计算"] C --> C2["实时信息查询"] C --> C3["格式规范输出"] D --> D1["结合 LLM 的推理能力 + 外部工具的精确性"] style A fill:#f44336,color:#fff

1.2 Toolformer 的思路#

# 传统 RAG
rag_output = generator.generate(question, retrieved_docs)
# Toolformer
toolformer_output = model.decide_and_execute_tools(question)
# 模型自己决定:是否调用工具、调用什么工具、传入什么参数

二、核心方法:自监督工具学习#

2.1 整体框架#

flowchart A["大规模无标注数据"] --> B["采样 API 调用"] B --> C["执行 API"] C --> D["过滤有效调用"] D --> E["微调语言模型"] E --> F["Toolformer"] style B fill:#ff9800,color:#fff style D fill:#4caf50,color:#fff

2.2 训练流程#

class Toolformer:
"""自监督工具学习框架"""
def __init__(self, model, tools):
self.model = model
self.tools = tools
def train(self, corpus):
"""
三个阶段的自监督训练
"""
# 阶段 1: 采样 API 调用
api_calls = self.sample_api_calls(corpus)
# 阶段 2: 执行 API 调用
executed = self.execute_api_calls(api_calls)
# 阶段 3: 过滤有效调用
filtered = self.filter_successful(executed)
# 微调
self.fine_tune(filtered)

三、技术细节#

3.1 API 调用采样#

class APISampler:
"""采样 API 调用"""
def __init__(self, model, tools):
self.model = model
self.tools = tools
def sample_api_calls(self, text):
"""
让模型决定在什么位置调用什么 API
"""
api_calls = []
# 在每个 token 位置决定是否调用 API
for position in range(len(text)):
# 构造上下文
context = text[:position]
# 让模型决定
action = self.model.decide(
context,
available_tools=list(self.tools.keys())
)
if action["type"] == "api_call":
api_calls.append({
"position": position,
"tool": action["tool"],
"arguments": action["args"]
})
return api_calls

3.2 执行 API 调用#

class APIExecutor:
"""执行 API 调用"""
def __init__(self, tools):
self.tools = tools
def execute(self, api_call):
"""
执行单个 API 调用
"""
tool_name = api_call["tool"]
args = api_call["arguments"]
if tool_name == "calculator":
result = eval(args["expression"])
elif tool_name == "search":
result = search_engine.query(args["query"])
elif tool_name == "translator":
result = translator.translate(
text=args["text"],
target_lang=args["target_lang"]
)
elif tool_name == "calendar":
result = calendar.get_current_date()
return {
"status": "success",
"result": result,
"api_response": str(result)
}

3.3 有效调用过滤#

class APIFilter:
"""过滤有效 API 调用"""
def __init__(self):
self.pass_count = 0
self.total_count = 0
def is_helpful(self, original_text, api_call, response):
"""
判断 API 调用是否有价值
"""
# 1. API 调用必须成功执行
if response["status"] != "success":
return False
# 2. 响应不能太长
if len(response["result"]) > 500:
return False
# 3. 响应不能是错误信息
if self.is_error(response["result"]):
return False
# 4. 调用应该是有用的(增加信息或修正错误)
helpful = self.check_helpfulness(
original_text,
api_call,
response["result"]
)
return helpful

四、工具集#

4.1 支持的工具#

TOOLS = {
# 计算器:精确算术运算
"calculator": {
"description": "Evaluate mathematical expressions",
"parameters": {
"expression": "string" # 如 "2 + 2"
}
},
# 搜索引擎:实时信息查询
"search": {
"description": "Search the web for information",
"parameters": {
"query": "string"
}
},
# 翻译系统:语言翻译
"translator": {
"description": "Translate text between languages",
"parameters": {
"text": "string",
"source_lang": "string",
"target_lang": "string"
}
},
# 日历系统:获取日期
"calendar": {
"description": "Get current date and time",
"parameters": {}
},
# 问答系统:百科知识
"question_answer": {
"description": "Answer factual questions",
"parameters": {
"question": "string"
}
}
}

4.2 调用示例#

# Toolformer 调用示例
input_text = "What is 1234 * 5678?"
# Toolformer 决策:
# 1. 需要计算 -> 调用 calculator
# 2. 构造调用
api_call = {
"tool": "calculator",
"arguments": {"expression": "1234 * 5678"}
}
# 执行
result = execute(api_call) # "7006652"
# 生成最终回答
output = "1234 × 5678 = 7,006,652"

五、实验结果#

5.1 零样本性能提升#

bar-chart title "Toolformer 在各任务上的零样本性能" x-label "任务" y-label "提升 %" data: 算术: 95, 问答: 40, 翻译: 25

5.2 工具使用分析#

flowchart TB A["工具使用分析"] --> B["调用频率"] A --> C["准确性"] B --> B1["计算器: 高频(用于精确计算)"] B --> B2["搜索: 中高频(用于实时信息)"] B --> B3["翻译: 低频(用于多语言任务)"] C --> C1["工具选择准确率: 92%"] C --> C2["参数构造准确率: 89%"] C --> C3["响应整合准确率: 87%"] style A fill:#ff9800,color:#fff

5.3 与大型模型对比#

# Toolformer 65B vs GPT-3 175B
comparison = {
"Toolformer 65B": {
"arithmetic": "95%", # 几乎完美
"factual_qa": "72%", # 略低
"translation": "85%" # 相当
},
"GPT-3 175B": {
"arithmetic": "60%", # 经常算错
"factual_qa": "75%", # 略高
"translation": "83%" # 相当
}
}
# 结论:Toolformer 用更少参数在算术上超过 GPT-3

六、与 Tool Use Agent 的区别#

flowchart LR subgraph Toolformer["Toolformer"] A1["自监督学习"] A2["模型自己学会何时调用工具"] A3["无需人工标注工具使用示例"] end subgraph 传统ToolUse["传统 Tool Use"] B1["人工定义工具调用格式"] B2["依赖 prompt engineering"] B3["模型被动响应工具调用指令"] end C["关键区别: Toolformer 是端到端学习的,Agent 通常是显式控制的"] Toolformer --> C 传统ToolUse --> C style Toolformer fill:#4caf50,color:#fff style 传统ToolUse fill:#f44336,color:#fff

常见问题 FAQ#

Q1:Toolformer 是如何自监督学习的?

A:不需要人工标注。模型通过在大规模文本中采样可能需要工具的位置,然后执行 API 调用,最后过滤出有帮助的调用,用这些数据微调模型。

Q2:Toolformer 和 RAG 有什么区别?

A:RAG 是被动的检索增强。Toolformer 是主动的,模型自己决定是否调用工具、调用什么工具。Toolformer 的决策更灵活。

Q3:Toolformer 学到的工具使用能力可以泛化吗?

A:可以。论文证明 Toolformer 在训练时没见过的任务上也能零样本使用工具,展现了良好的泛化能力。

Q4:Toolformer 对 Agent 研究有什么影响?

A:Toolformer 证明了自监督学习工具使用是可行的,为后续 Tool Use Agent 的发展提供了重要的方法论。


小结#

Toolformer 通过自监督学习让语言模型掌握工具使用能力:

flowchart TB A["Toolformer 核心总结"] --> B["核心方法"] A --> C["支持工具"] A --> D["效果"] A --> E["影响"] B --> B1["自监督 API 调用采样"] B --> B2["执行和过滤有效调用"] B --> B3["微调语言模型"] C --> C1["计算器、搜索引擎、翻译、日历、问答"] D --> D1["零样本泛化到新任务"] D --> D2["显著提升算术和事实准确性"] D --> D3["参数量更小但表现更好(vs GPT-3)"] E --> E1["启发了更多自监督工具学习研究"] E --> E2["为 Tool Use Agent 奠定基础"] style A fill:#4caf50,color:#fff

参考资料#

支持与分享

如果这篇文章对你有帮助,欢迎支持作者或分享给更多人

Toolformer 论文解读:让语言模型学会使用工具
https://blog.souloss.com/posts/machine-learning/llm-paper-history/toolformer-tool-use/
作者
Souloss
发布于
2025-02-01
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时