6.Use XML tags 使用 XML 标签

2024年4月13日修改
XML tags are a powerful tool for structuring prompts and guiding Claude's responses. Claude is particularly familiar with prompts that have XML tags as Claude was exposed to such prompts during training. By wrapping key parts of your prompt (such as instructions, examples, or input data) in XML tags, you can help Claude better understand the context and generate more accurate outputs. This technique is especially useful when working with complex prompts or variable inputs.
XML 标记是一种强大的工具,用于构建提示和引导克劳德的回应。克劳德对具有 XML 标记的提示特别熟悉,因为在培训期间接触过这样的提示。通过将提示的关键部分(如说明、示例或输入数据)用 XML 标记包裹起来,您可以帮助克劳德更好地理解上下文并生成更准确的输出。在处理复杂提示或可变输入时,这种技术尤其有用。
What are XML tags?
XML tags are angle-bracket tags like <tag></tag>. They come in pairs and consist of an opening tag, such as <tag>, and a closing tag marked by a /, such as </tag>. XML tags are used to wrap around content, like this: <tag>content</tag>.
XML 标签是像 <tag></tag> 这样的尖括号标签。它们成对出现,由一个起始标签组成,例如 <tag> ,以及由 / 标记的闭合标签,例如 </tag> 。XML 标签用于包裹内容,如下所示: <tag>content</tag>
Opening and closing XML tags should share exactly the same name. The tag name can be anything you like, as long as it's wrapped in angle brackets, although we recommend naming your tags something contextually relevant to the content it's wrapped around.
开始和结束的 XML 标签必须完全相同。标签名可以是任何你喜欢的,只要它被尖括号包围,尽管我们建议将标签命名为与其周围内容相关的上下文相关的名称。
XML tags should always be referred to in pairs and never as just as the first half of a set (e.g., Using the document in <doc></doc> tags, answer this question. ).
XML 标签应始终成对引用,而不应只作为一组的第一部分(例如: Using the document in <doc></doc> tags, answer this question. )。
💡XML tag names XML 标签名称
There is no canonical best set of XML tag names that Claude performs particularly well with. For example, <doc> works just as well as <document>. The only time you need very specific XML tag names is in the case of function calling.
没有一个特定的 XML 标签名称集合是 Claude 特别擅长的。例如, <doc><document> 的效果一样好。唯一需要非常具体的 XML 标签名称的情况是在函数调用的情况下。
Why use XML tags? 为什么使用 XML 标签?
There are several reasons why you might want to incorporate XML tags into your prompts:
有几个原因可能会让您想要将 XML 标记合并到您的提示中:
1.
Improved accuracy: XML tags help Claude distinguish between different parts of your prompt, such as instructions, examples, and input data. This can lead to more precise parsing of your prompt and thus more relevant and accurate responses, particularly in domains like mathematics or code generation.
提高准确性:XML 标记有助于克劳德区分提示中的不同部分,如说明、示例和输入数据。这可以导致更精确地解析您的提示,从而获得更相关和准确的响应,特别是在数学或代码生成等领域。
2.
Clearer structure: Just as headings and sections make documents easier for humans to follow, XML tags help Claude understand the hierarchy and relationships within your prompt.
更清晰的结构:正如标题和章节使文档更易于人类理解一样,XML 标记有助于克劳德理解提示中的层次结构和关系。