目录
[一、为什么普通 LLM 能力有限](#一、为什么普通 LLM 能力有限)
[二、什么是 Function Calling](#二、什么是 Function Calling)
[三、Function Calling 的核心思想](#三、Function Calling 的核心思想)
[四、第一个 Function Calling 示例](#四、第一个 Function Calling 示例)
[五、OpenAI Function Calling 原理](#五、OpenAI Function Calling 原理)
[六、Function Calling 工作流程](#六、Function Calling 工作流程)
[八、为什么 Agent 必须依赖 Function Calling](#八、为什么 Agent 必须依赖 Function Calling)
[九、什么是 Structured Output](#九、什么是 Structured Output)
[十、JSON 格式输出](#十、JSON 格式输出)
[十一、Pydantic 结构化输出](#十一、Pydantic 结构化输出)
[十二、LangChain Structured Output](#十二、LangChain Structured Output)
[十四、Function Calling 与 Structured Output 的关系](#十四、Function Calling 与 Structured Output 的关系)
[十五、企业级 AI 系统架构](#十五、企业级 AI 系统架构)
[十六、Function Calling 实战案例](#十六、Function Calling 实战案例)
[什么是 Function Calling?](#什么是 Function Calling?)
[Function Calling 的作用是什么?](#Function Calling 的作用是什么?)
[什么是 Structured Output?](#什么是 Structured Output?)
[为什么 Agent 依赖 Function Calling?](#为什么 Agent 依赖 Function Calling?)
[JSON 输出有什么优势?](#JSON 输出有什么优势?)
一、前言
很多初学者第一次接触大语言模型(LLM)时,会觉得它非常强大。
例如:
能写代码
能翻译
能回答问题
能生成文章
但是当真正开发 AI 应用时,很快会发现一个问题:
LLM 只能生成文本
例如:
用户提问:
帮我查一下北京天气
普通 LLM 只能回答:
我无法获取实时天气数据
因为:
LLM 并不能直接访问互联网
也不能直接调用系统接口
这时就需要:
Function Calling
即:
函数调用
工具调用
函数回调
能力。
而另一项关键能力:
Structured Output
即:
结构化输出
能够让模型返回:
JSON
对象
数据结构
而不是一段无法解析的自然语言。
本文将带你掌握:
什么是 Function Calling
为什么需要 Function Calling
Function Calling 工作原理
什么是 Structured Output
JSON 输出规范
LangChain 如何实现函数调用
Agent 如何依赖 Function Calling
一、为什么普通 LLM 能力有限
传统 LLM:
输入问题
↓
模型推理
↓
输出文本
架构:

例如:
今天北京天气怎么样?
模型:
抱歉,我无法访问实时天气。
原因:
模型无法直接操作外部世界
不能:
访问数据库
查询天气
发送邮件
调用接口
执行代码
因此:
需要赋予模型工具能力
二、什么是 Function Calling
Function Calling:
函数调用
本质:
让模型决定
什么时候调用工具
调用哪个工具
传递什么参数
例如:
用户:
查询北京天气
模型分析后:
{
"function":"get_weather",
"city":"北京"
}
系统接收到后:
get_weather("北京")
执行。
流程:

三、Function Calling 的核心思想
过去:
LLM直接回答
现在:
LLM负责决策
程序负责执行
职责划分:
| 组件 | 职责 |
|---|---|
| LLM | 判断调用什么工具 |
| Function | 执行具体操作 |
| 应用系统 | 返回结果 |
可以理解为:
LLM 是大脑
Function 是双手
四、第一个 Function Calling 示例
定义天气函数:
python
def get_weather(city):
return f"{city} 当前温度 28℃"
用户:
北京天气如何?
模型输出:
{
"name":"get_weather",
"arguments":{
"city":"北京"
}
}
程序:
result = get_weather("北京")
结果:
北京 当前温度 28℃
五、OpenAI Function Calling 原理
定义工具:
python
tools = [
{
"type":"function",
"function":{
"name":"get_weather",
"description":"查询天气",
"parameters":{
"type":"object",
"properties":{
"city":{
"type":"string"
}
}
}
}
}
]
发送请求:
python
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools
)
模型返回:
python
{
"tool_calls":[
{
"name":"get_weather",
"arguments":{
"city":"北京"
}
}
]
}
六、Function Calling 工作流程
完整流程:
sequenceDiagram
User->>LLM: 北京天气
LLM->>Tool: get_weather("北京")
Tool->>LLM: 28℃
LLM->>User: 北京当前温度28℃
七、常见工具场景
查询天气
get_weather(city)
查询数据库
query_user(id)
调用搜索引擎
search_google(keyword)
发邮件
send_email(to, content)
查询订单
query_order(order_no)
八、为什么 Agent 必须依赖 Function Calling
Agent 本质:
LLM
+
Tool Calling
没有工具调用:
Agent = 聊天机器人
有工具调用:
Agent = 智能助手
架构:

九、什么是 Structured Output
除了工具调用。
另一项重要能力:
Structured Output
即:
结构化输出
很多时候:
我们不希望模型返回:
张三今年25岁,是Java工程师。
而希望:
python
{
"name":"张三",
"age":25,
"job":"Java工程师"
}
因为:
程序更容易解析
十、JSON 格式输出
最常见形式:
python
{
"username":"admin",
"age":18,
"email":"admin@test.com"
}
Prompt:
python
请仅返回JSON格式:
{
"username":"",
"age":0
}
输出:
python
{
"username":"Tom",
"age":20
}
十一、Pydantic 结构化输出
现代 LLM 推荐:
Pydantic
定义模型:
python
from pydantic import BaseModel
class User(BaseModel):
name:str
age:int
模型输出:
{
"name":"Tom",
"age":20
}
自动转换:
python
user = User.model_validate_json(
response
)
十二、LangChain Structured Output
定义数据结构:
python
from pydantic import BaseModel
class Weather(BaseModel):
city:str
temperature:int
绑定:
python
structured_llm = llm.with_structured_output(
Weather
)
调用:
python
result = structured_llm.invoke(
"北京天气"
)
输出:
python
Weather(
city="北京",
temperature=28
)
十三、为什么需要格式化输出
传统输出:
北京今天晴天,温度28度。
程序解析困难。
结构化输出:
python
{
"city":"北京",
"temperature":28
}
程序可直接使用。
优势:
稳定
可解析
可校验
适合生产环境
十四、Function Calling 与 Structured Output 的关系
很多人会混淆。
区别:
| 技术 | 作用 |
|---|---|
| Function Calling | 调用工具 |
| Structured Output | 输出结构化数据 |
Function Calling:
解决执行问题
Structured Output:
解决数据格式问题
十五、企业级 AI 系统架构
现代 AI 系统:
Prompt
+
Function Calling
+
Structured Output
+
Memory
+
RAG
+
Agent
架构:

十六、Function Calling 实战案例
智能客服:
用户:
查询订单10086
模型:
python
{
"function":"query_order",
"order_no":"10086"
}
系统:
query_order("10086")
返回:
{
"status":"已发货"
}
最终:
您的订单已发货。
十七、面试高频问题
什么是 Function Calling?
让模型决定调用哪个工具以及传递什么参数
Function Calling 的作用是什么?
赋予模型访问外部系统的能力
什么是 Structured Output?
让模型输出固定格式的数据结构
为什么 Agent 依赖 Function Calling?
Agent需要通过工具完成任务
JSON 输出有什么优势?
易解析
易校验
适合系统集成
十八、总结
Function Calling 和 Structured Output 是现代 LLM 应用开发中的两项核心能力。
它们解决了两个关键问题:
Function Calling
解决:
模型如何执行任务
Structured Output
解决:
模型如何返回标准数据
可以这样理解:
Prompt
负责沟通
Function Calling
负责行动
Structured Output
负责规范数据
Memory
负责记忆
RAG
负责查知识
Agent
负责完成目标
因此:
Function Calling 让 LLM 从"会说话"变成"会做事",Structured Output 让 LLM 从"输出文本"变成"输出数据"。
掌握这两项技术后,你就真正具备了开发企业级 AI Agent、智能客服、知识库问答系统、自动化办公助手等现代 AI 应用的核心能力。