跳到内容
SCRM DOC
JSONSchemaLLM结构化输出DifyPrompt工程创建于2025-03-04更新于 2025-03-04来源:https://docs.dify.ai/zh-hans/learn-more/extended-reading/how-to-use-json-schema-in-dify

如何使用 JSON Schema 让 LLM 输出遵循结构化格式的内容

来源:Dify 官方文档

概述

JSON Schema 是一种用于描述 JSON 数据结构的规范。开发者可以通过定义 JSON Schema 结构,指定 LLM 输出严格遵循定义内的数据或内容格式,例如按照指定格式输出文档或代码结构。

支持的模型:

  • gpt-4o-mini-2024-07-18 及以上版本
  • gpt-4o-2024-08-06 及以上版本

如需了解更多关于 OpenAI 系列模型的结构化输出能力,请参考 Structured Outputs。

使用路径

LLM 结构化输出有两种典型用法:

  1. 连接工具/函数:在函数定义中设置 strict: true,结构化输出功能确保 LLM 生成的参数与你提供的 JSON Schema 完全匹配。
  2. 结构化内容输出:LLM 回答用户时,按照 JSON Schema 中的定义,以结构化内容格式输出。

在 Dify 中使用

将应用中的 LLM 切换至上述支持 JSON Schema 输出的模型,然后在设置表单中:

  1. 开启 JSON Schema 并填写 JSON Schema 模板
  2. 开启 response_format 栏并切换至 json_schema 格式

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 起一个描述性的名称
  2. 添加 description:简要说明 schema 的用途
  3. 设置 type: "object":指定根级别为对象类型
  4. 添加 properties 对象:用于定义所有字段
  5. 为每个字段创建对象,包含 type 和 description
  6. 常见类型:string、number、boolean、array、object
  7. 在每个级别添加 required 数组,列出所有必需字段
  8. 在每个对象级别设置 additionalProperties: false

示例一:数学推理

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.

示例二:动态 UI 生成

JSON Schema:

{
    "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 产生幻觉
  • 可以在提示中指定当输入与任务不兼容时返回空参数或特定句子
  • 所有字段必须设为 required,详见 Supported Schemas

相关文章

本页目录