跳过内容

存储#

概念#

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

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

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

文档/索引存储依赖于一个通用的键值存储抽象,下文也将对此进行详细介绍。

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

  • 本地文件系统
  • AWS 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(),
)

有关自定义/持久化的更多详细信息,请参阅下方的指南。

模块#

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