For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
主导航

引用格式

让模型生成可靠的引用。

可靠的引用能够建立信任,帮助读者核实回答的准确性。本指南提供实用建议,介绍如何准备可引用材料,并使用 OpenAI 模型熟悉的模式,指导模型正确设置引用格式。

概览

引用系统包含多个环节:您需要确定哪些内容可以引用,清晰地呈现这些材料,指导模型如何引用,并在向用户展示结果之前进行验证。

本指南介绍模型直接接触的五个核心要素:

  1. 可引用单元:定义允许模型引用的内容。
  2. 材料呈现:以清晰、结构化的格式呈现源材料。
  3. 引用格式:明确指定模型应使用的引用格式。
  4. 提示指令:告诉模型何时引用,以及如何正确引用。
  5. 引用解析:从模型的回答中提取引用,供后续使用。

选择可引用单元

编写提示之前,请明确定义模型可以引用哪些内容。常见选项包括:

可引用单元最适用的场景不足示例
文档您只需要说明回答来自哪份文档。不够精确。如果只需说明哪份文档支持某项论断,可以引用整本员工手册。
文本块/分块您希望在简便性和精确性之间取得良好平衡。仍无法精确到行。引用包含相关条款的具体合同段落或检索到的分块。
行范围您需要展示作为依据的确切文本。对模型而言难度更大。当用户需要核实确切的段落时,引用 L42-L47 行。

合适的可引用单元应具备以下特点:

  • 保持一致:同一来源在不同运行中应保持相同的 ID。
  • 便于核查:读者能够阅读其内容并理解上下文。
  • 大小适中:包含足够的内容以表达清楚含义,同时范围足够小以保持精确。

对于大多数系统,块级引用是最佳默认选择。与行级引用相比,模型通常更容易生成块级引用;与文档级引用相比,块级引用对用户更有用。

呈现可引用材料

如果材料没有清晰呈现,模型就无法引用。无论材料来自工具还是直接注入,都应确保其包含以下内容:

  • 稳定的来源 ID:保持一致的标识符,例如 file1block1
  • 易读的文本:格式清晰的源材料。
  • 元数据(可选):URL、时间戳、标题及类似的上下文信息。

来源 ID 与定位符的区别:来源 ID 是由模型生成的稳定标识符, 例如 block1。定位符则是 界面中渲染的精确高亮位置,例如 lines L8-L13Paragraph 21。通常,模型应输出来源 ID, 由您的系统解析或渲染定位符。过早混用两者 往往会增加格式错误。

定义引用格式

您需要定义模型生成引用时使用的格式。请采用明确、一致且便于模型稳定复现的格式。

以下是我们推荐的引用格式和标记。我们强烈建议使用这些引用标记,因为它们与模型训练时使用的标记非常接近。如果您选择其他标记值,请尽量保持整体引用格式相似。

组成部分作用推荐值
CITATION_START标示引用标记的开始。\ue200
引用类别标识引用类型。所有支持的来源均使用 citecite
CITATION_DELIMITER分隔标记内部的字段。\ue202
来源 ID标识被引用的单元。turn# 表示轮次编号。item# 表示具体的文件、文本块或 URL。turn0file1turn0block1turn0url1
定位符(可选)将引用范围缩小到精确的文本片段。L8-L13
CITATION_STOP标示引用标记的结束。\ue201

对于工具调用,turnN 在每次调用工具时递增一次, 而不是每返回一条结果就递增一次。在同一次调用中,各个来源 通过 file0file1 等后缀区分。 在单次回答系统中,所有引用都采用 turn0... 的形式,前提是模型在回答之前 恰好调用了一次工具。如果模型调用了多次工具,您可能会看到 turn0fileXturn1fileX 等形式的引用。

模板

{CITATION_START}<citation_family>{CITATION_DELIMITER}<source_id>{CITATION_DELIMITER}<locator>{CITATION_STOP}

示例

{CITATION_START}cite{CITATION_DELIMITER}turn0file1{CITATION_DELIMITER}L8-L13{CITATION_STOP}

如果您的系统不使用定位符,请省略该字段:

{CITATION_START}cite{CITATION_DELIMITER}turn0file1{CITATION_STOP}

编写有效的引用指令

为尽可能保持准确,请使用模型熟悉的引用模式。自定义格式或不熟悉的格式会增加模型的认知负担,导致引用错误,尤其是在以下情况下:

  • 推理投入较低时,模型用于纠正格式错误的预算更少。
  • 任务复杂度较高时,大部分推理预算都用于解决任务本身,而非修正引用语法。

下面推荐的引用格式接近模型熟悉的模式。您可以直接使用,也可以根据自己的系统进行调整。

如果您想自行编写提示,请明确以下内容:

  • 标记的确切语法。
  • 引用的放置位置。
  • 何时需要引用,何时不需要引用。
  • 如何引用多项支持材料。
  • 禁止使用哪些格式。
  • 缺少支持材料时如何处理。

解析引用

模型生成引用后,您需要从响应文本中提取这些引用,以便解析来源 ID、渲染链接,或在向用户展示答案前移除原始标记。

下面的辅助代码可直接复制到您的应用中。它可以解析单来源引用、多来源引用以及可选的行范围定位符,同时保留它们在原文中的字符偏移量。

此示例仅支持行定位符。如果您的系统使用其他定位符格式,请相应调整。

如果您的来源 ID 使用其他格式,请更新 SOURCE_ID_RE, 使其与您的系统匹配。

示例

以下示例展示了两种常见的引用模式:

  • 工具检索的上下文:由您的工具返回可引用的材料及其 ID。
  • 注入的上下文:由您在提示中直接提供可引用的文本块。

为工具检索的上下文设置引用格式

当模型通过工具检索上下文,并在答案中引用检索到的上下文时,请使用此模式。

定义可引用单元

您应根据用例所需的精确程度选择可引用单元。以下示例展示了几种可能的工具输出。

以下示例展示了几种推荐的工具输出格式。不同应用使用的底层工具可能不同,但最重要的是,输出应像这些示例一样采用清晰、稳定的结构。

编写提示指令

## Citations

Results are returned by "tool_1". Each message from `tool_1` is called a "source" and identified by its reference ID, which is the first occurrence of `turn\\d+file\\d+` (for example, `turn0file0` or `turn2file1`). In this example, the string `turn0file0` would be the source reference ID.

Citations are references to `tool_1` sources. Citations may be used to refer to either a single source or multiple sources.

A citation to a single source must be written as:
{CITATION_START}cite{CITATION_DELIMITER}turn\d+file\d+{CITATION_STOP}

If line-level citations are supported, a citation to a specific line range must be written as:
{CITATION_START}cite{CITATION_DELIMITER}turn\d+file\d+{CITATION_DELIMITER}L\d+-L\d+{CITATION_STOP}

Citations to multiple sources must be written by emitting multiple citation markers, one for each supporting source.

You must NOT write reference IDs like `turn0file0` verbatim in the response text without putting them between {CITATION_START}...{CITATION_STOP}.

- Place citations at the end of the supported sentence, or inline if the sentence is long and contains multiple supported clauses.
- Citations must be placed after punctuation.
- Cite only retrieved sources that directly support the cited text.
- Never invent source IDs, line ranges, or block locators that were not returned by the tool.
- If multiple retrieved sources materially support a proposition, cite all of them.
- If the retrieved sources disagree, cite the conflicting sources and describe the disagreement accurately.

输出示例:

The on-call handoff process is documented in the weekly support sync notes. \ue200cite\ue202turn0file0\ue202L8-L13\ue201

为注入的上下文设置引用格式

当您提前检索或准备好上下文,并将其直接注入提示时,请使用此模式。

定义可引用单元

对于注入的上下文,一种常见做法是使用带有稳定引用 ID 的显式标签包裹来源片段。

<BLOCK id="block1">
The service agreement states that termination for convenience requires thirty (30) days’ written notice, unless superseded by a customer-specific addendum.
In practice, renewal terms auto-extend for successive one-year periods when no written non-renewal notice is received before the deadline.
Appendix B further clarifies that pricing exceptions must be approved in writing by both Finance and the account owner.
</BLOCK>

<BLOCK id="block2">
Syllabus
</BLOCK>
...

这样可以明确可引用单元,方便模型引用。

编写提示指令

## Citations

Supporting context is provided directly in the prompt as citable units. Each citable unit is identified by the value of its `id` attribute in the first occurrence of a tag such as `<BLOCK id="block5"> ... </BLOCK>`. In this example, `block5` would be the source reference ID.

Because this pattern does not invoke tools, there is no tool turn counter to increment. That means you do not need to use a `turn#` prefix for the citation marker. You can keep IDs in a `turn0block5` style if that matches the rest of your system, or use plain IDs like `block5` as shown here. The key requirement is that the citation marker matches the injected context ID exactly and consistently.

Citations are references to these provided citable units. Citations may be used to refer to either a single source or multiple sources.

A citation to a single source must be written as:
{CITATION_START}cite{CITATION_DELIMITER}<block_id>{CITATION_STOP}

For example:
{CITATION_START}cite{CITATION_DELIMITER}block5{CITATION_STOP}

Citations to multiple sources must be written by emitting multiple citation markers, one for each supporting block.

You must NOT write block IDs verbatim in the response text without putting them between {CITATION_START}...{CITATION_STOP}.

- Place citations at the end of the supported sentence, or inline if the sentence is long and contains multiple supported clauses.
- Citations must be placed after punctuation.
- Cite only blocks that appear in the provided context.
- Never invent new block IDs.
- Never cite outside knowledge or outside authorities.
- If multiple blocks materially support a proposition, cite all of them.
- If the provided blocks conflict, cite the conflicting blocks and describe the conflict accurately.

输出示例:

The Court held that the District Court lacked personal jurisdiction over the petitioner. \ue200cite\ue202block5\ue201

注意:网页搜索等由 OpenAI 托管的工具会 自动提供行内引用。如果您想改用托管工具,请参阅 工具概览网页搜索指南文件搜索指南