主题
数据加载并索引之后,你多半会想把它存下来,以免重复索引所花费的时间和成本。默认情况下,你的索引数据只保存在内存里。
持久化到磁盘
保存索引数据最简单的方式,是用每种索引都自带的 .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)提示
重要:如果你当初是用自定义的 transformations、embed_model 等初始化索引的,那么在 load_index_from_storage 时必须传入同样的选项,或者把它们设置为全局配置。
使用向量库
如索引一节所述,最常见的索引类型之一就是 VectorStoreIndex。创建 VectorStoreIndex 中那些嵌入的 API 调用在时间和金钱上都不便宜,所以你需要把它们存下来,避免反复重建索引。
LlamaIndex 支持数量庞大的向量库,它们在架构、复杂度和成本上各不相同。这个例子我们用开源的向量库 Chroma。
首先需要安装 chroma:
pip install chromadb要用 Chroma 存放 VectorStoreIndex 的嵌入,你需要:
- 初始化 Chroma 客户端
- 在 Chroma 中创建一个 Collection 用来存数据
- 把 Chroma 指派为
StorageContext里的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)如果你已经生成并存储好了嵌入,通常希望直接加载它们,而不必再加载文档或新建一个 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,见文档管理操作指南。