• Messages
  • Managed Agents
  • 管理

Search...
⌘K
第一步
Claude 簡介快速入門
使用 Claude 進行建構
功能概覽使用 Messages API停止原因與備援拒絕與備援備援額度
模型能力
擴展思考自適應思考Effort任務預算(測試版)快速模式(研究預覽)結構化輸出引用串流 Messages批次處理搜尋結果串流拒絕多語言支援嵌入
工具
概覽工具使用的運作方式教學:建構使用工具的代理定義工具處理工具呼叫平行工具使用Tool Runner (SDK)嚴格工具使用搭配提示快取的工具使用伺服器工具疑難排解網頁搜尋工具網頁擷取工具程式碼執行工具顧問工具記憶體工具Bash 工具電腦使用工具文字編輯器工具
工具基礎架構
工具參考管理工具上下文工具組合工具搜尋程式化工具呼叫細粒度工具串流
上下文管理
上下文視窗壓縮上下文編輯提示快取對話中系統訊息建構編排模式快取診斷(測試版)Token 計數
處理檔案
Files APIPDF 支援圖片與視覺
技能
概覽快速入門最佳實務企業技能API 中的技能
MCP
遠端 MCP 伺服器MCP 連接器
雲端平台上的 Claude
Amazon BedrockAmazon Bedrock(舊版)AWS 上的 Claude PlatformMicrosoft FoundryVertex AI

Log in
引用
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Solutions

  • AI agents
  • Code modernization
  • Coding
  • Customer support
  • Education
  • Financial services
  • Government
  • Life sciences

Partners

  • Claude on AWS
  • Google Cloud's Vertex AI

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Company

  • Anthropic
  • Careers
  • Economic Futures
  • Research
  • News
  • Responsible Scaling Policy
  • Security and compliance
  • Transparency

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
Messages/模型能力

引用



此功能符合「Zero Data Retention」(零資料保留),即 ZDR 的資格。當您的組織具有 ZDR 安排時,透過此功能傳送的資料在 API 回應返回後不會被儲存。

Claude 能夠在回答有關文件的問題時提供詳細的引用,幫助您追蹤和驗證回應中的資訊來源。

所有現行模型都支援引用功能,但 Haiku 3 除外。



請使用此表單分享您對引用功能的意見回饋和建議。

以下是如何在 Messages API 中使用引用的範例:

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "text",
                        "media_type": "text/plain",
                        "data": "The grass is green. The sky is blue.",
                    },
                    "title": "My Document",
                    "context": "This is a trustworthy document.",
                    "citations": {"enabled": True},
                },
                {"type": "text", "text": "What color is the grass and sky?"},
            ],
        }
    ],
)
print(response)


與基於提示的方法比較

與基於提示的引用解決方案相比,引用功能具有以下優勢:

  • 節省成本: 如果您基於提示的方法要求 Claude 輸出直接引述,您可能會看到成本節省,因為 cited_text 不會計入您的輸出 token。
  • 更好的引用可靠性: 由於引用會被解析為上述各自的回應格式,且 cited_text 是被提取出來的,因此引用保證包含指向所提供文件的有效指標。
  • 改善的引用品質: 在評估中,與純粹基於提示的方法相比,引用功能被發現明顯更有可能從文件中引用最相關的引述。

引用的運作方式

透過以下步驟將引用與 Claude 整合:

  1. 1

    提供文件並啟用引用

    • 以任何支援的格式包含文件:PDF、純文字或自訂內容文件
    • 在每個文件上設定 citations.enabled=true。目前,必須在請求中的所有文件上啟用引用,或全部不啟用。
    • 請注意,目前僅支援文字引用,尚不支援圖片引用。
  2. 2

    文件被處理

    • 文件內容會被「分塊」(chunked),以定義可能引用的最小粒度。例如,句子分塊將允許 Claude 引用單一句子,或將多個連續句子串連在一起以引用一個段落(或更長的內容)!
      • 對於 PDF: 文字會按照 PDF 支援中所述的方式提取,內容會被分塊為句子。目前不支援從 PDF 引用圖片。
      • 對於純文字文件: 內容會被分塊為可供引用的句子。
      • 對於自訂內容文件: 您提供的內容區塊會按原樣使用,不會進行進一步的分塊。
  3. 3

    Claude 提供帶引用的回應

    • 回應現在可能包含多個文字區塊,其中每個文字區塊可以包含 Claude 所做的陳述以及支援該陳述的引用列表。
    • 引用會參照來源文件中的特定位置。這些引用的格式取決於被引用文件的類型。


自動分塊與自訂內容

預設情況下,純文字和 PDF 文件會自動分塊為句子。如果您需要對引用粒度有更多控制(例如,用於項目符號或逐字稿),請改用自訂內容文件。請參閱文件類型以了解更多詳情。

例如,如果您希望 Claude 能夠從您的 RAG 區塊中引用特定句子,您應該將每個 RAG 區塊放入一個純文字文件中。否則,如果您不希望進行任何進一步的分塊,或者您想自訂任何額外的分塊,您可以將 RAG 區塊放入自訂內容文件中。

可引用與不可引用的內容

  • 在文件的 source 內容中找到的文字可以被引用。
  • title 和 context 是可選欄位,會傳遞給模型,但不會用於被引用的內容。
  • title 的長度有限制,因此您可能會發現 context 欄位對於以文字或字串化 JSON 形式儲存任何文件中繼資料很有用。

引用索引

  • 文件索引從請求中所有文件內容區塊的列表(跨所有訊息)從 0 開始索引。
  • 字元索引從 0 開始索引,結束索引為不包含(exclusive)。
  • 頁碼從 1 開始索引,結束頁碼為不包含。
  • 內容區塊索引從自訂內容文件中提供的 content 列表從 0 開始索引,結束索引為不包含。

Token 成本

  • 啟用引用會因系統提示的增加和文件分塊而導致輸入 token 略有增加。
  • 然而,引用功能在輸出 token 方面非常高效。在底層,模型以標準化格式輸出引用,然後將其解析為引用文字和文件位置索引。cited_text 欄位是為了方便而提供的,不計入輸出 token。
  • 當在後續對話輪次中傳回時,cited_text 也不計入輸入 token。

功能相容性

引用可與其他 API 功能結合使用,包括提示快取、token 計數和批次處理。



引用與結構化輸出不相容

引用無法與結構化輸出一起使用。如果您在任何使用者提供的文件(Document 區塊或 RequestSearchResultBlock)上啟用引用,同時也包含 output_config.format 參數(或已棄用的 output_format 參數),API 將回傳 400 錯誤。

這是因為引用需要將引用區塊與文字輸出交錯排列,這與結構化輸出的嚴格 JSON schema 限制不相容。

將提示快取與引用一起使用

引用和提示快取可以有效地一起使用。

回應中產生的引用區塊無法直接快取,但它們所參照的來源文件可以快取。為了最佳化效能,請將 cache_control 套用到您的頂層文件內容區塊。

在此範例中:

  • 文件內容使用文件區塊上的 cache_control 進行快取
  • 文件上啟用了引用
  • Claude 可以產生帶有引用的回應,同時受益於快取的文件內容
  • 使用相同文件的後續請求將受益於快取的內容

文件類型

選擇文件類型

引用支援三種文件類型。文件可以直接在訊息中提供(base64、文字或 URL),或透過 Files API 上傳並透過 file_id 參照:

類型最適合分塊引用格式
純文字簡單的文字文件、散文句子字元索引(從 0 開始索引)
PDF具有文字內容的 PDF 檔案句子頁碼(從 1 開始索引)
自訂內容列表、逐字稿、特殊格式、更細粒度的引用無額外分塊區塊索引(從 0 開始索引)


.csv、.xlsx、.docx、.md 和 .txt 檔案不支援作為文件區塊。請將這些轉換為純文字並直接包含在訊息內容中。請參閱處理其他檔案格式。

純文字文件

純文字文件會自動分塊為句子。您可以內嵌提供它們,或透過其 file_id 參照:

PDF 文件

PDF 文件可以作為 base64 編碼資料、URL 或透過 file_id 提供。PDF 文字會被提取並分塊為句子。由於尚不支援圖片引用,因此作為文件掃描且不包含可提取文字的 PDF 將無法被引用。

自訂內容文件

自訂內容文件讓您可以控制引用粒度。不會進行額外的分塊,區塊會根據提供的內容區塊提供給模型。

{
    "type": "document",
    "source": {
        "type": "content",
        "content": [
            {"type": "text", "text": "First chunk"},
            {"type": "text", "text": "Second chunk"},
        ],
    },
    "title": "Document Title",  # optional
    "context": "Context about the document that will not be cited from",  # optional
    "citations": {"enabled": True},
}


回應結構

當啟用引用時,回應會包含多個帶有引用的文字區塊:

{
    "content": [
        {"type": "text", "text": "According to the document, "},
        {
            "type": "text",
            "text": "the grass is green",
            "citations": [
                {
                    "type": "char_location",
                    "cited_text": "The grass is green.",
                    "document_index": 0,
                    "document_title": "Example Document",
                    "start_char_index": 0,
                    "end_char_index": 20,
                }
            ],
        },
        {"type": "text", "text": " and "},
        {
            "type": "text",
            "text": "the sky is blue",
            "citations": [
                {
                    "type": "char_location",
                    "cited_text": "The sky is blue.",
                    "document_index": 0,
                    "document_title": "Example Document",
                    "start_char_index": 20,
                    "end_char_index": 36,
                }
            ],
        },
        {
            "type": "text",
            "text": ". Information from page 5 states that ",
        },
        {
            "type": "text",
            "text": "water is essential",
            "citations": [
                {
                    "type": "page_location",
                    "cited_text": "Water is essential for life.",
                    "document_index": 1,
                    "document_title": "PDF Document",
                    "start_page_number": 5,
                    "end_page_number": 6,
                }
            ],
        },
        {
            "type": "text",
            "text": ". The custom document mentions ",
        },
        {
            "type": "text",
            "text": "important findings",
            "citations": [
                {
                    "type": "content_block_location",
                    "cited_text": "These are important findings.",
                    "document_index": 2,
                    "document_title": "Custom Content Document",
                    "start_block_index": 0,
                    "end_block_index": 1,
                }
            ],
        },
    ]
}

串流支援

對於串流回應,會包含一個 citations_delta 類型,其中包含要新增到目前 text 內容區塊上的 citations 列表的單一引用。

Was this page helpful?

  • 引用的運作方式
  • 可引用與不可引用的內容
  • 引用索引
  • Token 成本
  • 功能相容性
  • 文件類型
  • 選擇文件類型
  • 純文字文件
  • PDF 文件
  • 自訂內容文件
  • 回應結構
  • 串流支援
  • 對於 PDF: 引用包含頁碼範圍(從 1 開始索引)。
  • 對於純文字文件: 引用包含字元索引範圍(從 0 開始索引)。
  • 對於自訂內容文件: 引用包含內容區塊索引範圍(從 0 開始索引),對應於提供的原始內容列表。
  • 文件索引用於指示參考來源,並根據您原始請求中所有文件的列表從 0 開始索引。
  • client = anthropic.Anthropic()
    
    # 長篇文件內容(例如:技術文件)
    long_document = (
        "This is a very long document with thousands of words..." + " ... " * 1000
    )  # Minimum cacheable length
    
    response = client.messages.create(
        model="claude-opus-4-8",
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "document",
                        "source": {
                            "type": "text",
                            "media_type": "text/plain",
                            "data": long_document,
                        },
                        "citations": {"enabled": True},
                        "cache_control": {
                            "type": "ephemeral"
                        },  # Cache the document content
                    },
                    {
                        "type": "text",
                        "text": "What does this document say about API features?",
                    },
                ],
            }
        ],
    )
    print(response)