10.Control output format (JSON mode) 控制输出格式(JSON 模式)

2024年4月13日修改
Claude is highly capable of producing output in a wide variety of formats. By providing clear instructions, examples, and prefilled responses, you can guide Claude to generate responses that adhere to your desired structure and style.
Claude 能够以多种格式生成输出,通过提供清晰的指导、示例和预填的响应,您可以引导 Claude 生成符合您期望的结构和风格的响应。
Specifying the desired format
指定所需格式
One of the simplest ways to control Claude's output is to simply state the format you want. Claude can understand and follow instructions related to formatting, and format outputs such as:
控制 Claude 输出的最简单方法之一是简单地说明您想要的格式。 Claude 可以理解并遵循与格式相关的指示,并格式化输出,例如:
JSON
XML
HTML
Markdown
CSV
Custom formats 自定义格式
For example, if you want Claude to generate a haiku in JSON format, you can use a prompt like this:
例如,如果你想让 Claude 以 JSON 格式生成一首俳句,你可以使用这样的提示:
Prefilling Claude's response
充填克劳德的回答
Another effective way to control Claude's output format is to prefill the Assistant field with the beginning of the desired format. This technique is particularly useful when working with structured formats like JSON or HTML.
另一种有效控制 Claude 输出格式的方法是在所需格式的开头用 Assistant 字段预填充。当处理结构化格式如 JSON 或 HTML 时,这种技术特别有用。
For example, to generate a recipe in JSON format:
例如,要生成 JSON 格式的食谱:
By prefilling the response with the opening JSON curly brace, you give Claude a clear starting point for generating the recipe in the desired format. This also is another technique you can use to have Claude skip its friendly preamble text.