Skip to content

使用 VectorStoreIndex#

向量存储是检索增强生成(RAG)的关键组件,因此在使用 LlamaIndex 开发的几乎所有应用中,你都会直接或间接地用到它们。

向量存储接收一组 Node 对象并基于它们构建索引

向索引中加载数据#

基础用法#

使用向量存储最简单的方式是通过 from_documents 加载文档集合并构建索引:

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

# 加载文档并构建索引
documents = SimpleDirectoryReader(
    "../../examples/data/paul_graham"
).load_data()
index = VectorStoreIndex.from_documents(documents)

Tip

如果在命令行使用 from_documents,可以传入 show_progress=True 来显示索引构建的进度条,这会非常方便。

使用 from_documents 时,你的文档会被分割成块并解析为 Node 对象,这些是对文本字符串的轻量级抽象,用于跟踪元数据和关系。

关于如何加载文档的更多信息,请参阅理解加载

默认情况下,VectorStoreIndex 将所有内容存储在内存中。关于如何使用持久化向量存储的更多信息,请参阅下面的使用向量存储

Tip

默认情况下,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

# 创建包含转换步骤的管道
pipeline = IngestionPipeline(
    transformations=[
        SentenceSplitter(chunk_size=25, chunk_overlap=0),
        TitleExtractor(),
        OpenAIEmbedding(),
    ]
)

# 运行管道
nodes = pipeline.run(documents=[Document.example()])

Tip

你可以了解更多关于如何使用数据预处理管道的信息。

直接创建和管理节点#

如果你想完全控制索引,可以手动创建和定义节点并直接传递给索引构造函数:

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

# 初始化 pinecone
pinecone.init(api_key="<api_key>", environment="<environment>")
pinecone.create_index(
    "quickstart", dimension=1536, metric="euclidean", pod_type="p1"
)

# 构造向量存储并自定义存储上下文
storage_context = StorageContext.from_defaults(
    vector_store=PineconeVectorStore(pinecone.Index("quickstart"))
)

# 加载文档并构建索引
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)

如果包含查询引擎的索引节点被检索到,查询引擎将会运行,并将结果作为节点返回。

更多细节,请查看指南