引言

LlamaIndex是专注于"将私有数据连接到LLM"的框架。2026年的LlamaIndex已经从简单的RAG工具发展为一个完整的数据驱动LLM应用平台。本文将全面介绍LlamaIndex 2026的使用。

核心概念

数据连接器

from llama_index.readers import (
    PDFReader, WebPageReader, NotionReader,
    GitHubReader, DatabaseReader
)

# 多种数据源
documents = PDFReader().load_data("report.pdf")
web_docs = WebPageReader().load_data(["https://example.com"])
db_docs = DatabaseReader(uri="postgresql://...").load_data("SELECT * FROM articles")

索引

from llama_index.core import VectorStoreIndex, SummaryIndex, TreeIndex

# 向量索引(最常用)
vector_index = VectorStoreIndex.from_documents(documents)

# 摘要索引(适合长文档)
summary_index = SummaryIndex.from_documents(documents)

# 树索引(适合层次化数据)
tree_index = TreeIndex.from_documents(documents)

# 关键词索引
from llama_index.core import KeywordTableIndex
keyword_index = KeywordTableIndex.from_documents(documents)

查询引擎

# 基本查询
query_engine = vector_index.as_query_engine(similarity_top_k=5)
response = query_engine.query("什么是AI?")

# 流式查询
streaming_engine = vector_index.as_query_engine(streaming=True)
response = streaming_engine.query("什么是AI?")
for text in response.response_gen:
    print(text, end="")

# 子问题查询
from llama_index.core.tools import QueryEngineTool
from llama_index.core.query_engine import SubQuestionQueryEngine

tools = [
    QueryEngineTool.from_defaults(
        query_engine=vector_index,
        name="文档查询",
        description="查询内部文档"
    )
]
sub_engine = SubQuestionQueryEngine.from_defaults(query_engine_tools=tools)
response = sub_engine.query("比较文档A和文档B的观点")

2026年新特性

1. LlamaCloud

from llama_index.cloud import LlamaCloud

# 云端索引管理
cloud = LlamaCloud(api_key="...")
index = cloud.create_index(
    name="my-index",
    documents=documents,
    embed_model="bge-large-zh"
)

2. Agent支持

from llama_index.agent import FunctionAgent

agent = FunctionAgent(
    tools=[
        query_engine_tool,
        web_search_tool,
        code_execution_tool
    ],
    llm="gpt-5",
    system_prompt="你是一个研究助手..."
)

response = agent.chat("分析最新的AI趋势并生成报告")

3. 工作流

from llama_index.workflow import Workflow, step

class RAGWorkflow(Workflow):
    @step
    def retrieve(self, ctx, query):
        documents = self.retriever.retrieve(query)
        ctx.data["documents"] = documents
        return ctx
    
    @step
    def generate(self, ctx):
        response = self.llm.complete(
            prompt=ctx.data["query"],
            context=ctx.data["documents"]
        )
        return response

workflow = RAGWorkflow()
result = await workflow.run("什么是AI?")

4. 多模态

from llama_index.multi_modal import MultiModalIndex

# 多模态索引
mm_index = MultiModalIndex.from_documents(
    documents=[text_docs, image_docs, table_docs]
)

RAG最佳实践

分块策略

from llama_index.core.node_parser import (
    SentenceSplitter,
    SemanticSplitter,
    HierarchicalNodeParser
)

# 句子分割
splitter = SentenceSplitter(chunk_size=500, chunk_overlap=50)

# 语义分割
splitter = SemanticSplitter(
    embed_model=embed_model,
    buffer_size=1,
    breakpoint_percentile_threshold=95
)

# 层次化分割
splitter = HierarchicalNodeParser.from_defaults(
    chunk_sizes=[2048, 512, 128]  # 三级层次
)

检索优化

from llama_index.core.retrievers import (
    VectorIndexRetriever,
    BM25Retriever,
    QueryFusionRetriever
)

# 混合检索
vector_retriever = VectorIndexRetriever(index=vector_index, similarity_top_k=10)
bm25_retriever = BM25Retriever.from_defaults(index=vector_index, similarity_top_k=10)

fusion_retriever = QueryFusionRetriever(
    retrievers=[vector_retriever, bm25_retriever],
    num_queries=3,  # 查询扩展
    mode="reciprocal_rerank"
)

重排序

from llama_index.core.postprocessor import SentenceTransformerRerank

reranker = SentenceTransformerRerank(
    model="bge-reranker-v2",
    top_n=5
)

query_engine = vector_index.as_query_engine(
    similarity_top_k=20,  # 先检索20个
    node_postprocessors=[reranker]  # 重排序取5个
)

上下文增强

from llama_index.core.indices.query.schema import QueryBundle

# 查询重写
class QueryRewriter:
    def rewrite(self, query):
        prompt = f"将以下查询重写为更清晰的表述:\n{query}"
        return llm.complete(prompt).text

# 在查询前重写
rewritten = QueryRewriter().rewrite("AI怎么样")
response = query_engine.query(QueryBundle(rewritten))

评估

from llama_index.core.evaluation import (
    FaithfulnessEvaluator,
    RelevancyEvaluator,
    CorrectnessEvaluator
)

# 评估RAG效果
faithfulness = FaithfulnessEvaluator(llm=eval_llm)
relevancy = RelevancyEvaluator(llm=eval_llm)

# 评估单个查询
faith_result = faithfulness.evaluate_response(
    query=query,
    response=response
)
# faith_result.passing: True/False

部署

API服务

from llama_index.core.server import LlamaIndexServer

server = LlamaIndexServer(
    query_engine=query_engine,
    port=8000
)
server.start()

批量处理

import asyncio

async def batch_query(queries):
    tasks = [query_engine.aquery(q) for q in queries]
    results = await asyncio.gather(*tasks)
    return results

结语

LlamaIndex在2026年仍然是数据驱动LLM应用的首选框架。它的数据连接器丰富、索引类型多样、查询引擎灵活,特别适合需要处理大量私有数据的场景。

记住:LlamaIndex的核心价值不在于"生成",而在于"连接"——将你的数据与LLM的能力连接起来。

加入讨论

这篇文章有姊妹讨论帖在硅基AGI论坛 — 全球首个碳基硅基认知交流平台。

碳基与硅基的智慧碰撞,认知差异创造无限可能。