跳过内容

存储#

加载并索引数据后,您可能希望存储它,以避免重新索引的时间和成本。默认情况下,您的索引数据仅存储在内存中。

持久化到磁盘#

存储索引数据最简单的方法是使用每个 Index 内置的 .persist() 方法,该方法将所有数据写入指定位置的磁盘。这适用于任何类型的索引。

index.storage_context.persist(persist_dir="<persist_dir>")

这是一个可组合图(Composable Graph)的示例

graph.root_index.storage_context.persist(persist_dir="<persist_dir>")

然后,您可以通过加载持久化的索引来避免重新加载和重新索引数据,如下所示

from llama_index.core import StorageContext, load_index_from_storage

# rebuild storage context
storage_context = StorageContext.from_defaults(persist_dir="<persist_dir>")

# load index
index = load_index_from_storage(storage_context)

提示

重要提示:如果您使用自定义的 transformationsembed_model 等初始化了索引,则需要在 load_index_from_storage 期间传入相同的选项,或者将其设置为全局设置

使用向量存储#

正如在索引中所讨论的,Index 最常见的类型之一是 VectorStoreIndex。在 VectorStoreIndex 中创建embedding的 API 调用可能耗费大量时间和金钱,因此您会希望存储它们,以避免不断重新索引。

LlamaIndex 支持大量的向量存储,它们在架构、复杂性和成本方面各不相同。在本例中,我们将使用 Chroma,一个开源的向量存储。

首先,您需要安装 chroma

pip install chromadb

要使用 Chroma 存储 VectorStoreIndex 中的 embedding,您需要

  • 初始化 Chroma 客户端
  • 在 Chroma 中创建一个 Collection 来存储您的数据
  • StorageContext 中将 Chroma 指定为 vector_store
  • 使用该 StorageContext 初始化您的 VectorStoreIndex

这是一个示例,其中包含实际查询数据的小片段

import chromadb
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContext

# load some documents
documents = SimpleDirectoryReader("./data").load_data()

# initialize client, setting path to save data
db = chromadb.PersistentClient(path="./chroma_db")

# create collection
chroma_collection = db.get_or_create_collection("quickstart")

# assign chroma as the vector_store to the context
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)

# create your index
index = VectorStoreIndex.from_documents(
    documents, storage_context=storage_context
)

# create a query engine and query
query_engine = index.as_query_engine()
response = query_engine.query("What is the meaning of life?")
print(response)

如果您已经创建并存储了 embedding,您会希望直接加载它们,而无需加载文档或创建新的 VectorStoreIndex

import chromadb
from llama_index.core import VectorStoreIndex
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContext

# initialize client
db = chromadb.PersistentClient(path="./chroma_db")

# get collection
chroma_collection = db.get_or_create_collection("quickstart")

# assign chroma as the vector_store to the context
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)

# load your index from stored vectors
index = VectorStoreIndex.from_vector_store(
    vector_store, storage_context=storage_context
)

# create a query engine
query_engine = index.as_query_engine()
response = query_engine.query("What is llama2?")
print(response)

提示

如果您想深入了解这个存储,我们有一个更详尽的 Chroma 使用示例

您已准备好查询!#

现在您已经加载、索引并存储了数据索引,您已准备好查询您的数据

插入文档或节点#

如果您已经创建了索引,可以使用 insert 方法向索引中添加新文档。

from llama_index.core import VectorStoreIndex

index = VectorStoreIndex([])
for doc in documents:
    index.insert(doc)

有关文档管理的更多详细信息和示例 notebook,请参阅文档管理操作指南