LlamaIndex RAG 框架入门
学习专为RAG设计的LlamaIndex框架,轻松构建知识库问答系统
18分钟
2025-12-19
LlamaIndexRAG知识库向量搜索Python
LlamaIndex RAG 框架入门
什么是 LlamaIndex
LlamaIndex(原名 GPT Index)是一个专门为 RAG(检索增强生成)设计的数据框架。
核心优势
- 专注数据:专为连接 LLM 和私有数据设计
- 简单易用:几行代码即可构建 RAG 系统
- 高级索引:支持多种索引结构
- 查询优化:内置查询改写、重排序等优化
LlamaIndex vs LangChain
| 特性 | LlamaIndex | LangChain |
|---|---|---|
| 定位 | 数据框架 | 通用框架 |
| RAG | 深度优化 | 基础支持 |
| Agent | 基础支持 | 深度支持 |
安装配置
# 核心包
pip install llama-index
# OpenAI 集成
pip install llama-index-llms-openai llama-index-embeddings-openai
快速开始
5分钟构建 RAG
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
# 1. 加载文档
documents = SimpleDirectoryReader("./data").load_data()
# 2. 创建索引
index = VectorStoreIndex.from_documents(documents)
# 3. 查询
query_engine = index.as_query_engine()
response = query_engine.query("文档中说了什么?")
print(response)
核心概念
1. Documents & Nodes
from llama_index.core import Document
from llama_index.core.node_parser import SentenceSplitter
# 创建文档
doc = Document(text="这是文档内容...", metadata={"source": "file.txt"})
# 分割为节点
parser = SentenceSplitter(chunk_size=1024, chunk_overlap=200)
nodes = parser.get_nodes_from_documents([doc])
2. Query Engines
# 基础查询引擎
query_engine = index.as_query_engine()
# 自定义参数
query_engine = index.as_query_engine(
similarity_top_k=5,
response_mode="tree_summarize"
)
# 流式输出
query_engine = index.as_query_engine(streaming=True)
response = query_engine.query("问题")
response.print_response_stream()
3. Chat Engines
# 创建对话引擎
chat_engine = index.as_chat_engine(chat_mode="condense_question")
# 多轮对话
response = chat_engine.chat("你好")
response = chat_engine.chat("能详细说说吗?")
高级功能
使用向量数据库
import chromadb
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContext
# 创建 Chroma 客户端
chroma_client = chromadb.PersistentClient(path="./chroma_db")
collection = chroma_client.get_or_create_collection("my_collection")
# 创建向量存储
vector_store = ChromaVectorStore(chroma_collection=collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
# 创建索引
index = VectorStoreIndex.from_documents(
documents,
storage_context=storage_context
)
持久化存储
# 保存索引
index.storage_context.persist(persist_dir="./storage")
# 加载索引
from llama_index.core import StorageContext, load_index_from_storage
storage_context = StorageContext.from_defaults(persist_dir="./storage")
index = load_index_from_storage(storage_context)