Was this page helpful?
访问我们的摘要 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 估计成本
在开始总结文件之前,您需要准备数据。这包括从 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 继续生成摘要,您可以迭代地添加其表现不佳的新示例,进一步完善其能力。随着时间的推移,这种持续的反馈循环将产生一个高度专业化于您法律摘要任务的模型。
Claude Haiku 3 估计成本
查看一个完整实现的基于代码的示例,了解如何使用 Claude 总结合同。