访问总结烹饪书查看使用 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.7 是需要高准确性的此类用例的绝佳选择。如果您的文件大小和数量很大,以至于成本开始成为问题,您也可以尝试使用较小的模型,如 Claude Haiku 4.5。
为了帮助估计这些成本,以下是使用 Sonnet 和 Haiku 总结 1,000 份转租协议的成本比较:
内容大小
估计令牌
Claude Opus 4.7 估计成本
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])在此示例中,您首先下载总结烹饪书中使用的示例转租协议的 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?