源码深度学习 · 零基础友好

LlamaIndex 源码学习

20 天 · 每天 30 分钟 · 逐层读懂 LlamaIndex——最流行的 RAG(检索增强生成)与智能体应用框架(Python)。它帮你把"私有数据 + 大模型"接起来:读数据 → 切块 → 向量化 → 建索引 → 检索 → 喂给 LLM 生成回答。核心是 llama-index-core + 300+ 集成插件。

00

开篇 · 这是个什么项目

LlamaIndexREADME.md:"an open-source framework to build agentic applications")是构建 RAG 和智能体应用的开源框架。最经典的用途:让大模型"读懂你的私有文档"——把 PDF/网页/数据库喂进去,就能基于它们问答。新版还把 Workflow(事件驱动)作为 Agent 的底座。

🎯 一句话:LlamaIndex = 数据摄入(读取+切块+向量化) + 索引与存储(向量库+文档库) + 检索与查询(找相关片段+合成答案) + Agent/Workflow(让 LLM 用工具、多步推理)。它把"RAG 全流程"封装成可组合的模块。

📄

数据摄入

读文档 → 切成 Node → 嵌入向量。

🗂️

索引与存储

VectorStoreIndex + docstore/vector_store。

🔍

检索与查询

Retriever 找片段 + 合成答案。

🤖

Agent/Workflow

事件驱动的多步智能体。

什么是 RAG? 大模型知识有限、会"胡编"。RAG(Retrieval-Augmented Generation)= 先从你的资料库里检索出相关片段,再连同问题一起喂给大模型生成答案——让它"开卷考试",答得准、可溯源。LlamaIndex 就是做这件事最流行的框架。源码在 llama-index-core/llama_index/core/(几十个子模块)。
01

架构全景(动起来看)

下图那颗发光小球是"你的文档变成可问答知识、再回答问题"的旅程。从上到下:原始数据 → 摄入 → 索引存储 → 检索生成。

L1 · 数据摄入(Ingestion)
Reader读文档→Document
NodeParser切成 Node
Embedding向量化
L2 · 索引与存储
IndexVectorStoreIndex
VectorStore向量库
DocStore文档库
L3 · 检索与查询
Retriever找相关 Node
Postprocessor重排/过滤
Synthesizer合成答案
L4 · Agent / Workflow
Workflow事件驱动
Agent用工具
LLM生成

最经典的 RAG 心智模型是"读 → 切 → 嵌 → 存 → 检 → 答":

读+切Document→Node 嵌+存向量→索引 检索找相关 Node 合成LLM 生成答案
LlamaIndex 的两条主线:① RAG(检索增强问答,最成熟);② Agentic(用 Workflow 编排的智能体,能用工具、多步推理)。前三周主攻 RAG 全链路,第四周讲 Agent/Workflow——它们能把 RAG 当"工具"来用。
02

怎么用这份教程

⏱️

每天 30 分钟

一天一个独立页面,跟着"今日小结 + 动手"收尾。

👶

零基础友好

不假设你懂 RAG/向量/嵌入,术语首次出现都有大白话解释。

📍

精确到行号

关键代码标 文件:行号,可在真源码里跳转对照。

一条主线:从"什么是 RAG、数据怎么摄入",到"索引/嵌入/存储怎么组织",到"检索和查询怎么工作",最后是"Agent/Workflow 怎么编排 + 可观测 + 生态"。源码只读 llama-index-core(框架骨架),集成插件(300+)用到时点名。
W1

第 1 周 · RAG 全景与数据摄入

全景、Node 数据结构、Reader、切块、Ingestion
01

项目全景(RAG 框架)

LlamaIndex 是什么、RAG、core+integrations、和 LangChain 对比。

≈30 min开始学 →
02

核心数据结构 Node

Document/TextNode/BaseNode、schema、关系。

≈30 min开始学 →
03

读取数据 Reader

SimpleDirectoryReader、生成 Document。

≈30 min开始学 →
04

切块 NodeParser

SentenceSplitter、chunk/overlap、metadata。

≈30 min开始学 →
05

摄入管道 Ingestion

IngestionPipeline、transformations、缓存。

≈30 min开始学 →
W2

第 2 周 · 索引/嵌入/存储

嵌入、索引、存储上下文、向量库、其他索引
06

嵌入 Embeddings

BaseEmbedding、向量与相似度。

≈30 min开始学 →
07

索引 Index

BaseIndex、VectorStoreIndex 构建。

≈30 min开始学 →
08

存储 StorageContext

docstore/index_store/vector_store 三件套。

≈30 min开始学 →
09

向量存储 VectorStore

SimpleVectorStore、query 相似度检索。

≈30 min开始学 →
10

其他索引类型

Summary/KeywordTable/PropertyGraph 概览。

≈30 min开始学 →
W3

第 3 周 · 检索与查询

检索器、查询引擎、响应合成、后处理、LLM
11

检索器 Retriever

BaseRetriever、VectorIndexRetriever、retrieve。

≈30 min开始学 →
12

查询引擎 QueryEngine

RetrieverQueryEngine、query 端到端。

≈30 min开始学 →
13

响应合成 Synthesizer

refine/compact/tree_summarize 模式。

≈30 min开始学 →
14

后处理 + 提示词

postprocessor 重排/过滤 + prompts 模板。

≈30 min开始学 →
15

LLM 抽象

BaseLLM、chat/complete、流式。

≈30 min开始学 →
W4

第 4 周 · Agent/Workflow/生态

Workflow、Agent、聊天记忆、可观测、收官
16

Workflow 引擎

事件驱动、@step、Event、Context。

≈30 min开始学 →
17

Agent 智能体

FunctionAgent/ReActAgent、tools、基于 workflow。

≈30 min开始学 →
18

聊天引擎 + 记忆

chat_engine、memory 多轮对话。

≈30 min开始学 →
19

可观测 + 评估

callbacks/instrumentation + evaluation。

≈30 min开始学 →
20

部署与收官串讲

core vs integrations + RAG 全景 + 框架对比。

≈30 min开始学 →

全部 20 天已就绪。零基础也能跟——每个 RAG/向量/智能体概念都有大白话铺垫。