LlamaIndex中的摄取管道:数据转换与缓存管理
在LlamaIndex中,摄取管道(IngestionPipeline)使用了一种称为“转换”(Transformations)的概念来处理输入数据。这些转换应用于你的输入数据,结果节点要么返回,要么插入到向量数据库中(如果提供了的话)。每个节点+转换组合都会被缓存,以便后续运行(如果缓存被持久化)时使用相同的节点+转换组合可以利用缓存结果,节省时间。
要查看摄取管道实际使用的交互式示例,请查看RAG CLI。
使用模式
最简单的使用方法是实例化一个摄取管道,如下所示:
from llama_index.core import Document
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core.extractors import TitleExtractor
from llama_index.core.ingestion import IngestionPipeline, IngestionCache
# 创建带有转换的管道
pipeline = IngestionPipeline(
transformations=[
SentenceSplitter(chunk_size=25, chunk_overlap=0),
TitleExtractor(),
OpenAIEmbedding(),
]
)
# 运行管道
nodes = pipeline.run(documents=[Document.example()])
请注意,在实际场景中,你会从SimpleDirectoryReader或Llama Hub中的其他读取器获取文档。
连接到向量数据库
运行摄取管道时,你还可以选择自动将结果节点插入到远程向量存储中。然后,你可以稍后从该向量存储构建索引。
from llama_index.core import Document
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core.extractors import TitleExtractor
from llama_index.core.ingestion import IngestionPipeline
from llama_index.vector_stores.qdrant import QdrantVectorStore
import qdrant_client
client = qdrant_client.QdrantClient(location=":memory:")
vector_store = QdrantVectorStore(client=client, collection_name="test_store")
pipeline = IngestionPipeline(
transformations=[
SentenceSplitter(chunk_size=25, chunk_overlap=0),
TitleExtractor(),
OpenAIEmbedding(),
],
vector_store=vector_store,
)
# 直接摄取到向量数据库
pipeline.run(documents=[Document.example()])
# 创建你的索引
from llama_index.core import VectorStoreIndex
index = VectorStoreIndex.from_vector_store(vector_store)
在管道中计算嵌入
请注意,在上面的示例中,嵌入作为管道的一部分进行计算。如果你将管道连接到向量存储,嵌入必须是管道的一个阶段,否则稍后实例化索引时会失败。
如果你不连接到向量存储,可以省略管道中的嵌入,即仅生成节点列表。
缓存
在摄取管道中,每个节点+转换组合都会被哈希并缓存。这节省了使用相同数据进行后续运行的时间。
以下部分描述了一些基本的缓存管理用法。
本地缓存管理
一旦你有了管道,你可能想要存储和加载缓存。
# 保存
pipeline.persist("./pipeline_storage")
# 加载并恢复状态
new_pipeline = IngestionPipeline(
transformations=[
SentenceSplitter(chunk_size=25, chunk_overlap=0),
TitleExtractor(),
],
)
new_pipeline.load("./pipeline_storage")
# 由于缓存,运行速度会非常快
nodes = pipeline.run(documents=[Document.example()])
如果缓存变得太大,你可以清除它:
# 删除所有缓存内容
cache.clear()
远程缓存管理
我们支持多种远程存储后端用于缓存:
- RedisCache
- MongoDBCache
- FirestoreCache
以下是使用RedisCache的示例:
from llama_index.core import Document
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core.extractors import TitleExtractor
from llama_index.core.ingestion import IngestionPipeline, IngestionCache
from llama_index.storage.kvstore.redis import RedisKVStore as RedisCache
ingest_cache = IngestionCache(
cache=RedisCache.from_host_and_port(host="127.0.0.1", port=6379),
collection="my_test_cache",
)
pipeline = IngestionPipeline(
transformations=[
SentenceSplitter(chunk_size=25, chunk_overlap=0),
TitleExtractor(),
OpenAIEmbedding(),
],
cache=ingest_cache,
)
# 直接摄取到向量数据库
nodes = pipeline.run(documents=[Document.example()])
这里不需要持久化步骤,因为所有内容都会在你进行时缓存到指定的远程集合中。
异步支持
摄取管道还支持异步操作:
nodes = await pipeline.arun(documents=documents)
文档管理
将文档存储附加到摄取管道将启用文档管理。
使用document.doc_id或node.ref_doc_id作为基础点,摄取管道将主动查找重复文档。
它的工作原理是:
- 存储doc_id -> document_hash的映射。
- 如果连接了向量存储:
- 如果检测到重复的doc_id且哈希已更改,文档将重新处理并更新插入。
- 如果检测到重复的doc_id且哈希未更改,节点将被跳过。
- 如果未连接向量存储:
- 检查所有现有哈希的每个节点。
- 如果找到重复项,节点将被跳过。
- 否则,节点将被处理。
注意:如果我们不附加向量存储,我们只能检查并删除重复的输入。
from llama_index.core.ingestion import IngestionPipeline
from llama_index.core.storage.docstore import SimpleDocumentStore
pipeline = IngestionPipeline(
transformations=[...], docstore=SimpleDocumentStore()
)
完整的演练可以在我们的演示笔记本这里找到。
还可以查看另一个使用Redis作为整个摄取栈的指南这里。
并行处理
摄取管道的run方法可以并行执行。它通过使用multiprocessing.Pool将节点批次分发到处理器上来实现。
要并行执行,请将num_workers设置为你希望使用的进程数:
from llama_index.core.ingestion import IngestionPipeline
pipeline = IngestionPipeline(
transformations=[...],
)
pipeline.run(documents=[...], num_workers=4)
通过这些方法,你可以高效地在LlamaIndex中使用摄取管道,根据需要进行自定义和扩展。