使用 VectorStoreIndex#
向量存储是检索增强生成 (RAG) 的关键组件,因此在使用 LlamaIndex 构建的几乎所有应用程序中,您都会直接或间接使用它们。
向量存储接受一个 Node
对象列表,并从中构建索引
将数据加载到索引中#
基本用法#
使用向量存储的最简单方法是加载一组文档,然后使用 from_documents
从中构建索引
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
# Load documents and build index
documents = SimpleDirectoryReader(
"../../examples/data/paul_graham"
).load_data()
index = VectorStoreIndex.from_documents(documents)
提示
如果您在命令行上使用 from_documents
,传递 show_progress=True
可以方便地在索引构建期间显示进度条。
当您使用 from_documents
时,您的文档会被分割成块,并解析成 Node
对象,这些对象是对文本字符串的轻量级抽象,用于跟踪元数据和关系。
有关如何加载文档的更多信息,请参阅理解加载。
默认情况下,VectorStoreIndex 将所有内容存储在内存中。有关如何使用持久化向量存储的更多信息,请参阅下面的使用向量存储。
提示
默认情况下,VectorStoreIndex
会以 2048 个节点为一批生成并插入向量。如果您内存受限(或内存充足),您可以通过传递 insert_batch_size=2048
以及您期望的批大小来修改此设置。
这在您插入远程托管的向量数据库时特别有用。
使用摄取管道创建节点#
如果您想更精细地控制文档的索引方式,我们建议使用摄取管道。这使您可以自定义节点的块切分、元数据和嵌入。
from llama_index.core import Document
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core.extractors import TitleExtractor
from llama_index.core.ingestion import IngestionPipeline, IngestionCache
# create the pipeline with transformations
pipeline = IngestionPipeline(
transformations=[
SentenceSplitter(chunk_size=25, chunk_overlap=0),
TitleExtractor(),
OpenAIEmbedding(),
]
)
# run the pipeline
nodes = pipeline.run(documents=[Document.example()])
提示
您可以了解更多关于如何使用摄取管道的信息。
直接创建和管理节点#
如果您想完全控制索引,您可以手动创建和定义节点,然后将它们直接传递给索引构造函数
from llama_index.core.schema import TextNode
node1 = TextNode(text="<text_chunk>", id_="<node_id>")
node2 = TextNode(text="<text_chunk>", id_="<node_id>")
nodes = [node1, node2]
index = VectorStoreIndex(nodes)
处理文档更新#
直接管理索引时,您会遇到随时间变化的数据源。Index
类具有插入、删除、更新和刷新操作,您可以在下面了解更多信息
存储向量索引#
LlamaIndex 支持数十种向量存储。您可以通过传入一个 StorageContext
来指定使用哪种向量存储,并在其中指定 vector_store
参数,例如使用 Pinecone 的示例所示
import pinecone
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
StorageContext,
)
from llama_index.vector_stores.pinecone import PineconeVectorStore
# init pinecone
pinecone.init(api_key="<api_key>", environment="<environment>")
pinecone.create_index(
"quickstart", dimension=1536, metric="euclidean", pod_type="p1"
)
# construct vector store and customize storage context
storage_context = StorageContext.from_defaults(
vector_store=PineconeVectorStore(pinecone.Index("quickstart"))
)
# Load documents and build index
documents = SimpleDirectoryReader(
"../../examples/data/paul_graham"
).load_data()
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context
)
有关如何使用 VectorStoreIndex 的更多示例,请参阅我们的向量存储索引使用示例笔记本。
有关如何将 VectorStoreIndex 与特定向量存储一起使用的示例,请查看“存储”部分下的向量存储。
可组合检索#
VectorStoreIndex
(以及任何其他索引/检索器)能够检索通用对象,包括
- 节点引用
- 查询引擎
- 检索器
- 查询管道
如果这些对象被检索到,它们将使用提供的查询自动运行。
例如
from llama_index.core.schema import IndexNode
query_engine = other_index.as_query_engine
obj = IndexNode(
text="A query engine describing X, Y, and Z.",
obj=query_engine,
index_id="my_query_engine",
)
index = VectorStoreIndex(nodes=nodes, objects=[obj])
retriever = index.as_retriever(verbose=True)
如果检索到包含查询引擎的索引节点,查询引擎将运行并将结果响应作为节点返回。
更多详细信息,请查看指南