訪問摘要烹飪書以查看使用 Claude 的法律文件摘要實現範例。
以下是一些關鍵指標,表明您應該使用 Claude 等 LLM 來摘要法律文件:
任何給定文件都沒有單一正確的摘要。沒有明確的指導,Claude 可能難以確定要包含哪些詳細資訊。為了獲得最佳結果,請確定您想在摘要中包含的具體資訊。
例如,在摘要轉租協議時,您可能希望提取以下關鍵要點:
details_to_extract = [
"涉及方(轉租人、承租人和原始出租人)",
"物業詳情(地址、描述和允許用途)",
"期限和租金(開始日期、結束日期、月租金和押金)",
"責任(公用事業、維護和修繕)",
"同意和通知(房東同意和通知要求)",
"特殊條款(家具、停車和轉租限制)",
]評估摘要品質是一項臭名昭著的困難任務。與許多其他自然語言處理任務不同,摘要評估通常缺乏明確的客觀指標。該過程可能高度主觀,不同的讀者重視摘要的不同方面。以下是您在評估 Claude 執行法律文件摘要的效果時可能希望考慮的標準。
有關更多資訊,請參閱建立成功標準的指南。
在摘要法律文件時,模型準確性極其重要。Claude Opus 4.7 是需要高準確性的此類用例的絕佳選擇。如果您的文件大小和數量很大,以至於成本開始成為問題,您也可以嘗試使用較小的模型,如 Claude Haiku 4.5。
為了幫助估計這些成本,以下是使用 Sonnet 和 Haiku 摘要 1,000 份轉租協議的成本比較:
內容大小
估計令牌
Claude Opus 4.7 估計成本
在開始摘要文件之前,您需要準備數據。這涉及從 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])在此範例中,您首先下載摘要烹飪書中使用的示例轉租協議的 pdf。此協議來自sec.gov 網站上公開提供的轉租協議。
該範例使用 pypdf 庫來提取 pdf 的內容並將其轉換為文本。然後通過移除額外的空白和頁碼來清理文本數據。
Claude 可以適應各種摘要風格。您可以更改提示的詳細資訊,以指導 Claude 更冗長或更簡潔、包含更多或更少的技術術語,或提供更高或更低層次的上下文摘要。
以下是如何建立提示的範例,以確保在分析轉租協議時生成的摘要遵循一致的結構:
# Initialize the Anthropic client
client = anthropic.Anthropic()
def summarize_document(
text, details_to_extract, model="claude-opus-4-7", 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},
],
)
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 調用:具有大量令牌的長文件可能需要長達一分鐘的時間才能讓 Claude 生成摘要。對於大型文件集合,您可能希望並行向 Claude 發送 API 調用,以便摘要可以在合理的時間範圍內完成。請參閱 Anthropic 的速率限制以確定可以並行執行的最大 API 調用數量。
在複雜的場景中,除了標準提示工程技術外,考慮其他策略來改進效能可能會有所幫助。以下是一些進階策略:
法律文件摘要通常涉及處理長文件或多個相關文件,使得您超過 Claude 的上下文窗口。您可以使用稱為元摘要的分塊方法來處理此用例。此技術涉及將文件分解為較小的、可管理的塊,然後分別處理每個塊。然後,您可以組合每個塊的摘要以建立整個文件的元摘要。
以下是如何執行元摘要的範例:
# 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-7", 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},
],
)
return response.content[0].text
long_summary = summarize_long_document(document_text, details_to_extract)
print(long_summary)summarize_long_document 函數通過將文件分成較小的塊並分別摘要每個塊來建立在早期 summarize_document 函數的基礎上。
代碼通過將 summarize_document 函數應用於原始文件內 20,000 個字符的每個塊來實現此目的。然後組合各個摘要,並從這些塊摘要建立最終摘要。
請注意,summarize_long_document 函數對於示例 pdf 並非嚴格必要的,因為整個文件適合 Claude 的上下文窗口。但是,對於超過 Claude 上下文窗口的文件或在一起摘要多個相關文件時,它變得至關重要。無論如何,此元摘要技術通常在最終摘要中捕捉到在早期單摘要方法中遺漏的其他重要詳細資訊。
使用 LLM 搜索文件集合通常涉及檢索增強生成 (RAG)。但是,在涉及大型文件或當精確資訊檢索至關重要時,基本 RAG 方法可能不夠。摘要索引文件是一種進階 RAG 方法,提供了一種更有效的方式來對文件進行排名以進行檢索,使用的上下文比傳統 RAG 方法少。在此方法中,您首先使用 Claude 為語料庫中的每個文件生成簡潔的摘要,然後使用 Claude 對每個摘要與所提出的查詢的相關性進行排名。有關此方法的進一步詳細資訊,包括基於代碼的範例,請查看摘要烹飪書中的摘要索引文件部分。
改進 Claude 生成摘要能力的另一種進階技術是微調。微調涉及在專門符合您的法律摘要需求的自訂數據集上訓練 Claude,確保 Claude 適應您的用例。以下是如何執行微調的概述:
識別錯誤:首先收集 Claude 的摘要不足的實例 - 這可能包括遺漏關鍵法律詳細資訊、誤解上下文或使用不適當的法律術語。
策劃數據集:一旦您識別了這些問題,請編譯這些有問題範例的數據集。此數據集應包括原始法律文件以及您的更正摘要,確保 Claude 學習所需的行為。
執行微調:微調涉及在您的策劃數據集上重新訓練模型以調整其權重和參數。此重新訓練幫助 Claude 更好地理解您的法律領域的具體要求,改進其根據您的標準摘要文件的能力。
迭代改進:微調不是一次性過程。當 Claude 繼續生成摘要時,您可以迭代地添加它表現不佳的新範例,進一步完善其功能。隨著時間的推移,此持續反饋循環將導致一個高度專門化的模型,用於您的法律摘要任務。
Was this page helpful?
Claude Haiku 3 估計成本
探索引用烹飪書食譜,以獲取有關如何確保資訊準確性和可解釋性的指導。