LLM 工具调用(Tool Calling)技术详解:原理、实践与关键问题解析

267 阅读6分钟

LLM 工具调用(Tool Calling)技术详解:原理、实践与关键问题解析

连接大模型与现实世界的桥梁【AI大模型教程】

在日常与基于大语言模型(LLM)的Chatbot产品交互中,我们逐渐发现这些聊天机器人能力越来越强,比如 Deepseek 可以通过联网检索来查询12306车票信息,但是目前还无法直接购买车票,部分能力当前似乎还难以实现。这正是工具调用(Tool Calling) 技术要持续解决的核心问题。通过工具调用,大模型就能像人类使用手机APP一样操作外部系统,极大扩展了其能力边界。

但一个常见的问题是:Tool Calling 到底是大模型本身的能力,还是推理引擎接口的能力?首先回答下这个问题:

— 1 Tool Calling 的本质—

双重依赖关系

要理解 Tool Calling 的能力归属,我们需要明确一个关键观点:这是模型自身能力与推理引擎接口支持相结合的产物,两者缺一不可。

1.1 大模型的基础能力

大模型本身需要经过训练,能够理解用户的请求何时需要调用外部工具,并且能够根据提供的工具描述,严格按照规定的格式(如JSON Schema)输出应该调用哪个工具以及具体的参数是什么。这确实是模型的一种内在能力,不同的模型在此项能力上表现有差异。例如早期DeepSeek-R1便不太能很好的支持tool calling,主要还是源于模型的训练。

而DeepSeek V3.1应该便具备了很好的tool calling能力,大家在选用模型时候需要稍加留意。

模型的作用

  • 识别用户意图中需要外部操作的部分
  • 生成结构化工具调用请求(函数名+参数)
  • 根据工具执行结果生成最终的自然语言回复

1.2 推理引擎的接口支持

光有"大脑"想还不够,需要外部的推理引擎来提供一套标准化的机制。这套机制包括:

  • 接收工具定义:允许开发者以结构化方式向模型声明可用工具
  • 引导模型输出:通过特定的提示模板将用户查询和工具定义信息组织成模型的输入
  • 解析模型响应:提供后处理模块能够可靠地从模型输出中识别并提取工具调用指令

—  2 Tool Calling 的工作原理与技术架构 —

从意图识别到结果整合

二、Tool Calling 的工作原理与技术架构

2.1 完整工作流程

工具调用工作的核心流程可以概括为以下步骤:

  1. 意图识别:模型分析用户查询,判断是否需要调用外部工具
  2. 工具选择:从可用工具中选择最合适的工具
  3. 参数提取:从用户查询中提取工具所需的参数
  4. 工具执行:外部系统实际执行工具调用
  5. 结果整合:模型将工具执行结果整合到最终回复中

—  3 实例演示—

OpenAI框架下的 Tool Calling 实现

3.1 OpenAI 原生 API 实现

以下是使用 OpenAI 风格 API 实现股票查询的完整示例:

代码来源:根据官方技术文档整理 

from openai import OpenAI
import json
client = OpenAI()
# 1. Define a list of callable tools for the model
tools = [
{
"type": "function",
"name": "get_horoscope",
"description": "Get today's horoscope for an astrological sign.",
"parameters": {
"type": "object",
"properties": {
"sign": {
"type": "string",
"description": "An astrological sign like Taurus or Aquarius",
},
},
"required": ["sign"],
},
},
]
def get_horoscope(sign):
return f"{sign}: Next Tuesday you will befriend a baby otter."
# Create a running input list we will add to over time
input_list = [
{"role": "user", "content": "What is my horoscope? I am an Aquarius."}
]
# 2. Prompt the model with tools defined
response = client.responses.create(
model="gpt-5",
tools=tools,
input=input_list,
)
# Save function call outputs for subsequent requests
input_list += response.output
for item in response.output:
if item.type == "function_call":
if item.name == "get_horoscope":
# 3. Execute the function logic for get_horoscope
horoscope = get_horoscope(json.loads(item.arguments))
# 4. Provide function call results to the model
input_list.append({
"type": "function_call_output",
"call_id": item.call_id,
"output": json.dumps({
"horoscope": horoscope
})
})
print("Final input:")
print(input_list)
response = client.responses.create(
model="gpt-5",
instructions="Respond only with a horoscope generated by a tool.",
tools=tools,
input=input_list,
)
# 5. The model should be able to give a response!
print("Final output:")
print(response.model_dump_json(indent=2))
print("\n" + response.output_text)

3.2 这里有几个非常重要的注意事项:

工具定义规范

工具定义需要遵循 JSON Schema 标准,这是确保模型正确理解工具功能的关键:

# 1. Define a list of callable tools for the model
tools = [
{
"type": "function",
"name": "get_horoscope",
"description": "Get today's horoscope for an astrological sign.",
"parameters": {
"type": "object",
"properties": {
"sign": {
"type": "string",
"description": "An astrological sign like Taurus or Aquarius",
},
},
"required": ["sign"],
},
},
]

函数可以在每个 API 请求的 tools 参数中进行设置。
一个函数由其 schema(模式) 定义,该模式告知模型函数的作用以及它期望的输入参数。
函数定义包含以下属性:

字段描述
type始终为 function
name函数名称(例如:get_weather
description说明何时以及如何使用该函数
parameters定义函数输入参数的 JSON schema
strict是否在调用函数时强制启用严格模式

下面是一个 get_weather 函数的示例定义。

{
"type": "function",
"name": "get_weather",
"description": "Retrieves current weather for the given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and country e.g. Bogotá, Colombia"
},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Units the temperature will be returned in."
}
},
"required": ["location", "units"],
"additionalProperties": false
},
"strict": true
}

多轮工具调用处理:复杂的查询可能需要多次工具调用,这就需要维护对话上下文,这篇文章我们先不过多展开。

3.3 需要规避的常见陷阱

  1. 函数命名规范:使用清晰明确的动词-名词组合
  • ❌ "name": "func1"

  • ✅ "name": "get_weather"

  1. 描述清晰准确:详细说明工具功能和适用场景
  • ❌"description": "天气功能"

  • ✅ "description": "获取指定城市的当前天气情况,包括温度和天气状况"

3.参数定义合理:避免冗余参数,明确必需参数

4、总结与展望

Tool Calling 技术是大模型与现实世界连接的关键桥梁。通过本文的分析,我们可以明确:

  1. 双重依赖关系:Tool Calling 能力既依赖于大模型本身的推理能力,也依赖于推理引擎的接口支持。
  2. 技术成熟度:当前主流框架(OpenAI API、LangChain、Spring AI等)都已提供完善的 Tool Calling 支持,但在具体实现上要甄别差异。
  3. 模型差异性:不同模型在 Tool Calling 能力上存在显著差异,需要根据具体需求选择合适的模型。
  4. 未来发展方向:Tool Calling 正朝着更加智能化、自动化的方向发展,未来可能会出现更复杂的多工具协作和自主规划能力。
  5. 通过合理运用 Tool Calling 技术,我们可以让大语言模型突破其内在限制,真正成为能够与现实世界交互的智能代理(Agent),为构建更加复杂的AI应用奠定坚实基础。

本文代码示例仅供参考,实际使用时请根据具体框架版本进行调整和优化。