LlamaIndex 存储体系深度解析
一个高效、可扩展的存储系统,并不只是保存数据那么简单。它需要解决文档分块、结构化表达、索引元数据记录、向量检索、知识图谱存储以及对话历史管理等问题,从而为模型提供可重复、可扩展的认知基础。本篇将对 LlamaIndex 的存储体系进行系统拆解,从架构逻辑、使用方式到扩展模式进行全面介绍,并结合完整的代码示例和 ASCII 架构图,帮助你理解“LLM 数据基础设施”应当如何构建。
目录
前言
随着大模型在企业级知识管理、智能搜索、智能体编排等场景的落地加速,如何让外部数据以结构化、可管理、可扩展的方式被模型消费,成为一个关键问题。LlamaIndex 作为业内领先的 LLM 数据框架,提供了丰富的数据加载、索引、检索与智能体构建能力。然而,许多人往往忽略了支撑这一切的“底层基座”——存储体系。
一个高效、可扩展的存储系统,并不只是保存数据那么简单。它需要解决文档分块、结构化表达、索引元数据记录、向量检索、知识图谱存储以及对话历史管理等问题,从而为模型提供可重复、可扩展的认知基础。
本篇将对 LlamaIndex 的存储体系进行系统拆解,从架构逻辑、使用方式到扩展模式进行全面介绍,并结合完整的代码示例和 ASCII 架构图,帮助你理解“LLM 数据基础设施”应当如何构建。
1 存储体系概览
LlamaIndex 的存储体系由多个可插拔的子模块构成,每个模块负责管理特定类型的数据。在整个索引与检索流程中,它们共同组成了模型的数据底座。
LlamaIndex 支持的核心存储类型如下:
- 文档存储(Document Store)
- 索引存储(Index Store)
- 向量存储(Vector Store)
- 属性图存储(Property Graph Store)
- 聊天记录存储(Chat Message Store)
它们由一个统一的 StorageContext 进行管理,使开发者既能使用内置的轻量级存储,也能替换为企业级基础设施。
2 LlamaIndex 存储总体架构
理解存储逻辑的最佳方式,是看它在整个数据流中的位置。以下 ASCII 架构图清晰展示了“数据加载 → 节点解析 → 存储 → 索引 → 查询”的全流程。
┌───────────────────────────┐
│ Input Data │
│ (PDF, HTML, DB, API, etc.) │
└─────────────┬─────────────┘
│
▼
┌────────────────────────┐
│ Document Loader │
└─────────────┬──────────┘
│
▼
┌──────────────────┐
│ Node Parser │
└─────────┬────────┘
│
▼
┌──────────────┐
│ Nodes │
│ (text+meta) │
└───────┬──────┘
│
┌─────────────┴─────────────────┐
│ StorageContext │
│ (存储总线,管理所有组件) │
└─────────────┬─────────────────┘
│
┌───────────────────────────┼──────────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌────────────────┐
│ DocumentStore │ │ IndexStore │ │ VectorStore │
│ 保存 Node │ │ 保存索引元数据 │ │ 保存嵌入/检索 │
└───────────────┘ └───────────────┘ └────────────────┘
│
▼
┌─────────────────┐
│ GraphStore │
│ 知识图谱存储 │
└─────────────────┘
│
▼
┌─────────────┐
│ ChatStore │
│ 聊天记录存储 │
└─────────────┘
3 核心存储组件详解
3.1 文档存储(Document Store)
文档存储负责保存文档加载器与解析器生成的 Node。每个 Node 是 LlamaIndex 的“最小知识单位”,包含:
- 拆分后的文本块
- 元信息(source、page 等)
- Embedding(构建索引后注入)
开发者可以使用默认的内存存储,也可以自定义数据库方案。
示例代码
from llama_index.core import Document, StorageContext, SimpleDirectoryReader
from llama_index.core.node_parser import SimpleNodeParser
from llama_index.storage.docstore import SimpleDocumentStore
documents = SimpleDirectoryReader("data").load_data()
parser = SimpleNodeParser()
nodes = parser.get_nodes_from_documents(documents)
doc_store = SimpleDocumentStore()
for node in nodes:
doc_store.add_document(node)
storage_context = StorageContext.from_defaults(docstore=doc_store)
3.2 索引存储(Index Store)
索引存储用于记录索引结构本身的元数据,例如:
- 向量索引的倒排表
- 树索引的父子节点关系
- 查询配置与内部状态
其最大作用是 恢复索引,而无需重新构建。
示例代码
from llama_index.storage.index_store import SimpleIndexStore
from llama_index.core import VectorStoreIndex, StorageContext
index_store = SimpleIndexStore()
storage_context = StorageContext.from_defaults(index_store=index_store)
index = VectorStoreIndex(nodes, storage_context=storage_context)
storage_context.persist("./storage")
3.3 向量存储(Vector Store)
向量存储用于保存 Node 的 Embedding,并提供高效的相似度检索能力。LlamaIndex 支持多种向量库,包括:
| 向量库 | 特性 |
|---|---|
| FAISS | 单机性能强、适合原型开发 |
| Chroma | Python 生态友好、轻量易用 |
| Milvus | 企业级分布式向量数据库 |
| Qdrant | Rust 实现、高性能向量检索 |
| Pinecone | 云托管、高可用 |
示例:Chroma 向量存储
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import VectorStoreIndex, StorageContext
vector_store = ChromaVectorStore(persist_dir="chroma_store")
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex(nodes, storage_context=storage_context)
query_engine = index.as_query_engine()
response = query_engine.query("介绍一下 LlamaIndex 的存储体系?")
print(response)
3.4 属性图存储(Property Graph Store)
当索引需要构建知识图谱时,图存储扮演重要角色。它保存:
- 实体(Entity)
- 关系(Relation)
- 属性(Properties)
适用于结构化知识问答、关系推理、智能体规划等场景。
代码示例
from llama_index.graph_stores.simple import SimplePropertyGraphStore
from llama_index.indices.property_graph import PropertyGraphIndex
graph_store = SimplePropertyGraphStore()
index = PropertyGraphIndex.from_documents(
documents,
property_graph_store=graph_store
)
relations = index.get_relations("OpenAI")
print(relations)
3.5 聊天记录存储(Chat Message Store)
智能体与用户之间的上下文是需要持久保存的。ChatStore 用于管理对话内容,包括:
- 多轮对话消息
- 会话线程管理
- 搭配 Memory 构建长期记忆
示例代码
from llama_index.core.memory import ChatMemoryBuffer
from llama_index.storage.chat_store import SimpleChatStore
chat_store = SimpleChatStore()
memory = ChatMemoryBuffer.from_defaults(
token_limit=2000,
chat_store=chat_store
)
memory.put("user", "你好,请介绍 LlamaIndex 存储?")
memory.put("assistant", "LlamaIndex 提供多种存储组件……")
print(memory.get())
4 存储组件如何协同工作?
存储组件并不是独立存在的,而是通过 StorageContext 组成统一的数据层。流程如下:
- 文档加载后转换为 Node,并写入 DocumentStore
- Node 嵌入在向量索引构建时写入 VectorStore
- 索引的元信息保存在 IndexStore
- 如启用图索引,则结构写入 GraphStore
- 若是聊天型应用,则会话写入 ChatStore
由于它们都由 StorageContext 管理,因此可以实现:
- 自定义替换(如从内存替换为数据库)
- 持久化与恢复
- 分布式分片
- 跨进程共享
这种架构使得 LlamaIndex 既能用于轻量级应用,也能扩展到企业级平台。
5 衔接实践:简单示例流程
下面展示一个端到端的流程代码(简化版):
# 1. 构建存储上下文
storage_context = StorageContext.from_defaults()
# 2. 构建向量索引
index = VectorStoreIndex.from_documents(
documents,
storage_context=storage_context
)
# 3. 持久化
storage_context.persist("./storage")
# 4. 恢复(另一个进程或启动后)
from llama_index.core import load_index_from_storage
restored_index = load_index_from_storage(storage_context)
# 5. 查询
query_engine = restored_index.as_query_engine()
print(query_engine.query("文档中提到的关键概念是什么?"))
6 总结
LlamaIndex 的存储体系是构建 LLM 应用的关键基础设施,它解决了:
- 如何以结构化方式管理数据
- 如何高效进行检索与推理
- 如何让索引能被复用、迁移与扩展
- 如何让智能体具备长期记忆与知识体系
通过可替换的存储组件,LlamaIndex 既能作为轻量级框架供个人使用,也能无缝扩展到大型企业知识平台、智能客服、RAG 系统与智能体架构中。
存储体系的强大之处并非在于某个组件,而在于它们之间的协同与整体性设计。在未来的 LLM 架构演化中,数据与存储的分层设计将成为行业标准,而 LlamaIndex 已经走在前面。
参考资料
- LlamaIndex 官方文档
https://docs.llamaindex.ai/ - Chroma 官方文档
https://docs.trychroma.com/ - Milvus 向量数据库
https://milvus.io/ - Qdrant Vector DB
https://qdrant.tech/ - Pinecone 官方文档
https://docs.pinecone.io/
更多推荐



所有评论(0)