跳转到内容

存储

LlamaIndex 提供了一个用于摄取、索引和查询外部数据的高级接口。

在底层,LlamaIndex 还支持可替换的存储组件,允许您自定义:

  • 文档存储: 存储已摄入文档(即 Node 对象)的位置,
  • 索引存储: 存储索引元数据的地方,
  • 向量存储: 存储嵌入向量的地方。
  • 属性图存储:知识图谱的存储位置(例如用于 PropertyGraphIndex)。
  • 聊天存储: 用于存储和组织聊天消息的地方。

文档/索引存储依赖于一个通用的键值存储抽象,该抽象也在下文详细说明。

LlamaIndex 支持将数据持久化到任何由 fsspec 支持的存储后端。 我们已经确认支持以下存储后端:

  • 本地文件系统
  • 亚马逊云存储服务 S3
  • Cloudflare R2

许多向量存储(除FAISS外)会同时存储数据及其索引(嵌入向量)。这意味着您无需使用单独的文档存储或索引存储。同时也意味着您无需显式持久化这些数据——系统会自动完成此操作。构建新索引/重新加载现有索引的使用方式如下所示。

## build a new index
from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.vector_stores.deeplake import DeepLakeVectorStore
# construct vector store and customize storage context
vector_store = DeepLakeVectorStore(dataset_path="<dataset_path>")
storage_context = StorageContext.from_defaults(vector_store=vector_store)
# Load documents and build index
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context
)
## reload an existing one
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
# create storage context using default stores
storage_context = StorageContext.from_defaults(
docstore=SimpleDocumentStore(),
vector_store=SimpleVectorStore(),
index_store=SimpleIndexStore(),
)

更多关于自定义/持久化的详细信息可在以下指南中找到。

我们提供关于不同存储组件的深入指南。