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
  • 易讀的文字:格式清楚的來源資料。
  • 中繼資料(選用):網址、時間戳記、標題及類似的背景資訊。

來源 ID 與定位資訊的差異:來源 ID 是由模型產生的穩定識別碼, 例如 block1。定位資訊則是 介面中精確標示的醒目提示範圍,例如 lines L8-L13Paragraph 21。一般而言,模型應輸出來源 ID, 由系統負責解析或呈現定位資訊。過早混用這兩者, 往往會增加格式錯誤。

定義引用格式

你需要定義模型要產生的引用格式。請使用明確、一致,且模型容易穩定重現的格式。

以下是我們建議的引用格式與標記。我們強烈建議使用這些引用標記,因為它們與模型訓練時使用的標記非常接近。若選擇不同的標記值,請盡量維持相似的整體引用格式。

組成部分作用建議值
CITATION_START標示引用標記的開頭。\ue200
引用類別識別引用類型。所有支援的來源都使用 citecite
CITATION_DELIMITER分隔標記內的欄位。\ue202
來源 ID識別被引用的單位。turn# 是回合編號。item# 是特定檔案、區塊或網址。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 託管的工具(例如網頁搜尋)會 自動提供行內引用。如果你想改用託管工具,請參閱 工具概覽網頁搜尋指南檔案搜尋指南