Claudeは、ドキュメントに関する質問に回答する際に詳細な引用を提供できるため、各応答の背後にあるソースを追跡し、検証するのに役立ちます。
すべてのアクティブなモデルが引用をサポートしています。
次の例は、Messages APIでプレーンテキストドキュメントに対して引用を有効にする方法を示しています。
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5",
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に引用を統合します。
ドキュメントを提供し、引用を有効にする
ドキュメントが処理される
Claudeが引用付きの応答を提供する
sourceコンテンツ内にあるテキストは引用可能です。titleとcontextはオプションのフィールドで、モデルに渡されますが、引用コンテンツには使用されません。titleは長さに制限があるため、contextフィールドはドキュメントのメタデータをテキストまたは文字列化されたJSONとして保存するのに便利です。contentリストから0始まりで、終了インデックスは排他的です。cited_textフィールドは利便性のために提供されており、出力トークンにはカウントされません。cited_textは入力トークンにカウントされません。引用は、プロンプトキャッシング、トークンカウント、バッチ処理などの他のAPI機能と組み合わせて使用できます。
引用とプロンプトキャッシングは効果的に併用できます。
応答で生成された引用ブロックは直接キャッシュできませんが、それらが参照するソースドキュメントはキャッシュできます。パフォーマンスを最適化するには、トップレベルのドキュメントコンテンツブロックにcache_controlを適用します。
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-5",
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)この例では:
cache_controlを使用してドキュメントコンテンツがキャッシュされます。引用では3つのドキュメントタイプがサポートされています。ドキュメントはメッセージ内で直接提供する(base64、テキスト、またはURL)か、Files APIを通じてアップロードしてfile_idで参照できます。
| タイプ | 最適な用途 | チャンク化 | 引用形式 |
|---|---|---|---|
| プレーンテキスト | シンプルなテキストドキュメント、散文 | 文単位 | 文字インデックス(0始まり) |
| テキストコンテンツを含むPDFファイル | 文単位 | ページ番号(1始まり) | |
| カスタムコンテンツ | リスト、トランスクリプト、特殊なフォーマット、より細かい引用 | 追加のチャンク化なし | ブロックインデックス(0始まり) |
プレーンテキストドキュメントは自動的に文単位にチャンク化されます。インラインで提供するか、file_idで参照して提供できます。
このページの冒頭にある導入例は、すべてのSDKでの完全なプレーンテキストリクエストを示しています。ドキュメントブロックはtextソースを使用します。
{
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": "Plain text content..."
},
"title": "Document Title",
"context": "Context about the document that will not be cited from",
"citations": { "enabled": true }
}PDFドキュメントは、base64エンコードされたデータ、URL、またはfile_idとして提供できます。PDFテキストは抽出され、文単位にチャンク化されます。画像の引用はまだサポートされていないため、ドキュメントのスキャンであり抽出可能なテキストを含まないPDFは引用できません。
client = anthropic.Anthropic()
pdf_base64 = base64.standard_b64encode(
pathlib.Path("/path/to/document.pdf").read_bytes()
).decode()
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_base64,
},
"title": "Document Title",
"context": "Context about the document that will not be cited from",
"citations": {"enabled": True},
},
{"type": "text", "text": "Summarize this document."},
],
}
],
)
print(response)カスタムコンテンツドキュメントを使用すると、引用の粒度を制御できます。追加のチャンク化は行われず、提供されたコンテンツブロックに従ってチャンクがモデルに提供されます。
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "content",
"content": [
{"type": "text", "text": "First chunk"},
{"type": "text", "text": "Second chunk"},
],
},
"title": "Document Title",
"context": "Context about the document that will not be cited from",
"citations": {"enabled": True},
},
{"type": "text", "text": "Summarize this document."},
],
}
],
)
print(response)引用が有効になっている場合、応答には引用付きの複数のテキストブロックが含まれます。
{
"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,
}
],
},
]
}ストリーミング応答の場合、引用はcontent_block_deltaイベント内のcitations_deltaデルタタイプとして到着します。各デルタには、現在のtextコンテンツブロックのcitationsリストに追加する単一の引用が含まれます。
テキストデルタと並行してcitations_deltaデルタタイプを処理し、引用付きの応答をストリーミング中にレンダリングします。
RAGパイプラインからの検索結果を、組み込みの引用サポートを備えたファーストクラスのコンテンツブロックとして渡します。
ClaudeがPDFからテキストを抽出する方法と、ページベースの引用がソースファイルにどのようにマッピングされるかを学びます。
ドキュメントを一度アップロードし、複数の引用リクエストでfile_idによって参照します。
| Supported platforms |
|
|---|
Was this page helpful?