ChatGLM3 Chat Format 对话格式规范:多轮对话、工具调用与代码执行全解析
ChatGLM3 Chat Format 对话格式规范:多轮对话、工具调用与代码执行全解析
导读
本文基于 ChatGLM3 官方 PROMPT_en.md 文档,系统讲解 ChatGLM3 系列模型采用的全新对话格式(Chat Format):包括 <|system|>、<|user|>、<|assistant|>、<|observation|> 四种角色头的语法规则、特殊 token 的注入防护机制,以及多轮对话、工具调用(Tool Calling)、代码执行(Code Interpreter)三类场景的完整示例。读完本文,你将掌握如何手工构造符合规范的历史消息、如何在工具调用与代码执行任务中正确编排 <|assistant|> 的 metadata 与 <|observation|> 回填,并能结合仓库内 composite_demo、tools_using_demo、openai_api_demo 的源码验证格式在真实推理链路中的落地方式。
为什么 ChatGLM3 需要一套全新对话格式
传统的大语言模型对话通常以纯文本拼接 role 标签(如 Human: / Assistant:),这种形式存在两个问题:
- 注入攻击风险:用户输入的内容可能包含伪造的对话分隔符,诱导模型"扮演"其他角色或覆盖系统指令;
- 任务输入不统一:Code Interpreter、Tool & Agent 等任务需要传递"外部工具返回结果""代码执行环境"等信息,纯文本格式无法优雅地区分这些语义不同的输入。
为此,ChatGLM3 引入了一套全新的对话格式,将对话分成多个"对话节"(conversation),每个对话节由对话头(chat header)+ 内容组成。该设计在仓库 README 中被描述为模型原生支持多轮对话、工具调用(Function Call)与代码执行(Code Interpreter)能力的基础,参见 README_en.md。
对话头(Chat Header)的语法
对话头独占一整行,格式为:
<|role|>{metadata}
<|role|>部分使用**特殊 token(special token)**表示,文本形态无法被 tokenizer 编码,从机制上杜绝了用户输入伪造角色头的注入攻击;metadata部分使用普通文本表示,是可选内容。
四种角色头及其规则如下:
| 角色头 | 含义 | 规则约束 |
|---|---|---|
| `< | system | >` |
| `< | user | >` |
| `< | assistant | >` |
| `< | observation | >` |
可读性提示:文档示例中每个角色特殊 token 前额外加了一个
\n,这只是为了阅读方便;实际使用和 tokenizer 实现中不应加入这个多余的换行。
角色枚举在源码中的体现
仓库 composite_demo/conversation.py 中,Role 枚举完整映射了这四种角色,并额外细化了工具与解释器两种子角色:
class Role(Enum):
SYSTEM = auto()
USER = auto()
ASSISTANT = auto()
TOOL = auto()
INTERPRETER = auto()
OBSERVATION = auto()
def __str__(self):
match self:
case Role.SYSTEM:
return "<|system|>"
case Role.USER:
return "<|user|>"
case Role.ASSISTANT | Role.TOOL | Role.INTERPRETER:
return "<|assistant|>"
case Role.OBSERVATION:
return "<|observation|>"
注意 Role.TOOL 与 Role.INTERPRETER 在序列化时同样输出为 <|assistant|>,其区别完全由 metadata(工具名或 interpreter)体现——这与下文工具调用、代码执行场景的格式完全一致。
Conversation.__str__(composite_demo/conversation.py)则展示了拼接规则:普通角色输出 角色头\n内容;工具调用输出 角色头{工具名}\n{调用代码};解释器输出 角色头interpreter\n{代码}。
场景一:多轮对话(Multi-turn Dialogue)
多轮对话只使用三种角色:<|user|>、<|assistant|> 与 <|system|>。完整示例:
<|system|>
You are ChatGLM3, a large language model trained by Zhipu.AI. Follow the user's instructions carefully. Respond using markdown.
<|user|>
Hello
<|assistant|>
Hello, I'm ChatGLM3. What can I assist you today?
系统提示词是模型角色设定的核心载体,以上面这套 ChatGLM3 默认系统提示词为例,它同时约束了"遵循用户指令"与"使用 Markdown 回复"两条行为准则。
在 transformers 中发起对话
仓库 README_en.md 给出了标准调用方式,多轮历史直接通过 history 参数回传:
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm3-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm3-6b", trust_remote_code=True, device='cuda')
model = model.eval()
response, history = model.chat(tokenizer, "你好", history=[])
response, history = model.chat(tokenizer, "What should I do if I can't sleep at night?", history=history)
历史记录的组装由 tokenizer 的 build_chat_input(query, history=history, role=role) 完成(见 composite_demo/client.py),history 中每个元素为 {"role": ..., "content": ...} 字典,其中 role 即上文四种角色之一。
场景二:工具调用(Tool Calling)
工具调用场景的完整格式如下(注意 <|assistant|> 后携带工具名作为 metadata):
<|system|>
Answer the following questions as best as you can. You have access to the following tools:
[
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string"},
},
"required": ["location"],
},
}
]
<|user|>
What's the weather in Beijing today?
<|assistant|>
Okay, let's look up the weather in Bejing today.
<|assistant|>get_current_weather
```python
tool_call(location="beijing", unit="celsius")
```
<|observation|>
{"temperature": 22}
<|assistant|>
According to the query results, the temperature in Beijing today is 22 degrees Celsius.
整个流程可以拆解为四个步骤:
- 系统提示注入工具清单:系统提示词固定为
Answer the following questions as best as you can. You have access to the following tools:,随后以 JSON 数组形式给出工具描述(name / description / parameters,其中 parameters 遵循 JSON Schema 的type、properties、required结构); - 模型发起工具调用:模型先输出一段自然语言说明意图,然后输出
<|assistant|>get_current_weather作为对话头,紧接着输出一段tool_call(location="beijing", unit="celsius")形式的调用代码; - 外部工具返回结果:调用方执行工具后,将结果以 JSON 文本放入
<|observation|>角色; - 模型生成最终回答:模型基于观察结果组织最终回复。
工具调用在 composite_demo 中的实现
composite_demo/demo_tool.py 展示了完整的状态机:以 Role.USER 与 Role.OBSERVATION 作为停止序列(stop_sequences=[str(r) for r in (Role.USER, Role.OBSERVATION)])流式生成;当检测到特殊 token <|assistant|> 时切换到"工具"消息块,检测到 <|observation|> 时:
- 按换行把输出切分为
工具名 + 调用参数文本,构造Role.TOOL对话; - 用正则
r'```([^\n]*)\n(.*?)```'从代码块中提取参数,通过eval(code, {'tool_call': tool_call}, {})解析出参数字典; - 调用
dispatch_tool(tool, args)执行真实工具(见 composite_demo/tool_registry.py),并把返回值写回Role.OBSERVATION历史,继续下一轮生成。
工具的定义与注册通过 @register_tool 装饰器完成(composite_demo/tool_registry.py),它利用 typing.Annotated 类型注解自动提取参数名、类型、描述与必填标记,例如:
@register_tool
def get_weather(
city_name: Annotated[str, 'The name of the city to be queried', True],
) -> str:
"""Get the current weather for `city_name`"""
仅支持 chat 方法的工具调用(tools_using_demo)
工具调用当前仅支持 model.chat 方法,不支持 stream_chat(见 tools_using_demo/README_en.md)。官方流程如下:
tools = [
{
"name": "track",
"description": "Track the real-time price of a specified stock",
"parameters": {
"type": "object",
"properties": {
"symbol": {"description": "The stock code that needs to be tracked"}
},
"required": ['symbol']
}
},
{
"name": "text-to-speech",
"description": "Convert text to speech",
"parameters": {
"type": "object",
"properties": {
"text": {"description": "The text that needs to be converted into speech"},
"voice": {"description": "The type of voice to use (male, female, etc.)"},
"speed": {"description": "The speed of the speech (fast, medium, slow, etc.)"}
},
"required": ['text']
}
}
]
system_info = {"role": "system", "content": "Answer the following questions as best as you can. You have access to the following tools:", "tools": tools}
发起查询:
history = [system_info]
query = "Help me inquire the price of stock 10111"
response, history = model.chat(tokenizer, query, history=history)
print(response)
预期输出为 JSON:
{"name": "track", "parameters": {"symbol": "10111"}}
表示模型需要调用工具 track 并传入参数 symbol。拿到工具执行结果后,以 role="observation" 回填:
import json
result = json.dumps({"price": 12412}, ensure_ascii=False)
response, history = model.chat(tokenizer, result, history=history, role="observation")
print(response)
role="observation" 表明该输入是工具调用返回值而非用户输入,不能省略。预期输出:
Based on your query, after the API call, the price of stock 10111 is 12412.
对于更复杂的任务,模型可能需要多次调用工具,此时可通过判断返回的 response 是 str(生成回复)还是 dict(工具调用请求)来决定是否继续循环调用。
工具调用在 OpenAI 兼容 API 中的解析
openai_api_demo/utils.py 的 process_response 展示了从生成文本还原工具调用的解析逻辑:按 <|assistant|> 切分输出,metadata 部分即工具名,代码块内的参数经 eval 解析后封装为 OpenAI 风格的 {"name": ..., "arguments": ...};process_chatglm_messages(openai_api_demo/utils.py)则把 OpenAI 格式的 function 角色映射回 observation 角色、把带工具清单的 system 消息转换为带 tools 字段的消息。同时注意生成端把 <|user|> 与 <|observation|> 作为停止 token(openai_api_demo/utils.py),保证每轮生成在轮到用户或观察输入前收敛。
场景三:代码执行(Code Interpreter)
代码执行场景使用四种角色:<|user|>、<|assistant|>、<|system|> 与 <|observation|>,其中 <|assistant|> 的 metadata 只能是 interpreter。完整示例:
<|system|>
You are an intelligent AI assistant, named ChatGLM3. You are connected to a computer, but please note that you cannot connect to the internet. When using Python to solve tasks, you can run the code and get the results. If there are errors in the running results, you need to improve the code as much as possible. You can process files uploaded by users to your computer, and the default storage path for files is /mnt/data/.
<|user|>
#File: /mnt/data/metadata.jsonl
#Size: 35380
#File uploaded
Are there missing or abnormal values in the file?
<|assistant|>
Firstly, I will read the file you provided and review its contents to determine if there are missing or abnormal values.
We start by loading the file:
<|assistant|>interpreter
```python
import json
# Load the uploaded file
data = []
with open("/mnt/data/metadata.jsonl", 'r') as file:
for line in file:
data.append(json.loads(line))
# Display the first few records to get a sense of the data
data[:5]
```
<|observation|>
```result
[{'file_name': 'aceinthehole.png',
'name': 'Ace in the Hole',
'type': 'survivor',
'description': 'Lady Luck always seems to be throwing something good your way.'},
{'file_name': 'adrenaline.png',
'name': 'Adrenaline',
'type': 'survivor',
'description': 'You are fuelled by unexpected energy when on the verge of escape.'},
{'file_name': 'aftercare.png',
'name': 'Aftercare',
'type': 'survivor',
'description': "Unlocks potential in one's aura reading ability."},
{'file_name': 'agitation.png',
'name': 'Agitation',
'type': 'killer',
'description': 'You get excited in anticipation of hooking your prey.'},
{'file_name': 'alert.png',
'name': 'Alert',
'type': 'survivor',
'description': 'Your acute senses are on high alert.'}]
```
<|assistant|>
This file appears to contain metadata about certain items, each with the following fields:
- `file_name`
- `name`
- `type` (e.g. "survivor" or "killer")
- `description`
Next, I will check for missing values in these fields.
<|assistant|>interpreter
```python
# Check for missing values in each column
missing_values = {key: sum(1 for item in data if key not in item) for key in data[0].keys()}
missing_values
```
<|observation|>
```result
{'file_name': 0, 'name': 0, 'type': 0, 'description': 0}
```
<|assistant|>
Each field has no missing values. This means that each entry is complete.
Next, we can check for any potential outliers. This usually requires deeper analysis and understanding of the data, but I can start by examining some basic statistics and distributions. To do this, we can check the value distribution of the "type" field to see if there are any unexpected categories.
<|assistant|>interpreter
```python
# Check the distribution of values in the 'type' column
type_distribution = {key: sum(1 for item in data if item['type'] == key) for key in set(item['type'] for item in data)}
type_distribution
```
<|observation|>
```result
{'survivor': 116, 'killer': 103}
```
<|assistant|>
The 'type' field has two values: 'survivor' and 'killer'. Among them, 'survivor' has 116 entries, while 'killer' has 103 entries.
From this preliminary examination, we did not find any obvious abnormal values.
If you have any specific standards or other fields that need to be checked, I can provide you with a more detailed analysis. What else do you want me to check?
<|user|>
Help me draw a heart
<|assistant|>interpreter
```python
import numpy as np
import matplotlib.pyplot as plt
# The parametric equation for the shape of a heart
def heart(t):
x = 16 * np.sin(t) ** 3
y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
return x, y
t = np.linspace(0, 2 * np.pi, 1000)
x, y = heart(t)
plt.figure(figsize=(6, 6))
plt.plot(x, y, color='red')
plt.axis('equal')
plt.axis('off')
plt.show()
```
<|observation|>
```result
[Image]
```
<|assistant|>
This is a shape of heart. I used parametric equations to describe this shape and drew it using Matplotlib. If you have any other needs or questions, please feel free to let me know.
<|user|> # End
这段长示例揭示了代码解释器的几个关键行为特征:
- 系统提示词声明能力边界:模型被明确告知"连接着电脑但不能联网""可用 Python 运行代码并获得结果""可处理用户上传文件,默认存储路径 /mnt/data/";
- 用户以文件元信息开头:上传文件以
#File:(路径)与#Size:(大小)注释作为上下文注入用户消息; - 代码以
<|assistant|>interpreter头开始:每个代码块前都有独立的<|assistant|>interpreter对话头; - **执行结果以
\``result代码块回填**:文本结果以```result块包裹,绘图等图像结果以[Image]` 占位; - 多轮交互直至
# End结束:用户以# End注释终止对话。
代码解释器在 composite_demo 中的实现
composite_demo/demo_ci.py 的生成循环中,模型流式输出遇到 <|assistant|> 特殊 token 时切到 interpreter 消息块;遇到 <|observation|> 时,用 extract_code 提取代码块并通过 CodeKernel.execute 在 Jupyter 内核(jupyter_client.KernelManager,见 composite_demo/demo_ci.py)中真实执行:
res_type, res = execute(code, get_kernel())
execute(composite_demo/demo_ci.py)会先剥离代码中的 <|observation|>、<|assistant|>interpreter 等格式标记,再把内核执行结果分类为 text 或 image(image/png 输出经 base64 解码为 PIL Image);文本结果超过 truncate_length(默认 1024)时截断并追加 [TRUNCATED] 标记。执行结果随后以 Role.OBSERVATION 写回历史,模型据此继续生成下一轮回复,形成"思考 → 写代码 → 观察结果 → 再思考"的闭环。
实践:手工构造与解析对话格式的通用要点
序列化(构造 prompt)
综合 composite_demo/conversation.py 的 preprocess_text,构造完整 prompt 的顺序为:
<|system|>\n{系统提示} # 无工具时
<|system|>\nAnswer the following questions as best as you can. You have access to the following tools:\n{tools JSON} # 有工具时
<|user|>\n{内容}
<|assistant|>\n{内容}
<|assistant|>{工具名}\n{工具调用代码}
<|assistant|>interpreter\n{代码}
<|observation|>\n{结果}
<|assistant|>\n # 末尾追加,等待模型续写
其中工具列表在 preprocess_text 中会被序列化为缩进 JSON(json.dumps(tools, indent=4, ensure_ascii=False))并拼接在固定引导语之后。
反序列化(解析生成结果)
生成结果解析的通用要点(综合 composite_demo/conversation.py 与 openai_api_demo/utils.py):
- 特殊 token 本身(
<|assistant|>、<|observation|>等)需要通过postprocess_text清理,避免污染展示文本; - 代码块内容通过
```围栏正则提取; - 工具调用按
metadata行(工具名)+ 参数代码块拆分,参数通过eval解析为字典; - 流式生成时必须把
<|user|>、<|observation|>加入停止 token / 停止序列(见 openai_api_demo/utils.py 与 composite_demo/demo_tool.py),否则模型会一直生成到下一轮用户输入。
运行与验证
可以在仓库中直接运行完整集成的演示(对话 / 工具 / 代码解释器三合一),参见 composite_demo/README_en.md:
cd composite_demo
pip install -r requirements.txt
streamlit run main.py
集成演示通过 composite_demo/main.py 按 --tool、--ci 等参数切换 Tool 与 Code Interpreter 模式,是观察格式在实际推理链路中流转最直观的入口。此外,工具调用的最小复现路径是 tools_using_demo,代码解释器的最小复现路径是 composite_demo/demo_ci.py。
小结
ChatGLM3 的对话格式以特殊 token 角色头为安全边界,以 metadata 承载工具名与解释器标记,以 <|observation|> 统一外部结果回填,从而让多轮对话、工具调用、代码执行三类任务共用同一套输入协议。无论是通过 transformers 的 model.chat / stream_chat、OpenAI 兼容 API(openai_api_demo),还是 Streamlit 集成演示,都遵循本文所述的同一套格式规范。构造 prompt 时务必遵守"角色顺序、metadata 限定、结果回填"三条规则,并配合 <|user|> / <|observation|> 停止 token 使用,即可稳定获得符合预期的生成行为。