← 返回目录

Working with Schemas

第 30 / 34 卷日期:

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 卷
  1. 1Introduction
  2. 2Prompt engineering
  3. 3LLM output configuration
  4. 4Sampling controls
  5. 5Top-K and top-P
  6. 6Putting it all together
  7. 7Prompting techniques
  8. 8One-shot & few-shot
  9. 9System, contextual and role prompting
  10. 10System prompting
  11. 11Role prompting
  12. 12Contextual prompting
  13. 13Step-back prompting
  14. 14Chain of Thought (CoT)
  15. 15Self-consistency
  16. 16Tree of Thoughts (ToT)
  17. 17ReAct (reason & act)
  18. 18Automatic Prompt Engineering
  19. 19Code prompting
  20. 20Prompts for explaining code
  21. 21Prompts for translating code
  22. 22Prompts for debugging and reviewing code
  23. 23What about multimodal prompting?
  24. 24Design with simplicity
  25. 25Be specific about the output
  26. 26Control the max token length
  27. 27Experiment with input formats and writing styles
  28. 28Adapt to model updates
  29. 29JSON Repair
  30. 30Working with Schemas
  31. 31Experiment together with other prompt engineers
  32. 32CoT Best practices
  33. 33Summary
  34. 34Endnotes