元数据提取使用模式(Metadata Extraction Usage Pattern)
概念解释
你可以使用 LLM(大型语言模型)来自动提取元数据,通过我们的元数据提取器模块实现。
我们的元数据提取器模块包括以下“特征提取器”:
- SummaryExtractor - 自动提取一组节点的摘要
- QuestionsAnsweredExtractor - 提取每个节点可以回答的一组问题
- TitleExtractor - 提取每个节点的标题
- EntityExtractor - 提取每个节点内容中提到的实体(例如地点、人物、事物的名称)
然后,你可以将元数据提取器与我们的节点解析器链接起来:
使用模式
定义元数据提取器
from llama_index.core.extractors import (
TitleExtractor,
QuestionsAnsweredExtractor,
)
from llama_index.core.node_parser import TokenTextSplitter
text_splitter = TokenTextSplitter(
separator=" ", chunk_size=512, chunk_overlap=128
)
title_extractor = TitleExtractor(nodes=5)
qa_extractor = QuestionsAnsweredExtractor(questions=3)
运行元数据提取器
假设文档已经定义,提取节点:
from llama_index.core.ingestion import IngestionPipeline
pipeline = IngestionPipeline(
transformations=[text_splitter, title_extractor, qa_extractor]
)
nodes = pipeline.run(
documents=documents,
in_place=True,
show_progress=True,
)
或者插入到索引中:
from llama_index.core import VectorStoreIndex
index = VectorStoreIndex.from_documents(
documents, transformations=[text_splitter, title_extractor, qa_extractor]
)
自动元数据提取以改善检索和合成
在本教程中,我们展示了如何执行自动元数据提取以获得更好的检索结果。我们使用两个提取器:一个 QuestionsAnsweredExtractor
,它从一段文本中生成问答对,以及一个 SummaryExtractor
,它不仅在当前文本中提取摘要,还在相邻文本中提取摘要。
我们展示了这允许“块梦境”——每个单独的块可以有更多的“整体”细节,从而在给定检索结果的情况下提高答案质量。
我们的数据来源取自 Eugene Yan 的流行文章《LLM Patterns》:https://eugeneyan.com/writing/llm-patterns/
设置
如果你在 colab 上打开这个 Notebook,你可能需要安装 LlamaIndex 🦙。
%pip install llama-index-llms-openai
%pip install llama-index-readers-web
!pip install llama-index
import nest_asyncio
nest_asyncio.apply()
import os
import openai
# OPTIONAL: setup W&B callback handling for tracing
from llama_index.core import set_global_handler
set_global_handler("wandb", run_args={
"project": "llamaindex"}