################ 6. LlamaIndex ################ .. include:: ../links.ref .. include:: ../tags.ref .. include:: ../abbrs.ref ============ ========================== **Abstract** LlamaIndex 入门教程 **Authors** Walter Fan **Status** WIP as draft **Updated** |date| ============ ========================== LlamaIndex 是一个强大的数据框架,专为连接大语言模型(LLM)与外部数据源而设计。 它提供了从数据摄取、索引构建到查询优化的完整解决方案。 为什么需要 LlamaIndex? ====================== 大语言模型虽然强大,但存在以下限制: - **知识截止** - 训练数据有时间限制,无法获取最新信息 - **无法访问私有数据** - 无法直接使用企业内部文档、数据库等 - **上下文窗口有限** - 无法一次处理大量文档 - **幻觉问题** - 可能生成与事实不符的内容 LlamaIndex 通过以下方式解决这些问题: - **数据连接器** - 支持 100+ 种数据源(PDF、数据库、API 等) - **智能索引** - 高效组织和检索文档 - **查询引擎** - 提供多种检索和生成策略 - **Agent 系统** - 支持复杂的推理和工具调用 核心架构 ======== .. code-block:: text ┌─────────────────────────────────────────────────────────────────┐ │ LlamaIndex Architecture │ │ │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ Data Source │───►│ Loading │───►│ Indexing │ │ │ │ 数据源 │ │ 数据加载 │ │ 构建索引 │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ │ │ │ │ │ │ │ ▼ ▼ ▼ │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ PDF/HTML/ │ │ Document │ │ Vector │ │ │ │ Database/ │ │ Node │ │ Index │ │ │ │ API/... │ │ Parsing │ │ Storage │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ │ │ │ │ │ ▼ │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ Response │◄───│ Query │◄───│ Retriever │ │ │ │ 生成响应 │ │ Engine │ │ 检索器 │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────┘ 学习目标 ======== 完成本教程后,你将能够: - 理解 LlamaIndex 的核心概念(Document、Node、Index、Query Engine) - 使用数据连接器加载各种格式的文档 - 掌握文本分割和节点解析策略 - 构建和优化向量索引 - 实现高级检索策略(混合检索、重排序等) - 使用 LlamaIndex Agent 进行复杂推理 - 部署生产级 RAG 应用 实战项目:智能知识库系统 ======================== 我们将构建一个完整的企业知识库问答系统: .. code-block:: text ┌─────────────────────────────────────────────────────────────────┐ │ Intelligent Knowledge Base System │ │ │ │ [文档采集] ──► [智能解析] ──► [索引构建] ──► [查询服务] │ │ │ │ │ │ │ │ ▼ ▼ ▼ ▼ │ │ [多源数据] [结构化提取] [向量存储] [对话问答] │ │ │ │ │ ▼ │ │ [知识增强生成] │ │ │ │ │ ▼ │ │ [持续学习优化] │ └─────────────────────────────────────────────────────────────────┘ .. list-table:: 与其他框架对比 :header-rows: 1 :widths: 25 25 25 25 * - 特性 - LlamaIndex - LangChain - 原生实现 * - 数据连接 - ★★★★★ - ★★★☆☆ - ★☆☆☆☆ * - 索引优化 - ★★★★★ - ★★★☆☆ - ★★☆☆☆ * - 检索策略 - ★★★★★ - ★★★★☆ - ★★☆☆☆ * - Agent 能力 - ★★★★☆ - ★★★★★ - ★☆☆☆☆ * - 学习曲线 - 中等 - 中等 - 高 .. toctree:: :maxdepth: 1 :caption: 教程目录: tutorial_01_introduction tutorial_02_data_loading tutorial_03_node_parsing tutorial_04_embeddings_vectorstore tutorial_05_index_types tutorial_06_query_engine tutorial_07_retrieval_strategies tutorial_08_agents_tools tutorial_09_knowledge_base tutorial_10_production