Loading...
    • 构建
    • 管理
    • 模型和定价
    • 客户端 SDK
    • API 参考
    Search...
    ⌘K
    第一步
    Claude 简介快速开始
    使用 Claude 构建
    功能概览使用 Messages APIClaude API 技能处理停止原因
    模型能力
    扩展思考自适应思考工作量任务预算(测试版)快速模式(测试版:研究预览)结构化输出引用流式消息批量处理搜索结果流式拒绝多语言支持嵌入
    工具
    概览工具使用原理网络搜索工具网络获取工具代码执行工具顾问工具内存工具Bash 工具计算机使用工具文本编辑器工具
    工具基础设施
    工具参考工具搜索程序化工具调用细粒度工具流式传输
    上下文管理
    上下文窗口压缩上下文编辑提示缓存Token 计数
    处理文件
    Files APIPDF 支持图像和视觉
    技能
    概览快速开始最佳实践企业技能API 中的技能
    MCP
    远程 MCP 服务器MCP 连接器
    提示工程
    概览提示最佳实践Console 提示工具
    测试和评估
    定义成功并构建评估在 Console 中使用评估工具降低延迟
    加强防护栏
    减少幻觉提高输出一致性缓解越狱减少提示泄露
    资源
    术语表
    概览工单路由客户支持代理内容审核法律总结
    发布说明
    Claude Platform
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    用例

    法律文件总结

    本指南介绍如何利用 Claude 的先进自然语言处理能力高效总结法律文件,提取关键信息并加快法律研究。使用 Claude,您可以简化合同审查、诉讼准备和监管工作的流程,节省时间并确保法律流程的准确性。

    访问总结烹饪书查看使用 Claude 的法律文件总结实现示例。

    使用 Claude 构建之前

    决定是否使用 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 总结法律文件

    选择合适的 Claude 模型

    在总结法律文件时,模型准确性极其重要。Claude Opus 4.7 是需要高准确性的此类用例的绝佳选择。如果您的文件大小和数量很大,以至于成本开始成为问题,您也可以尝试使用较小的模型,如 Claude Haiku 4.5。

    为了帮助估计这些成本,以下是使用 Sonnet 和 Haiku 总结 1,000 份转租协议的成本比较:

    • 内容大小

      • 协议数量:1,000
      • 每份协议的字符数:300,000
      • 总字符数:300M
    • 估计令牌

      • 输入令牌:86M(假设每 3.5 个字符 1 个令牌)
      • 每个总结的输出令牌:350
      • 总输出令牌:350,000
    • Claude Opus 4.7 估计成本

      • 输入令牌成本:86 MTok * $5.00/MTok = $430
      • 输出令牌成本:0.35 MTok * $25.00/MTok = $8.75
      • 总成本:$430.00 + $8.75 = $438.75
    • Claude Haiku 3 估计成本

      • 输入令牌成本:86 MTok * $0.25/MTok = $21.50
      • 输出令牌成本:0.35 MTok * $1.25/MTok = $0.44
      • 总成本:$21.50 + $0.44 = $21.96
    实际成本可能与这些估计不同。这些估计基于提示部分中突出显示的示例。

    将文件转换为 Claude 可以处理的格式

    在开始总结文件之前,您需要准备数据。这涉及从 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 更冗长或更简洁、包含更多或更少的技术术语,或提供更高或更低级别的上下文总结。

    以下是如何创建提示的示例,以确保在分析转租协议时生成的总结遵循一致的结构:

    Python
    # 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 标头中嵌套的每个详细信息的总结进行响应。

    由于代码在标签中输出总结的每个部分,每个部分可以轻松地作为后处理步骤解析出来。这种方法支持结构化总结,可以针对您的用例进行调整,以便每个总结遵循相同的模式。

    评估您的提示

    提示通常需要测试和优化才能投入生产。为了确定解决方案的就绪情况,请使用结合定量和定性方法的系统流程来评估总结的质量。基于您定义的成功标准创建强大的经验评估使您能够优化提示。以下是您可能希望在经验评估中包含的一些指标:

    部署您的提示

    以下是部署解决方案到生产时需要牢记的一些额外考虑事项。

    1. **确保无责任:**了解总结中错误的法律含义,这可能导致您的组织或客户的法律责任。提供免责声明或法律通知,澄清总结由 AI 生成,应由法律专业人士审查。

    2. **处理多种文件类型:**本指南讨论如何从 PDF 中提取文本。在现实中,文件可能采用多种格式(PDF、Word 文件、文本文件等)。确保您的数据提取管道可以转换您期望接收的所有文件格式。

    3. **并行化对 Claude 的 API 调用:**具有大量令牌的长文件可能需要长达一分钟的时间才能让 Claude 生成总结。对于大型文件集合,您可能希望并行向 Claude 发送 API 调用,以便可以在合理的时间范围内完成总结。请参阅 Anthropic 的速率限制以确定可以并行执行的最大 API 调用数。


    改进性能

    在复杂的场景中,除了标准提示工程技术之外,考虑其他策略来改进性能可能会有所帮助。以下是一些高级策略:

    执行元总结以总结长文件

    法律总结通常涉及处理长文件或一次处理许多相关文件,使您超过 Claude 的上下文窗口。您可以使用称为元总结的分块方法来处理此用例。此技术涉及将文件分解为较小的、可管理的块,然后分别处理每个块。然后,您可以组合每个块的总结以创建整个文件的元总结。

    以下是如何执行元总结的示例:

    Python
    # 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 适应您的用例。以下是如何执行微调的概述:

    1. **识别错误:**首先收集 Claude 的总结不足的实例 - 这可能包括遗漏关键法律细节、误解上下文或使用不适当的法律术语。

    2. **策划数据集:**一旦您识别了这些问题,请编译这些有问题示例的数据集。此数据集应包括原始法律文件以及您的更正总结,确保 Claude 学习所需的行为。

    3. **执行微调:**微调涉及在您的策划数据集上重新训练模型以调整其权重和参数。这种重新训练帮助 Claude 更好地理解您的法律领域的具体要求,改进其根据您的标准总结文件的能力。

    4. **迭代改进:**微调不是一次性过程。当 Claude 继续生成总结时,您可以迭代地添加它表现不佳的新示例,进一步完善其能力。随着时间的推移,这个持续的反馈循环将产生一个高度专门化的模型,用于您的法律总结任务。

    微调目前仅通过 Amazon Bedrock 提供。其他详情可在 AWS 发布博客中获得。
    总结烹饪书

    查看如何使用 Claude 总结合同的完整实现代码示例。

    引用烹饪书

    探索引用烹饪书配方,获取有关如何确保信息准确性和可解释性的指导。

    Was this page helpful?

    • 使用 Claude 构建之前
    • 决定是否使用 Claude 进行法律文件总结
    • 如何使用 Claude 总结法律文件
    • 选择合适的 Claude 模型
    • 将文件转换为 Claude 可以处理的格式
    • 微调 Claude 以从您的数据集学习