Skip to content

元数据提取#

简介#

在许多情况下,特别是对于长篇文档,一段文本可能缺乏必要的上下文来消除与其他类似文本的歧义。

为了解决这个问题,我们使用LLM(Large Language Models)来提取与文档相关的某些上下文信息,以更好地帮助检索和语言模型消除外观相似的段落。

我们在示例笔记本中展示了这一点,并展示了它在处理长篇文档中的有效性。

用法#

首先,我们定义一个元数据提取器,它接受一个要按顺序处理的特征提取器列表。

然后,我们将其提供给节点解析器,该解析器将向每个节点添加额外的元数据。

from llama_index.core.node_parser import SentenceSplitter
from llama_index.core.extractors import (
    SummaryExtractor,
    QuestionsAnsweredExtractor,
    TitleExtractor,
    KeywordExtractor,
)
from llama_index.extractors.entity import EntityExtractor

transformations = [
    SentenceSplitter(),
    TitleExtractor(nodes=5),
    QuestionsAnsweredExtractor(questions=3),
    SummaryExtractor(summaries=["prev", "self"]),
    KeywordExtractor(keywords=10),
    EntityExtractor(prediction_threshold=0.5),
]

然后,我们可以在输入文档或节点上运行我们的转换:

from llama_index.core.ingestion import IngestionPipeline

pipeline = IngestionPipeline(transformations=transformations)

nodes = pipeline.run(documents=documents)

这是提取的元数据样本:

{'page_label': '2',
 'file_name': '10k-132.pdf',
 'document_title': 'Uber Technologies, Inc. 2019 Annual Report: Revolutionizing Mobility and Logistics Across 69 Countries and 111 Million MAPCs with $65 Billion in Gross Bookings',
 'questions_this_excerpt_can_answer': '\n\n1. Uber Technologies, Inc. 在多少个国家运营?\n2. Uber Technologies, Inc. 为多少个MAPC提供服务?\n3. 2019年 Uber Technologies, Inc. 产生了多少总票面金额?',
 'prev_section_summary': "\n\n2019年年度报告概述了过去一年对组织重要的关键主题和实体。其中包括财务表现、运营亮点、客户满意度、员工参与度和可持续发展倡议。它还概述了组织未来一年的战略目标和目标。",
 'section_summary': '\n本节讨论了一个全球科技平台,该平台服务于多个万亿美元的市场,其产品利用核心技术和基础设施。它使消费者和司机可以轻松获取乘车或工作。该平台通过拼车革新了个人出行方式,现在正在利用其平台重新定义庞大的餐饮外卖和物流行业。该平台的基础是其庞大的网络、领先的技术、卓越的运营和产品专业知识。',
 'excerpt_keywords': '\n拼车、出行、外卖、物流、网络、技术、运营卓越、产品专业知识、A点、B点'}

自定义提取器#

如果提供的提取器不符合您的需求,您也可以像下面这样定义一个自定义提取器:

from llama_index.core.extractors import BaseExtractor


class CustomExtractor(BaseExtractor):
    async def aextract(self, nodes) -> List[Dict]:
        metadata_list = [
            {
                "custom": node.metadata["document_title"]
                + "\n"
                + node.metadata["excerpt_keywords"]
            }
            for node in nodes
        ]
        return metadata_list

extractor.extract() 将在幕后自动调用 aextract(),以提供同步和异步入口点。

在更高级的示例中,它还可以利用 llm 从节点内容和现有元数据中提取特征。有关更多详细信息,请参阅提供的元数据提取器的源代码

模块#

下面将为各种元数据提取器提供指南和教程。