当前位置:首页 > 技术 > 正文内容

LlamaIndex存储架构核心解析

访客 技术 2026年6月7日 1

存储系统概览

LlamaIndex的存储架构由多个可插拔模块构成,分别管理特定类型的数据组件。这些模块共同为语言模型提供结构化数据底座:

  • 文档存储(Document Store)
  • 索引存储(Index Store)
  • 向量存储(Vector Store)
  • 属性图存储(Property Graph Store)
  • 聊天记录存储(Chat Message Store)

通过统一的StorageContext进行集中管理,支持从内存存储到分布式数据库的灵活扩展。

存储组件关系图

LlamaIndex存储架构总览

                   ┌───────────────────────┐
                   │       原始数据         │
                   │ (PDF/HTML/DB/API等)   │
                   └───────────┬───────────┘
                               │
                               ▼
                    ┌─────────────────────┐
                    │     文档加载器       │
                    └───────────┬─────────┘
                               │
                               ▼
                      ┌─────────────────┐
                      │    节点解析器    │
                      └─────────┬───────┘
                                │
                                ▼
                        ┌──────────────┐
                        │   文本节点    │
                        │ (内容+元数据) │
                        └───────┬──────┘
                                │
                  ┌─────────────┴───────────────┐
                  │       StorageContext         │
                  │   (统一管理存储组件)         │
                  └─────────────┬───────────────┘
                                │
    ┌───────────────────────────┼─────────────────────────┐
    ▼                           ▼                         ▼
┌─────────────┐          ┌─────────────┐           ┌─────────────┐
│ 文档存储     │          │ 索引存储     │           │ 向量存储     │
│ (节点管理)   │          │ (元数据记录) │           │ (嵌入检索)   │
└─────────────┘          └─────────────┘           └─────────────┘
                                │
                                ▼
                        ┌───────────────┐
                        │   图存储       │
                        │ (知识图谱)     │
                        └───────────────┘
                                │
                                ▼
                         ┌─────────────┐
                         │ 聊天存储     │
                         │ (会话记录)   │
                         └─────────────┘

核心存储组件解析

文档存储(Document Store)

管理文档解析后生成的文本节点(Node),每个节点包含:

  • 文本内容片段
  • 元信息(来源/页码等)
  • 向量嵌入(索引构建后注入)
from llama_index.core import Document
from llama_index.storage.docstore import SimpleDocumentStore
from llama_index.core.node_parser import TokenTextSplitter

data_docs = Document.load_from_dir("dataset")
text_splitter = TokenTextSplitter()
text_nodes = text_splitter.process_docs(data_docs)

node_storage = SimpleDocumentStore()
for node in text_nodes:
    node_storage.add_node(node)

storage_ctx = StorageContext(doc_store=node_storage)

索引存储(Index Store)

维护索引结构元数据,支持索引快速恢复:

  • 向量索引的倒排表
  • 树形索引的层级关系
  • 查询配置参数
from llama_index.storage.index_store import RedisIndexStore
from llama_index.core import VectorStoreIndex

idx_storage = RedisIndexStore(host="localhost")
storage_ctx = StorageContext(index_store=idx_storage)

vector_index = VectorStoreIndex(text_nodes, storage_context=storage_ctx)
storage_ctx.persist("./index_data")

向量存储(Vector Store)

存储文本嵌入并提供相似性检索:

解决方案特性
FAISS单机高性能
Chroma轻量易集成
Milvus分布式架构
QdrantRust高性能
Pinecone云托管服务
from llama_index.vector_stores.pinecone import PineconeVectorStore
from llama_index.core import VectorStoreIndex

vector_db = PineconeVectorStore(index_name="tech_docs")
storage_ctx = StorageContext(vector_store=vector_db)

search_index = VectorStoreIndex(text_nodes, storage_context=storage_ctx)
result = search_index.as_query_engine().query("存储架构核心组件?")
print(result.response)

属性图存储(Property Graph Store)

管理知识图谱结构数据:

  • 实体与关系
  • 属性键值对
from llama_index.graph_stores.neo4j import Neo4jGraphStore
from llama_index.indices.property_graph import KnowledgeGraphIndex

graph_db = Neo4jGraphStore(uri="bolt://localhost", username="neo4j")
kg_index = KnowledgeGraphIndex.from_docs(
    data_docs,
    property_graph_store=graph_db
)

connections = kg_index.fetch_relations("向量存储")
print(connections)

聊天记录存储(Chat Message Store)

持久化多轮对话上下文:

  • 会话线程管理
  • 历史消息存储
  • 长期记忆支持
from llama_index.storage.chat_store import MongoDBMessageStore
from llama_index.core.memory import ConversationBuffer

chat_history = MongoDBMessageStore(collection="dialogs")
memory = ConversationBuffer(
    token_capacity=4096, 
    chat_store=chat_history
)

memory.record("user", "解释图存储作用")
memory.record("assistant", "图存储管理实体关系...")
print(memory.get_recent(3))

存储组件协同工作机制

组件通过StorageContext实现数据联动:

  1. 文档解析节点存入DocumentStore
  2. 向量嵌入写入VectorStore
  3. 索引元数据保存至IndexStore
  4. 图结构持久化到GraphStore
  5. 对话记录存储于ChatStore
组件协同示意图

实践示例:端到端流程

# 初始化存储上下文
storage_ctx = StorageContext()

# 创建向量索引
main_index = VectorStoreIndex.from_documents(
    data_docs,
    storage_context=storage_ctx
)

# 持久化存储
storage_ctx.persist("./storage_db")

# 恢复索引
from llama_index.core import load_index_from_storage
loaded_index = load_index_from_storage(storage_ctx)

# 执行查询
query_result = loaded_index.as_query_engine().query("存储持久化方法?")
print(query_result)

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Mac 安装 Node.js 指南

方法一:通过官网安装包(最简单,适合初学者)如果你只是想快速安装并开始使用,这是最直接的方法。访问 Node.js 官网。页面会显示两个版本:LTS (Recommended For Most Users):长期支持版,最稳定。建议选这个。Current:最新特性版,包含最新功能但可能不够稳定。下载 .pkg 安装包并运行。按照安装向导点击“下一步”即可完成。方法二:使用 Homebrew 安装(...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。