Skip to content

LlamaIndex

LlamaIndex Inc.

一个专注于 RAG 应用的数据框架,将 LLM 与私有数据连接。

Official Site
01

概述

LlamaIndex 是由 Jerry Liu 创建的数据框架,专注于将 LLM 与私有数据连接。其核心是数据摄取、索引和检索,使 LLM 能访问和利用大量文档。LlamaIndex 提供丰富的索引类型(列表、树、关键词、向量等)和查询引擎,支持复杂的检索策略。与 LangChain 的通用框架不同,LlamaIndex 更专注于 RAG 场景,提供更深度的数据集成能力。

02

安装

LlamaIndex 通过 pip 安装,核心包提供基础功能。按需安装额外的集成包。推荐 Python 3.9+。

bash
# Install core package
pip install llama-index

# Install specific integrations
pip install llama-index-llms-openai
pip install llama-index-embeddings-openai
pip install llama-index-vector-stores-chroma

# Or install the full package
pip install llama-index[full]
03

文档

Document 是 LlamaIndex 的基本数据单元。它支持多种数据源:PDF、网页、数据库、API 等。文档被切分为 Node 用于索引和检索。LlamaIndex 提供 100+ 数据加载器。

bash
from llama_index.core import Document, SimpleDirectoryReader

# Load from files
documents = SimpleDirectoryReader("./data").load_data()

# Create manually
doc = Document(text="This is document content", metadata={"source": "manual"})

# Load from web pages
from llama_index.readers.web import SimpleWebPageReader
docs = SimpleWebPageReader().load_data(["https://example.com"])
04

索引

Index 是 LlamaIndex 的核心,组织文档以实现高效检索。它支持多种索引类型:VectorStoreIndex、SummaryIndex、KeywordTableIndex、KnowledgeGraphIndex 等。每种索引类型适用于不同的查询场景。

bash
from llama_index.core import VectorStoreIndex, SummaryIndex

# Vector index (most commonly used)
vector_index = VectorStoreIndex.from_documents(documents)

# Summary index
summary_index = SummaryIndex.from_documents(documents)

# Persist
vector_index.storage_context.persist(persist_dir="./storage")
05

查询引擎

Query Engine 是将用户查询转换为答案的查询接口。它支持多种查询模式:检索、摘要、路由、子查询等。可以组合多个查询引擎实现复杂逻辑。

bash
from llama_index.core.query_engine import RetrieverQueryEngine

# Basic query engine
query_engine = vector_index.as_query_engine()
response = query_engine.query("What is RAG?")

# Streaming response
streaming_engine = vector_index.as_query_engine(streaming=True)
response = streaming_engine.query("question")
for text in response.response_gen:
    print(text, end="")
06

RAG 流水线

LlamaIndex 提供完整的 RAG 流水线构建能力。包括数据摄取、分块、嵌入、索引、检索、重排、生成等步骤。支持句子窗口检索、自动合并检索、混合检索等高级 RAG 技术。

bash
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core import VectorStoreIndex

# Advanced RAG configuration
splitter = SentenceSplitter(chunk_size=1024, chunk_overlap=20)
nodes = splitter.get_nodes_from_documents(documents)

index = VectorStoreIndex(nodes)
query_engine = index.as_query_engine(
    similarity_top_k=5,
    response_mode="compact"
)
07

Agent

LlamaIndex 提供 Agent 功能,支持自主决策和工具调用。Agent 可以将查询引擎作为工具使用,实现复杂推理和检索。支持 OpenAI Agent、ReAct Agent 等类型。

bash
from llama_index.core.agent import ReActAgent
from llama_index.core.tools import QueryEngineTool

# Use query engine as a tool
tool = QueryEngineTool.from_defaults(
    query_engine=query_engine,
    name="knowledge_base",
    description="Query the knowledge base for information"
)

agent = ReActAgent.from_tools([tool], llm=llm)
response = agent.chat("Answer the question based on the documents")
08

工具

LlamaIndex 提供丰富的工具,包括查询引擎工具、函数工具、Spec 工具等。工具可以组合构建强大的 Agent。支持自定义工具开发。

bash
from llama_index.core.tools import FunctionTool

# Custom function tool
def search_database(query: str) -> str:
    """Search the database."""
    # Implement search logic
    return results

tool = FunctionTool.from_defaults(fn=search_database)

# Use the tool
agent = ReActAgent.from_tools([tool], llm=llm)
09

评估

LlamaIndex 提供评估框架来评估 RAG 系统的质量。支持评估检索相关性、答案准确性、忠实度等。评估有助于优化 RAG 流水线的参数和配置。

bash
from llama_index.core.evaluation import FaithfulnessEvaluator, RelevancyEvaluator

faithfulness = FaithfulnessEvaluator(llm=llm)
relevancy = RelevancyEvaluator(llm=llm)

# Evaluate the answer
faith_result = faithfulness.evaluate_response(response=response)
rel_result = relevancy.evaluate_response(query="question", response=response)
10

部署

LlamaIndex 应用可部署为 Web 服务、API、CLI 工具等。支持与 FastAPI 和 Flask 集成。LlamaCloud 提供托管服务,简化部署和扩展。也支持部署到云平台。

bash
# FastAPI deployment
from fastapi import FastAPI
from llama_index.core import VectorStoreIndex

app = FastAPI()
index = VectorStoreIndex.load_from_persist_dir("./storage")
engine = index.as_query_engine()

@app.post("/query")
async def query(question: str):
    response = engine.query(question)
    return {"answer": str(response)}

Persist the index to disk so the API can reload it without rebuilding on every start.

11

Configuration

LlamaIndex is configured in Python with integration packages. Set LLM API keys as environment variables and pick an embedding model + vector store. Index type (VectorStoreIndex, SummaryIndex, etc.) and query engine settings (similarity_top_k, response_mode) are the main tuning knobs. Storage persists to a directory via storage_context.

bash
# .env
OPENAI_API_KEY=your-key

# Python: model + embedding + vector store
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import VectorStoreIndex, Settings

Settings.llm = OpenAI(model="gpt-4o")
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")

# Index with tuning
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine(
    similarity_top_k=5,
    response_mode="compact"
)

# Persist
index.storage_context.persist(persist_dir="./storage")

Tune similarity_top_k and chunk_size together—larger chunks need fewer retrieved nodes.

12

FAQ

Common questions cover LlamaIndex vs LangChain, model choice, cost, RAG tuning, and index types. LlamaIndex is RAG-focused with deeper data ingestion tooling; LangChain is a more general framework. VectorStoreIndex is the default for most RAG apps.

bash
Q: LlamaIndex vs LangChain?
A: LlamaIndex is RAG-focused with richer indexing/retrieval abstractions;
   LangChain is a general LLM framework. They can be used together.

Q: Which index type should I use?
A: VectorStoreIndex for most RAG; SummaryIndex for whole-doc summaries;
   KnowledgeGraphIndex for relationship-heavy data.

Q: How do I cut costs?
A: Use a smaller embedding model, lower similarity_top_k, and cache
   embeddings in a persisted vector store.

Q: How do I improve RAG quality?
A: Tune chunk_size/chunk_overlap, use sentence-window or auto-merging
   retrieval, and add a reranker.

Q: Which models are supported?
A: Any provider with an integration package: OpenAI, Anthropic, Ollama,
   Google, and more.

For better RAG, add a reranker (e.g. Cohere Rerank) on top of vector retrieval.

Ready to try LlamaIndex?

Visit the official site for the latest version and full documentation.

Visit LlamaIndex