跳到内容
SCRM DOC
JSON SchemaLLM结构化输出DifyAIGPT-4o函数调用工具集成提示词工程创建于2025-03-07更新于 2026-01-10来源:https://docs.dify.ai/zh-hans/learn-more/extended-reading/how-to-use-json-schema-in-dify版本2

JSON Schema 在 LLM 结构化输出中的应用

JSON Schema 是一种用于描述 JSON 数据结构的规范,通过定义 JSON Schema 结构,开发者可以约束大型语言模型 (LLM) 的输出,使其严格遵循预定义的格式和内容。这对于需要 LLM 按照特定格式输出文档或代码结构等场景至关重要。

支持 JSON Schema 功能的模型

以下模型支持 JSON Schema 功能:

  • gpt-4o-mini-2024-07-18 及更高版本
  • gpt-4o-2024-08-06 及更高版本

更多关于 OpenAI 系列模型的结构化输出能力,请参考相关文档。

结构化输出方法

以下是利用 JSON Schema 实现 LLM 结构化输出的典型流程:

  1. 工具/函数集成: 将 LLM 与系统中的工具、函数、数据等进行连接。在函数定义中,设置 strict: true。启用此选项后,结构化输出功能可确保 LLM 为函数调用生成的参数与函数定义中提供的 JSON Schema 完全匹配。
  2. 结构化输出: LLM 在响应用户查询时,将按照 JSON Schema 中的定义,以结构化的内容格式输出。

以下将介绍如何在 Dify 平台开启并使用 JSON Schema 功能。

1. 开启 JSON Schema 功能

首先,将应用中使用的 LLM 切换至上述支持 JSON Schema 输出的模型。然后在设置表单中,启用 JSON Schema 选项,并填写 JSON Schema 模板。同时,开启 response_format 栏,并切换至 json_schema 格式。

LLM 生成的内容支持以下格式输出:

  • Text: 以纯文本格式输出

2. 定义 JSON Schema 模板

可以参考以下 JSON Schema 格式并定义模板内容:

{
    "name": "template_schema",
    "description": "A generic template for JSON Schema",
    "strict": true,
    "schema": {
        "type": "object",
        "properties": {
            "field1": {
                "type": "string",
                "description": "Description of field1"
            },
            "field2": {
                "type": "number",
                "description": "Description of field2"
            },
            "field3": {
                "type": "array",
                "description": "Description of field3",
                "items": {
                    "type": "string"
                }
            },
            "field4": {
                "type": "object",
                "description": "Description of field4",
                "properties": {
                    "subfield1": {
                        "type": "string",
                        "description": "Description of subfield1"
                    }
                },
                "required": ["subfield1"],
                "additionalProperties": false
            }
        },
        "required": ["field1", "field2", "field3", "field4"],
        "additionalProperties": false
    }
}

步骤指导:

  1. 定义基本信息:
    • 设置 name:为 schema 提供一个描述性的名称。
    • 添加 description:简要说明 schema 的用途。
    • 设置 strict: true:启用严格模式,强制 LLM 严格遵循 schema 定义。
  2. 创建 schema 对象:
    • 设置 type: "object":指定根级别为对象类型。
    • 添加 properties 对象:用于定义所有字段。
  3. 定义字段:
    • 为每个字段创建一个对象,包含 type 和 description。
    • 常用类型包括:string, number, boolean, array, object。
    • 对于数组类型,使用 items 定义数组元素的类型。
    • 对于对象类型,递归定义 properties。
  4. 设置约束:
    • 在每个级别添加 required 数组,列出所有必需字段。
    • 在每个对象级别设置 additionalProperties: false,禁止添加未定义的属性。
  5. 特殊字段处理:
    • 使用 enum 限制字段的可选值。
    • 使用 $ref 实现递归结构,允许 schema 引用自身。

示例

1. 推理链(常规)

JSON Schema 文件示例

{
    "name": "math_reasoning",
    "description": "Records steps and final answer for mathematical reasoning",
    "strict": true,
    "schema": {
        "type": "object",
        "properties": {
            "steps": {
                "type": "array",
                "description": "Array of reasoning steps",
                "items": {
                    "type": "object",
                    "properties": {
                        "explanation": {
                            "type": "string",
                            "description": "Explanation of the reasoning step"
                        },
                        "output": {
                            "type": "string",
                            "description": "Output of the reasoning step"
                        }
                    },
                    "required": ["explanation", "output"],
                    "additionalProperties": false
                }
            },
            "final_answer": {
                "type": "string",
                "description": "The final answer to the mathematical problem"
            }
        },
        "additionalProperties": false,
        "required": ["steps", "final_answer"]
    }
}

提示词参考

You are a helpful math tutor. You will be provided with a math problem,
and your goal will be to output a step by step solution, along with a final answer.
For each step, just provide the output as an equation use the explanation field to detail the reasoning.

2. UI 生成器(根递归模式)

{
    "name": "ui",
    "description": "Dynamically generated UI",
    "strict": true,
    "schema": {
        "type": "object",
        "properties": {
            "type": {
                "type": "string",
                "description": "The type of the UI component",
                "enum": ["div", "button", "header", "section", "field", "form"]
            },
            "label": {
                "type": "string",
                "description": "The label of the UI component, used for buttons or form fields"
            },
            "children": {
                "type": "array",
                "description": "Nested UI components",
                "items": {
                    "$ref": "#"
                }
            },
            "attributes": {
                "type": "array",
                "description": "Arbitrary attributes for the UI component, suitable for any element",
                "items": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string",
                            "description": "The name of the attribute, for example onClick or className"
                        },
                        "value": {
                            "type": "string",
                            "description": "The value of the attribute"
                        }
                    },
                  "additionalProperties": false,
                  "required": ["name", "value"]
                }
            }
        },
        "required": ["type", "label", "children", "attributes"],
        "additionalProperties": false
    }
}

提示词参考:

You are a UI generator AI. Convert the user input into a UI.

效果示例:

使用提示

  • 异常处理: 确保应用提示词中包含处理用户输入无法产生有效响应的情况说明,例如返回错误信息或默认值。
  • 模式兼容性: 模型会尽力遵循提供的模式,但如果输入内容与指定的模式完全无关,可能会导致 LLM 产生幻觉 1。
  • 空参数处理: 如果 LLM 检测到输入与任务不兼容,可以在提示中包含指令,指定返回空参数或特定语句。
  • 字段要求: 所有字段都应设置为 required,以确保输出的完整性。
  • 禁止额外属性: 必须始终在对象中设置 additionalProperties: false,以防止输出包含未定义的属性。
  • 根对象类型: Schema 的根级别对象必须是一个对象。

务必仔细设计 JSON Schema,并提供清晰的提示词,以确保 LLM 能够生成符合预期的结构化输出。

参考阅读

Previous提示词编排

Next常见问题

Footnotes

  1. 幻觉 (Hallucination) 在 LLM 领域中指的是模型生成不真实或与输入不一致的内容的现象。 ↩

页面信息›
已优化是

本页目录