首页/AI/RAG 与向量库速查/LlamaIndex 快速搭建索引与查询引擎

LlamaIndex 快速搭建索引与查询引擎

LlamaIndex 用 SimpleDirectoryReader 加载文档、VectorStoreIndex 建索引,查询引擎自动完成“检索 + 合成回答”,适合文档问答原型。

RAG 与向量库速查AI更新于 2026-10-12

精简示例

精简示例
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex

docs = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(docs)
engine = index.as_query_engine(similarity_top_k=3)
resp = engine.query("文档里如何配置超时?")
print(resp)
for node in resp.source_nodes:
    print(node.score, node.metadata.get("file_name"))
默认是内存索引,进程退出即丢失;生产要配置持久化(落盘或外接 Milvus / Qdrant / pgvector 等向量库)。

易错点

  • from_documents 会逐块调用 embedding,数据多时要配置批量与并发
  • 忘记持久化:每次重启都重建索引,费用与时间翻倍

数据来源与参考

  • LlamaIndex 官方文档(docs.llamaindex.ai)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌