📋 JSON Mode(结构化输出模式)
让AI说"人话"的同时,也能说"机器话"——输出干净、可解析的JSON数据
📖 定义
"AI说的每句话都像诗——优美、深意、但你没法直接存进数据库。JSON Mode就是把诗翻译成数据库能懂的语言。"
JSON Mode(JSON模式)是让大语言模型输出符合JSON格式的结构化数据的技术。它解决了AI输出"自由文本"难以被程序解析的问题,是Agent与外部系统交互的基础能力。
🎮 周星驰式比喻:JSON Mode就像给AI装了一个"格式化输出"的开关。关掉的时候,AI像一个诗人,说话随性飘逸。打开的时候,AI像一个程序员,每句话都是标准格式,可以直接被程序解析。"用户姓名:张三,年龄:25"——多整齐!
⚙️ 工作原理
1. 输出模式对比
| 模式 | 输出格式 | 适用场景 |
|---|---|---|
| 自由文本 | 自然语言 | 对话、创意写作 |
| JSON Mode | 合法JSON | 数据提取、API集成 |
| Structured Output | 符合Schema的JSON | 表单填写、严格类型 |
| Tool Calling | 函数调用格式 | Agent工具调用 |
2. OpenClaw JSON输出配置
# OpenClaw 结构化输出配置
output:
format: "json" # text | json | json_schema
schema:
type: "object"
properties:
title:
type: "string"
description: "文章标题"
summary:
type: "string"
description: "内容摘要"
tags:
type: "array"
items:
type: "string"
confidence:
type: "number"
minimum: 0
maximum: 1
required: ["title", "summary"]
3. Tool Calling 中的JSON
# Agent 工具调用的JSON格式
{
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "web_search",
"arguments": {
"query": "OpenClaw latest features",
"count": 10
}
}
}
]
}
🔧 实战应用
场景1:数据提取
# 从非结构化文本提取结构化数据
Input: "张三,25岁,在北京工作,月薪15000"
Output (JSON Mode):
{
"name": "张三",
"age": 25,
"city": "北京",
"salary": 15000
}
场景2:批量处理
# 批量生成SEO元数据
{
"pages": [
{
"url": "/glossary/json-mode.html",
"title": "JSON Mode 是什么?",
"description": "了解JSON Mode...",
"keywords": ["JSON", "结构化输出"]
}
]
}