06.2 - LlamaIndex 实践指南
定位: 专注 RAG 数据层的框架——LlamaIndex 的索引系统、查询引擎与高级 RAG 模式 面试高频度: ⭐⭐⭐ 考查方式: LlamaIndex 数据模型、索引类型、查询引擎
一、这是什么?为什么需要它?
是什么
LlamaIndex 是一个专注于 RAG 数据层的框架,提供了一整套数据索引和查询工具。相比 LangChain 的"通用 LLM 框架"定位,LlamaIndex 更聚焦于"如何高效地索引和检索数据"。
LangChain 视角:RAG 是 LLM 应用的一种
Chain -> Agent -> Tool -> RAG
RAG 是众多功能之一
LlamaIndex 视角:RAG 是数据管理的一种方式
Ingestion -> Indexing -> Retrieval -> Query Engine
RAG 就是全部功能为什么使用 LlamaIndex?
1. 丰富的数据连接器:75+ 数据源支持
2. 多种索引类型:向量索引、摘要索引、树索引、关键词索引
3. 强大的查询引擎:自动路由、多步推理
4. 结构化+RAG:支持与 SQL/Graph 结合
5. 与 LangChain 互补:可用作 LangChain 的"数据层"二、原理拆解
2.1 LlamaIndex 核心概念
Document(原始文档)
-> Node(文档块,基本信息单元)
-> Index(索引结构)
-> Retriever(检索器)
-> Query Engine(查询引擎)| 概念 | 说明 | 对应 LangChain |
|---|---|---|
| Document | 原始文档(PDF/网页/API) | Document |
| Node | 文档块(含 metadata 和关系) | Document 分块后 |
| Index | 文档块的索引结构 | VectorStore |
| Retriever | 从 Index 检索的方法 | Retriever |
| Query Engine | 检索 + 生成的端到端组件 | Chain |
| Chat Engine | 多轮对话引擎 | Conversation Chain |
2.2 索引类型对比
LlamaIndex 提供多种索引类型,适用于不同场景:
python
from llama_index.core import (
VectorStoreIndex,
SummaryIndex,
TreeIndex,
KeywordTableIndex,
KnowledgeGraphIndex,
)
# 向量索引(最常用)
index = VectorStoreIndex.from_documents(documents)
# 适合:语义搜索、开放域问答
# 摘要索引
index = SummaryIndex.from_documents(documents)
# 适合:需要对整篇文档做摘要
# 树索引
index = TreeIndex.from_documents(documents)
# 适合:分层检索(类似 RAPTOR)
# 关键词索引
index = KeywordTableIndex.from_documents(documents)
# 适合:精确关键词匹配
# 知识图谱索引
index = KnowledgeGraphIndex.from_documents(documents)
# 适合:实体关系查询| 索引类型 | 检索方式 | 适合 | 不适合 |
|---|---|---|---|
| VectorStoreIndex | 语义相似度 | 开放问答、语义搜索 | 精确匹配 |
| SummaryIndex | 摘要+回溯 | 文档总结 | 具体事实 |
| TreeIndex | 层次化 | 结构文档 | 碎片文档 |
| KeywordTableIndex | 关键词 | 专有名词 | 语义模糊 |
| KnowledgeGraphIndex | 图遍历 | 关系推理 | 简单问答 |
2.3 LlamaIndex RAG 完整示例
python
# 安装:pip install llama-index-core llama-index-readers-file
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.node_parser import SentenceSplitter
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.llms.openai import OpenAI
# 1. 加载文档
documents = SimpleDirectoryReader("./documents").load_data()
# 2. 分块配置
parser = SentenceSplitter(chunk_size=512, chunk_overlap=100)
nodes = parser.get_nodes_from_documents(documents)
# 3. 创建索引
index = VectorStoreIndex(
nodes=nodes,
embed_model=OpenAIEmbedding(),
)
# 4. 创建查询引擎
query_engine = index.as_query_engine(
llm=OpenAI(model="gpt-4o-mini"),
similarity_top_k=5,
)
# 5. 查询
response = query_engine.query("公司年假政策是什么?")
print(response)2.4 高级查询引擎
python
# Router Query Engine:根据查询类型自动路由
from llama_index.core.query_engine import RouterQueryEngine
from llama_index.core.tools import QueryEngineTool
# 不同的查询引擎
vector_tool = QueryEngineTool.from_defaults(
query_engine=vector_index.as_query_engine(),
description="语义搜索,适合一般问答"
)
summary_tool = QueryEngineTool.from_defaults(
query_engine=summary_index.as_query_engine(),
description="文档总结,适合概述类问题"
)
# 路由引擎
router_engine = RouterQueryEngine.from_defaults(
query_engine_tools=[vector_tool, summary_tool],
select_multi=True, # 可同时选多个引擎
)
# 自动路由
response = router_engine.query("公司的年假制度概述")三、图解全景
LlamaIndex 架构
+------------------------------------------------------------------+
| LlamaIndex 架构 |
| |
| 数据摄入层 |
| SimpleDirectoryReader / 75+ Data Connectors |
| -> Document -> Node Parser -> Node |
| |
| 索引层 |
| VectorStoreIndex / SummaryIndex / TreeIndex / KG Index |
| + Index 管理(插入/更新/删除) |
| |
| 检索层 |
| Retriever: Vector/Single/Keyword/BM25/Ensemble |
| + Postprocessor: Reranker / SimilarityFilter / TimeWeight |
| |
| 查询引擎层 |
| Query Engine: Retriever + Response Synthesis |
| Chat Engine: 多轮对话 + Memory |
| Router Engine: 智能路由 + 多引擎组合 |
| |
| 应用层 |
| Agent + Tool / API / 自定义应用 |
+------------------------------------------------------------------+四、面试视角
| 问题 | 答案要点 |
|---|---|
| LlamaIndex 相比 LangChain 的核心优势? | 专注 RAG 数据层,索引类型丰富(向量/摘要/树/图/关键词),查询引擎自动路由,RAG 模式更深入。在纯 RAG 场景上体验优于 LangChain |
| LlamaIndex 的多种索引分别在什么场景用? | VectorIndex:通用语义搜索;SummaryIndex:文档摘要;TreeIndex:层次化文档;KeywordTableIndex:专有名词精确匹配;KGIndex:关系推理 |
| LlamaIndex 和 LangChain 可以一起用吗? | 可以。常见组合:LlamaIndex 负责数据索引和检索(作为"数据层"),LangChain 负责 Agent 编排和工具调用(作为"逻辑层") |
| LlamaIndex 的 Router Query Engine 解决了什么问题? | 自动根据查询类型选择合适的检索策略。比如"总结年假制度"走摘要索引,"年假几天"走向量索引。不需要手动判断和切换 |
📚 相关链接
- **LangChain实践指南** — 另一主流框架
- **评估指标详解** — RAG 评估指标配合 LlamaIndex
- **GraphRAG** — LlamaIndex 的 KnowledgeGraphIndex
- <- 返回 **工程化索引**