請造訪我們的摘要 cookbook,查看使用 Claude 實作法律摘要的範例。
以下是一些關鍵指標,說明您應該使用像 Claude 這樣的 LLM 來摘要法律文件:
對於任何給定的文件,沒有單一正確的摘要。如果沒有明確的指示,Claude 可能難以確定應包含哪些細節。為了達到最佳結果,請確定您希望在摘要中包含的具體資訊。
例如,在摘要轉租協議時,您可能希望提取以下要點:
details_to_extract = [
'Parties involved (sublessor, sublessee, and original lessor)',
'Property details (address, description, and permitted use)',
'Term and rent (start date, end date, monthly rent, and security deposit)',
'Responsibilities (utilities, maintenance, and repairs)',
'Consent and notices (landlord\'s consent, and notice requirements)',
'Special provisions (furniture, parking, and subletting restrictions)'
]評估摘要品質是一項眾所周知的困難任務。與許多其他自然語言處理任務不同,摘要的評估通常缺乏明確、客觀的指標。這個過程可能非常主觀,不同的讀者重視摘要的不同面向。以下是您在評估 Claude 執行法律摘要效果時可能希望考慮的標準。
請參閱我們關於建立成功標準的指南以獲取更多資訊。
在摘要法律文件時,模型準確性極為重要。Claude Opus 4.6 是此類需要高準確性的使用案例的絕佳選擇。如果您的文件大小和數量很大,以至於成本開始成為問題,您也可以嘗試使用較小的模型,如 Claude Haiku 4.5。
為了幫助估算這些成本,以下是使用 Sonnet 和 Haiku 摘要 1,000 份轉租協議的成本比較:
內容大小
預估 token 數
Claude Opus 4.6 預估成本
Claude Haiku 3 預估成本
在開始摘要文件之前,您需要準備資料。這包括從 PDF 中提取文字、清理文字,並確保它已準備好由 Claude 處理。
以下是在範例 PDF 上演示此過程:
from io import BytesIO
import re
import pypdf
import requests
def get_llm_text(pdf_file):
reader = pypdf.PdfReader(pdf_file)
text = "\n".join([page.extract_text() for page in reader.pages])
# Remove extra whitespace
text = re.sub(r'\s+', ' ', text)
# Remove page numbers
text = re.sub(r'\n\s*\d+\s*\n', '\n', text)
return text
# Create the full URL from the GitHub repository
url = "https://raw.githubusercontent.com/anthropics/anthropic-cookbook/main/skills/summarization/data/Sample Sublease Agreement.pdf"
url = url.replace(" ", "%20")
# Download the PDF file into memory
response = requests.get(url)
# Load the PDF from memory
pdf_file = BytesIO(response.content)
document_text = get_llm_text(pdf_file)
print(document_text[:50000]) 在此範例中,我們首先下載摘要 cookbook 中使用的範例轉租協議 PDF。此協議來源於 sec.gov 網站上公開可用的轉租協議。
我們使用 pypdf 函式庫提取 PDF 的內容並將其轉換為文字。然後透過移除多餘的空白和頁碼來清理文字資料。
Claude 可以適應各種摘要風格。您可以更改提示的細節,引導 Claude 更詳細或更簡潔、包含更多或更少的技術術語,或提供更高層次或更低層次的上下文摘要。
以下是如何建立一個提示的範例,確保在分析轉租協議時生成的摘要遵循一致的結構:
import anthropic
# Initialize the Anthropic client
client = anthropic.Anthropic()
def summarize_document(text, details_to_extract, model="claude-opus-4-6", max_tokens=1000):
# Format the details to extract to be placed within the prompt's context
details_to_extract_str = '\n'.join(details_to_extract)
# Prompt the model to summarize the sublease agreement
prompt = f"""Summarize the following sublease agreement. Focus on these key aspects:
{details_to_extract_str}
Provide the summary in bullet points nested within the XML header for each section. For example:
<parties involved>
- Sublessor: [Name]
// Add more details as needed
</parties involved>
If any information is not explicitly stated in the document, note it as "Not specified". Do not preamble.
Sublease agreement text:
{text}
"""
response = client.messages.create(
model=model,
max_tokens=max_tokens,
system="You are a legal analyst specializing in real estate law, known for highly accurate and detailed summaries of sublease agreements.",
messages=[
{"role": "user", "content": prompt},
{"role": "assistant", "content": "Here is the summary of the sublease agreement: <summary>"}
],
stop_sequences=["</summary>"]
)
return response.content[0].text
sublease_summary = summarize_document(document_text, details_to_extract)
print(sublease_summary)此程式碼實作了一個 summarize_document 函式,使用 Claude 來摘要轉租協議的內容。該函式接受一個文字字串和一個要提取的細節列表作為輸入。在此範例中,我們使用前面程式碼片段中定義的 document_text 和 details_to_extract 變數來呼叫該函式。
在函式內部,為 Claude 生成一個提示,包括要摘要的文件、要提取的細節,以及摘要文件的具體指示。提示指示 Claude 以嵌套在 XML 標頭內的每個細節摘要來回應。
因為我們決定將摘要的每個部分輸出在標籤內,每個部分都可以在後處理步驟中輕鬆解析出來。這種方法實現了結構化摘要,可以根據您的使用案例進行調整,使每份摘要遵循相同的模式。
提示通常需要測試和最佳化才能達到生產就緒狀態。為了確定您的解決方案的就緒程度,請使用結合定量和定性方法的系統化流程來評估摘要的品質。根據您定義的成功標準建立強大的實證評估將使您能夠最佳化提示。以下是您可能希望在實證評估中包含的一些指標:
以下是在將解決方案部署到生產環境時需要記住的一些額外考量。
確保無責任風險: 了解摘要中錯誤的法律影響,這可能導致您的組織或客戶承擔法律責任。提供免責聲明或法律通知,說明摘要由 AI 生成,應由法律專業人員審查。
處理多樣化的文件類型: 在本指南中,我們討論了如何從 PDF 中提取文字。在現實世界中,文件可能以各種格式出現(PDF、Word 文件、文字檔案等)。確保您的資料提取管道可以轉換您預期接收的所有檔案格式。
並行化對 Claude 的 API 呼叫: 具有大量 token 的長文件可能需要長達一分鐘的時間讓 Claude 生成摘要。對於大型文件集合,您可能希望並行發送 API 呼叫給 Claude,以便在合理的時間範圍內完成摘要。請參閱 Anthropic 的速率限制以確定可以並行執行的最大 API 呼叫數量。
在複雜的場景中,除了標準的提示工程技術之外,考慮額外的策略來提升效能可能會有所幫助。以下是一些進階策略:
法律摘要通常涉及處理長文件或一次處理許多相關文件,以至於超過 Claude 的上下文視窗。您可以使用一種稱為元摘要的分塊方法來處理此使用案例。此技術涉及將文件分解為更小、可管理的區塊,然後分別處理每個區塊。然後您可以合併每個區塊的摘要,以建立整個文件的元摘要。
以下是如何執行元摘要的範例:
import anthropic
# Initialize the Anthropic client
client = anthropic.Anthropic()
def chunk_text(text, chunk_size=20000):
return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
def summarize_long_document(text, details_to_extract, model="claude-opus-4-6", max_tokens=1000):
# Format the details to extract to be placed within the prompt's context
details_to_extract_str = '\n'.join(details_to_extract)
# Iterate over chunks and summarize each one
chunk_summaries = [summarize_document(chunk, details_to_extract, model=model, max_tokens=max_tokens) for chunk in chunk_text(text)]
final_summary_prompt = f"""
You are looking at the chunked summaries of multiple documents that are all related.
Combine the following summaries of the document from different truthful sources into a coherent overall summary:
<chunked_summaries>
{"".join(chunk_summaries)}
</chunked_summaries>
Focus on these key aspects:
{details_to_extract_str})
Provide the summary in bullet points nested within the XML header for each section. For example:
<parties involved>
- Sublessor: [Name]
// Add more details as needed
</parties involved>
If any information is not explicitly stated in the document, note it as "Not specified". Do not preamble.
"""
response = client.messages.create(
model=model,
max_tokens=max_tokens,
system="You are a legal expert that summarizes notes on one document.",
messages=[
{"role": "user", "content": final_summary_prompt},
{"role": "assistant", "content": "Here is the summary of the sublease agreement: <summary>"}
],
stop_sequences=["</summary>"]
)
return response.content[0].text
long_summary = summarize_long_document(document_text, details_to_extract)
print(long_summary)summarize_long_document 函式建立在先前的 summarize_document 函式之上,將文件分割成較小的區塊並分別摘要每個區塊。
程式碼透過對原始文件中每個 20,000 字元的區塊應用 summarize_document 函式來實現這一點。然後合併各個摘要,並從這些區塊摘要中建立最終摘要。
請注意,summarize_long_document 函式對於我們的範例 PDF 並非嚴格必要,因為整個文件都在 Claude 的上下文視窗內。然而,對於超過 Claude 上下文視窗的文件或同時摘要多個相關文件時,它變得不可或缺。無論如何,這種元摘要技術通常能在最終摘要中捕捉到先前單一摘要方法中遺漏的額外重要細節。
使用 LLM 搜尋文件集合通常涉及檢索增強生成(RAG)。然而,在涉及大型文件或需要精確資訊檢索的場景中,基本的 RAG 方法可能不夠充分。摘要索引文件是一種進階 RAG 方法,提供了一種更有效的文件排名檢索方式,使用的上下文比傳統 RAG 方法更少。在此方法中,您首先使用 Claude 為語料庫中的每個文件生成簡潔的摘要,然後使用 Claude 對每個摘要與所提問查詢的相關性進行排名。有關此方法的更多詳細資訊,包括基於程式碼的範例,請查看摘要 cookbook 中的摘要索引文件部分。
另一種提升 Claude 生成摘要能力的進階技術是微調。微調涉及在專門符合您法律摘要需求的自訂資料集上訓練 Claude,確保 Claude 適應您的使用案例。以下是如何執行微調的概述:
識別錯誤: 首先收集 Claude 摘要不足的實例——這可能包括遺漏關鍵法律細節、誤解上下文或使用不當的法律術語。
策劃資料集: 一旦您識別了這些問題,編譯這些有問題範例的資料集。此資料集應包含原始法律文件以及您修正後的摘要,確保 Claude 學習到期望的行為。
執行微調: 微調涉及在您策劃的資料集上重新訓練模型以調整其權重和參數。這種重新訓練幫助 Claude 更好地理解您法律領域的特定需求,提升其根據您的標準摘要文件的能力。
迭代改進: 微調不是一次性的過程。隨著 Claude 繼續生成摘要,您可以迭代地添加其表現不佳的新範例,進一步完善其能力。隨著時間的推移,這種持續的回饋循環將產生一個高度專門化於您法律摘要任務的模型。
Was this page helpful?