存储#
概念#
LlamaIndex 为外部数据的摄取、索引和查询提供了高级接口。
在底层,LlamaIndex 还支持可替换的存储组件,允许您自定义:
- 文档存储:用于存储已摄取的文档(即 Node
对象)
- 索引存储:用于存储索引元数据
- 向量存储:用于存储嵌入向量
- 属性图存储:用于存储知识图谱(即 PropertyGraphIndex
)
- 聊天存储:用于存储和组织聊天消息
文档/索引存储依赖于通用的键值存储抽象,下文也将详细说明。
LlamaIndex 支持将数据持久化到任何 fsspec 兼容的存储后端。我们已确认支持以下存储后端: - 本地文件系统 - AWS S3 - Cloudflare R2
使用模式#
许多向量存储(FAISS 除外)会同时存储数据及其索引(嵌入向量)。这意味着您不需要使用单独的文档存储或索引存储。这也意味着您无需显式持久化这些数据——该过程会自动完成。构建新索引/重新加载现有索引的使用示例如下:
## 构建新索引
from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.vector_stores.deeplake import DeepLakeVectorStore
# 构造向量存储并自定义存储上下文
vector_store = DeepLakeVectorStore(dataset_path="<dataset_path>")
storage_context = StorageContext.from_defaults(vector_store=vector_store)
# 加载文档并构建索引
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context
)
## 重新加载现有索引
index = VectorStoreIndex.from_vector_store(vector_store=vector_store)
更多细节请参阅下方的向量存储模块指南。
请注意,通常要使用存储抽象,您需要定义一个 StorageContext
对象:
from llama_index.core.storage.docstore import SimpleDocumentStore
from llama_index.core.storage.index_store import SimpleIndexStore
from llama_index.core.vector_stores import SimpleVectorStore
from llama_index.core import StorageContext
# 使用默认存储创建存储上下文
storage_context = StorageContext.from_defaults(
docstore=SimpleDocumentStore(),
vector_store=SimpleVectorStore(),
index_store=SimpleIndexStore(),
)
更多关于自定义/持久化的细节可在以下指南中找到: - 自定义 - 保存/加载
模块#
我们提供关于不同存储组件的详细指南: - 向量存储 - 文档存储 - 索引存储 - 键值存储 - 属性图存储 - 聊天存储