Working with Schemas
Working with Schemas
正如我们在本文中多次看到的,使用结构化 JSON 作为输出是一种极佳的解决方案。但对于 input 呢?虽然 JSON 非常适合构建 LLM 生成的 output,但它同样能有效组织您提供的 input~。
这正是 JSON Schema 发挥作用之处。JSON Schema 定义了预期输入 JSON 的结构与数据类型,通过提供模式,您为 LLM 提供了清晰的数据蓝图,使其能聚焦于 Ñõ • ` ð •相关信息,降低输入误解风险。此外,模式还能建立数据间关联,甚至通过包含特定格式的日期 / 时间戳字段,让 LLM 具备 “ 时间感知 ” 能力。
这里有个简单示例:
假设您想用 LLM 为电商目录中的商品生成描述。与其仅提供自由文本描述,您可以使用 JSON Schema 定义商品属性:
{ "type": "object", "properties": { "name": { "type": "string", "description": "Product name" }, "category": { "type": "string", "description": "Product category" }, "price": { "type": "number", "format": "float", "description": "Product price" }, "features": { "type": "array", "items": { "type": "string" }, "description": "Key features of the product"
}, "release_date": { "type": "string", "format": "date", "description":
"Date the product was released"} },
片段 5. 结构化输出模式的定义
全站分卷索引 · 34 卷
- 1Introduction
- 2Prompt engineering
- 3LLM output configuration
- 4Sampling controls
- 5Top-K and top-P
- 6Putting it all together
- 7Prompting techniques
- 8One-shot & few-shot
- 9System, contextual and role prompting
- 10System prompting
- 11Role prompting
- 12Contextual prompting
- 13Step-back prompting
- 14Chain of Thought (CoT)
- 15Self-consistency
- 16Tree of Thoughts (ToT)
- 17ReAct (reason & act)
- 18Automatic Prompt Engineering
- 19Code prompting
- 20Prompts for explaining code
- 21Prompts for translating code
- 22Prompts for debugging and reviewing code
- 23What about multimodal prompting?
- 24Design with simplicity
- 25Be specific about the output
- 26Control the max token length
- 27Experiment with input formats and writing styles
- 28Adapt to model updates
- 29JSON Repair
- 30Working with Schemas
- 31Experiment together with other prompt engineers
- 32CoT Best practices
- 33Summary
- 34Endnotes