Skip to content
LlamaIndex 文档版本 跟随上游 源快照 cf1dc5ce 更新于 2026-10-15

向量库是检索增强生成(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 时,你的 Documents 会被切成块并解析为Node 对象——它们是叠加在文本串之上的轻量抽象,负责记录元数据与关联关系。

关于如何加载文档的更多内容,见理解加载(Understanding Loading)。

默认情况下,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 的用法示例,见我们的向量库索引用法示例 notebook。

关于 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)

如果检索到的是包含查询引擎的那个索引节点,该查询引擎就会被执行,其响应结果会作为节点返回。

更多细节见该指南

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。