使用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)
如果检索到包含查询引擎的索引节点,将运行该查询引擎并将结果响应作为节点返回。
更多详情,请查看指南