在处理大量文档数据时,我们常常面临这样的困扰:当文档内容更新或新增时,如何避免重复处理已存在的文档?传统方案中,每次更新都重新索引全部内容,不仅消耗大量资源,还可能导致数据冗余。今天我们就来探讨一种智能解决方案 —— 通过摄入管道结合文档存储(docstore)实现文档的增量更新与高效管理,让系统只处理变化的内容,大幅提升数据处理效率。
摄入管道与文档存储的结合,核心在于建立了一套 "文档指纹" 追踪体系。系统会为每个文档生成唯一的标识(doc_id),并计算内容哈希值(document_hash),通过存储doc_id -> document_hash
的映射关系来识别文档变化:
首先安装必要的依赖包:
bash
# 安装Redis/MongoDB文档存储相关包
pip install llama-index-storage-docstore-redis
pip install llama-index-storage-docstore-mongodb
# 安装HuggingFace嵌入模型
pip install llama-index-embeddings-huggingface
创建测试数据作为种子文档:
bash
# 创建数据文件夹
mkdir -p data
# 生成测试文件
echo "这是一个测试文件:其一!" > data/test1.txt
echo "这是一个测试文件:其二!" > data/test2.txt
使用 SimpleDirectoryReader 加载文档,并指定以文件名作为文档 ID:
python
from llama_index.core import SimpleDirectoryReader
# 加载文档并使用文件名作为唯一ID
documents = SimpleDirectoryReader("./data", filename_as_id=True).load_data()
python
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.core.ingestion import IngestionPipeline
from llama_index.core.storage.docstore import SimpleDocumentStore
from llama_index.core.node_parser import SentenceSplitter
# 构建摄入管道:包含文本分割和嵌入生成
pipeline = IngestionPipeline(
transformations=[
SentenceSplitter(), # 按句子分割文本
HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5"), # 生成文本嵌入向量
],
docstore=SimpleDocumentStore(), # 使用简单文档存储
)
# 执行摄入流程
nodes = pipeline.run(documents=documents)
print(f"已摄取 {len(nodes)} 个节点") # 输出:已摄取 2 个节点
python
# 保存管道状态(包括缓存和文档存储)
pipeline.persist("./pipeline_storage")
# 恢复管道(后续使用时可直接加载)
pipeline = IngestionPipeline(
transformations=[
SentenceSplitter(),
HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5"),
]
)
pipeline.load("./pipeline_storage")
修改现有文档并添加新文档,测试系统是否只处理变化的内容:
bash
# 修改test1.txt并添加test3.txt
echo "这是一个新的测试文件:一!" > data/test1.txt
echo "这是一个测试文件:三!" > data/test3.txt
重新加载文档并执行摄入流程:
python
# 重新加载更新后的文档
documents = SimpleDirectoryReader("./data", filename_as_id=True).load_data()
nodes = pipeline.run(documents=documents)
print(f"摄取了 {len(nodes)} 个节点") # 输出:摄取了 2 个节点
# 验证具体摄取的内容
for node in nodes:
print(f"节点: {node.text}")
# 输出应为修改后的test1和新增的test3内容
# 验证文档存储中的文档数量
print(len(pipeline.docstore.docs)) # 输出:3(两个原文档+一个新文档)
实际项目中可根据需求选择更强大的文档存储:
python
# 使用Redis作为文档存储(适合分布式环境)
from llama_index.storage.docstore.redis import RedisDocumentStore
redis_docstore = RedisDocumentStore.from_host_and_port("localhost", 6379)
# 使用MongoDB作为文档存储(适合海量数据)
from llama_index.storage.docstore.mongodb import MongoDocumentStore
mongo_docstore = MongoDocumentStore.from_uri("mongodb://localhost:27017")
通过今天的实践,我们实现了一个具备智能文档管理能力的摄入管道系统,它能够:
在实际应用中,建议根据文档更新频率和数据规模做以下优化:
如果本文对你有帮助,别忘了点赞收藏,关注我,一起探索更高效的开发方式~