請造訪摘要 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.8 是此類需要高準確性使用案例的絕佳選擇。如果您的文件規模和數量龐大,以致成本開始成為考量因素,您也可以嘗試使用較小的模型,例如 Claude Haiku 4.5。
為了協助估算這些成本,以下是使用 Opus 和 Haiku 摘要 1,000 份轉租協議的成本比較:
內容規模
預估 token 數
Claude Opus 4.8 預估成本
Claude Haiku 4.5 預估成本
在開始摘要文件之前,您需要準備資料。這包括從 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])
# 移除頁碼
text = re.sub(r"\n\s*\d+\s*\n", "\n", text)
# 移除多餘的空白
text = re.sub(r"\s+", " ", text)
return text
# 從 GitHub 儲存庫建立完整的 URL
url = "https://raw.githubusercontent.com/anthropics/anthropic-cookbook/main/skills/summarization/data/Sample Sublease Agreement.pdf"
url = url.replace(" ", "%20")
# 將 PDF 檔案下載到記憶體中
response = requests.get(url)
# 從記憶體載入 PDF
pdf_file = BytesIO(response.content)
document_text = get_llm_text(pdf_file)
print(document_text[:50000])在此範例中,您首先下載摘要 cookbook 中使用的範例轉租協議 PDF。此協議來源於 sec.gov 網站上公開提供的轉租協議。
此範例使用 pypdf 函式庫擷取 PDF 的內容並將其轉換為文字。然後透過移除頁碼和多餘的空白來清理文字資料。
Claude 可以適應各種摘要風格。您可以變更提示的詳細內容,引導 Claude 更詳細或更簡潔、包含更多或更少的技術術語,或提供更高層次或更低層次的上下文摘要。
以下是如何建立提示的範例,以確保在分析轉租協議時產生的摘要遵循一致的結構:
# 初始化 Anthropic 用戶端
client = anthropic.Anthropic()
def summarize_document(
text, details_to_extract, model="claude-opus-4-8", max_tokens=1000
):
# 格式化要擷取的詳細資訊,以便放入提示的上下文中
details_to_extract_str = "\n".join(details_to_extract)
# 提示模型摘要轉租協議
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 呼叫:**具有大量 token 的長文件可能需要長達一分鐘的時間讓 Claude 產生摘要。對於大型文件集合,您可能希望平行傳送 API 呼叫給 Claude,以便在合理的時間範圍內完成摘要。請參閱 Anthropic 的速率限制以確定可以平行執行的最大 API 呼叫數量。
在複雜的情境中,除了標準的提示工程技術之外,考慮額外的策略來提升效能可能會有所幫助。以下是一些進階策略:
法律摘要通常涉及一次處理長文件或許多相關文件,以致超出 Claude 的「context window」(上下文視窗)。您可以使用一種稱為元摘要的分塊方法來處理此使用案例。此技術涉及將文件分解為較小、可管理的區塊,然後分別處理每個區塊。然後,您可以結合每個區塊的摘要,以建立整個文件的元摘要。
以下是如何執行元摘要的範例:
# 初始化 Anthropic 用戶端
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-8", max_tokens=1000
):
# 格式化要擷取的詳細資訊,以便放入提示的上下文中
details_to_extract_str = "\n".join(details_to_extract)
# 迭代處理各個區塊並逐一進行摘要
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 個字元的區塊來實現這一點。然後將個別摘要結合起來,並從這些區塊摘要中建立最終摘要。
請注意,對於範例 PDF 而言,summarize_long_document 函式並非絕對必要,因為整個文件可以放入 Claude 的上下文視窗中。然而,對於超出 Claude 上下文視窗的文件或一起摘要多個相關文件時,它就變得至關重要。無論如何,這種元摘要技術通常能在最終摘要中捕捉到先前單一摘要方法中遺漏的額外重要細節。
使用 LLM 搜尋文件集合通常涉及「retrieval-augmented generation」(檢索增強生成),即 RAG。然而,在涉及大型文件或精確資訊檢索至關重要的情境中,基本的 RAG 方法可能不足。摘要索引文件是一種進階的 RAG 方法,提供更有效率的方式來對文件進行檢索排名,使用的上下文比傳統 RAG 方法更少。在此方法中,您首先使用 Claude 為語料庫中的每個文件產生簡潔的摘要,然後使用 Claude 對每個摘要與所提問查詢的相關性進行排名。有關此方法的更多詳細資訊,包括基於程式碼的範例,請查看摘要 cookbook 中的摘要索引文件章節。
另一種提升 Claude 產生摘要能力的進階技術是「fine-tuning」(微調)。微調涉及在專門符合您法律摘要需求的自訂資料集上訓練 Claude,確保 Claude 適應您的使用案例。以下是如何執行微調的概述:
**識別錯誤:**首先收集 Claude 摘要不足的實例——這可能包括遺漏關鍵法律細節、誤解上下文或使用不適當的法律術語。
**整理資料集:**一旦您識別出這些問題,請編譯這些有問題範例的資料集。此資料集應包含原始法律文件以及您修正後的摘要,確保 Claude 學習所需的行為。
**執行微調:**微調涉及在您整理的資料集上重新訓練模型,以調整其權重和參數。這種重新訓練有助於 Claude 更好地理解您法律領域的特定需求,提升其根據您的標準摘要文件的能力。
**迭代改進:**微調不是一次性的過程。隨著 Claude 持續產生摘要,您可以迭代地新增其表現不佳的新範例,進一步完善其能力。隨著時間的推移,這種持續的回饋循環將產生一個高度專門化於您法律摘要任務的模型。
Was this page helpful?