mcpField
S
Summarization Engine

Summarization Engine

github.com/jmsktm文档处理10220 次安装40 周前更新
代码仓库

关于 Summarization Engine

Summarization Engine 是面向系统开发者的文档摘要技能,涵盖抽取式、生成式及混合方法,并提供长文档、多文档处理和准确性评估指导。

主要功能

  1. 01抽取式与生成式方法
  2. 02按需求权衡摘要方案
  3. 03长文档分块处理
  4. 04多文档内容综合
  5. 05事实一致性检查

适用场景

  1. 01设计文档摘要流程
  2. 02浓缩研究论文或新闻
  3. 03总结会议记录或法律文件

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/jmsktm/claude-settings --skill summarization-engine

安装后在 Claude Code 中输入/summarization-engine确认已加载。

详细介绍

摘要引擎

摘要引擎技能将指导你构建系统,将长文档浓缩为简洁、准确的摘要。从选取关键句子的抽取式方法,到生成新文本的抽象式方法,本技能涵盖了各种摘要技术。

有效的摘要需要理解哪些信息至关重要,在浓缩内容时保持准确,并根据不同的文档类型和摘要要求进行调整。本技能将帮助你选择合适的方法,并实现稳健的摘要处理流程。

无论你要总结新闻文章、研究论文、会议记录还是法律文档,本技能都能确保摘要准确、相关且实用。

核心工作流

工作流 1:选择摘要方法
  1. 分析需求:
    • 摘要长度(比例或固定长度)
    • 准确性要求(事实精确度)
    • 风格偏好(抽取式或抽象式)
    • 速度和规模限制
  2. 比较不同方法:
    方法准确性流畅度速度适用场景
    抽取式高不定快法律、医疗、精确度要求高的场景
    抽象式(小模型)中好快通用内容
    抽象式(大模型)高极佳慢对质量要求高的场景
    混合式高好中等需要兼顾多方面的场景
  3. 根据权衡结果进行选择:
    • 抽取式:准确性至关重要时
    • 抽象式:流畅度很重要时
    • 混合式:两者都很重要时
  4. 设计评估标准
工作流 2:实现抽取式摘要
  1. 为句子评分:
    from sklearn.feature_extraction.text import TfidfVectorizer
    import numpy as np
    
    def extractive_summarize(text, num_sentences=3):
        # Split into sentences
        sentences = sent_tokenize(text)
    
        # Score by TF-IDF importance
        vectorizer = TfidfVectorizer()
        tfidf_matrix = vectorizer.fit_transform(sentences)
    
        # Score each sentence by sum of TF-IDF scores
        scores = np.array(tfidf_matrix.sum(axis=1)).flatten()
    
        # Select top sentences maintaining order
        top_indices = np.argsort(scores)[-num_sentences:]
        top_indices = sorted(top_indices)  # Maintain original order
    
        summary_sentences = [sentences[i] for i in top_indices]
        return " ".join(summary_sentences)
    
  2. 添加其他评分因素:
    • 位置(首句通常很重要)
    • 命名实体密度
    • 与标题或小标题的相似度
  3. 去除冗余内容:
    • 跳过与已选句子过于相似的句子
  4. 后处理以提升连贯性
工作流 3:实现抽象式摘要
  1. 配置摘要模型:
    from transformers import pipeline
    
    class AbstractiveSummarizer:
        def __init__(self, model="facebook/bart-large-cnn"):
            self.summarizer = pipeline("summarization", model=model)
            self.max_input_length = 1024  # Model-specific limit
    
        def summarize(self, text, max_length=150, min_length=50):
            # Handle long documents
            if len(text.split()) > self.max_input_length:
                return self.summarize_long(text, max_length, min_length)
    
            result = self.summarizer(
                text,
                max_length=max_length,
                min_length=min_length,
                do_sample=False
            )
            return result[0]["summary_text"]
    
        def summarize_long(self, text, max_length, min_length):
            # Chunk and summarize iteratively
            chunks = self.chunk_text(text)
            summaries = [self.summarize(chunk) for chunk in chunks]
    
            # Combine and re-summarize if needed
            combined = " ".join(summaries)
            if len(combined.split()) > max_length:
                return self.summarize(combined, max_length, min_length)
            return combined
    
  2. 通过分块处理长文档
  3. 验证事实准确性
  4. 后处理格式

快速参考

操作命令/触发语
总结文本“总结这份文档”
选择方法“[文档类型]适合用哪种摘要方法?”
控制长度“用 [N] 个词/句子总结”
多文档总结“将这些文档一起总结”
评估摘要“检查摘要质量”
减少幻觉“提高摘要准确性”

最佳实践

  • 保留关键信息:摘要必须涵盖重要内容

    • 识别关键实体、事实和结论
    • 确认关键信息得到保留
    • 不要为了简短而牺牲准确性
  • 保持事实准确性:抽象式摘要可能产生幻觉

    • 对照来源核实生成的事实
    • 高风险领域使用抽取式方法
    • 考虑采用混合式方法
  • 处理长文档:大多数模型都有长度限制

    • 合理分块(按章节或段落)
    • 使用分层摘要
    • 在各分块之间保留上下文
  • 让风格符合用途:不同用途需要不同的摘要

    • 执行摘要:首先呈现关键结论
    • 摘要:提供均衡的概述
    • 项目符号:列出便于快速浏览的关键信息
    • 渐进式呈现:提供多个详细程度
  • 正确评估:ROUGE 分数无法说明全部情况

    • 手动检查事实准确性
    • 评估连贯性和可读性
    • 与人工摘要进行比较
  • 考虑多文档摘要:通常需要总结多个来源

    • 找出共同主题和差异
    • 妥善处理矛盾
    • 将信息归属到相应来源

高级技术

基于 LLM 的摘要

使用大模型生成高质量摘要:

def llm_summarize(text, style="executive", max_words=150):
    style_instructions = {
        "executive": "Focus on key conclusions, decisions, and action items.",
        "technical": "Preserve technical details and methodology.",
        "narrative": "Maintain the story arc and key events.",
        "bullet": "Format as bullet points with key facts."
    }

    prompt = f"""Summarize the following text in approximately {max_words} words.
{style_instructions.get(style, "")}

Important:
- Only include information from the source text
- Maintain accuracy of facts, numbers, and names
- Preserve the most important information

Text to summarize:
{text}

Summary:"""

    return llm.complete(prompt, max_tokens=max_words * 2)
分层摘要

迭代总结超长文档:

def hierarchical_summarize(document, target_length=500):
    """
    Summarize long documents through progressive compression.
    """
    # Level 1: Split into sections
    sections = split_into_sections(document)

    # Level 2: Summarize each section
    section_summaries = []
    for section in sections:
        summary = summarize(section, max_length=200)
        section_summaries.append({
            "title": section.title,
            "summary": summary
        })

    # Level 3: Combine section summaries
    combined = "\n\n".join([
        f"{s['title']}: {s['summary']}"
        for s in section_summaries
    ])

    # Level 4: Final summary if still too long
    if len(combined.split()) > target_length:
        return summarize(combined, max_length=target_length)

    return combined
多文档摘要

综合多个来源的信息:

def multi_document_summarize(documents, topic=None):
    """
    Summarize multiple documents into coherent summary.
    """
    # Step 1: Summarize each document
    doc_summaries = []
    for i, doc in enumerate(documents):
        summary = summarize(doc.text)
        doc_summaries.append({
            "source": doc.source,
            "summary": summary
        })

    # Step 2: Identify themes and differences
    prompt = f"""Given these summaries from different sources about {topic or "a topic"}:

{chr(10).join(f"Source {i+1} ({s['source']}): {s['summary']}" for i, s in enumerate(doc_summaries))}

Create a unified summary that:
1. Identifies common themes across sources
2. Notes any contradictions or different perspectives
3. Attributes key claims to their sources
4. Presents a balanced, comprehensive view

Unified summary:"""

    return llm.complete(prompt)
事实一致性检查

验证摘要是否存在幻觉:

def check_factual_consistency(source, summary):
    """
    Verify summary facts against source document.
    """
    # Extract claims from summary
    claims = extract_claims(summary)

    # Check each claim against source
    results = []
    for claim in claims:
        prompt = f"""Does the source document support this claim?

Source: {source}

Claim: {claim}

Answer:
- SUPPORTED: The claim is directly supported by the source
- NOT SUPPORTED: The claim is not in the source
- CONTRADICTED: The source contradicts this claim

Provide answer and brief explanation."""

        result = llm.complete(prompt)
        results.append({
            "claim": claim,
            "status": parse_status(result),
            "explanation": result
        })

    return {
        "claims_checked": len(results),
        "supported": sum(1 for r in results if r["status"] == "SUPPORTED"),
        "not_supported": sum(1 for r in results if r["status"] == "NOT SUPPORTED"),
        "contradicted": sum(1 for r in results if r["status"] == "CONTRADICTED"),
        "details": results
    }
面向查询的摘要

生成针对特定问题定制的摘要:

def query_focused_summarize(document, query):
    """
    Summarize document with focus on answering specific question.
    """
    prompt = f"""Summarize the following document, focusing specifically on information relevant to this question:

Question: {query}

Document:
{document}

Provide a summary that:
1. Directly addresses the question
2. Includes relevant supporting details from the document
3. Notes if the document doesn't fully answer the question
4. Stays focused on query-relevant information

Summary:"""

    return llm.complete(prompt)

应避免的常见陷阱

  • 不经事实核查就相信抽象式摘要
  • 未处理超出模型上下文限制的文档
  • 仅使用 ROUGE 分数作为评估指标
  • 分块时忽略文档结构
  • 生成不符合用户需求的摘要
  • 多文档摘要中未注明信息来源
  • 过度压缩,导致关键信息丢失
  • 认为摘要无需人工审核即可直接使用

评论

支持 Markdown