资讯详情

如何用 fsspec 把 LlamaIndex 索引持久化到 S3 兼容远程桶并重新加载

📅 2026/9/10 13:05:36 | 华诺云谱 👁 阅读
如何用 fsspec 把 LlamaIndex 索引持久化到 S3 兼容远程桶并重新加载
如何用 fsspec 把 LlamaIndex 索引持久化到 S3 兼容远程桶并重新加载【免费下载链接】llama_indexLlamaIndex is the leading document agent and OCR platform项目地址: https://gitcode.com/GitHub_Trending/ll/llama_indexLlamaIndex 默认把索引数据存在内存中调用storage_context.persist()时默认写入本地文件系统不指定persist_dir时落到./storage。当你需要把索引持久化到 S3 兼容的远程对象存储AWS S3、Cloudflare R2 等并之后重新加载时可以传入一个fsspec.AbstractFileSystem对象来覆盖默认本地文件系统。本文基于仓库文档 save_load.md 与 storing/index.md 说明这条完整路径构建索引 → 实例化 S3 文件系统 → 用fs参数持久化 → 从远程桶重新加载。文档已确认支持的 fsspec 后端包括本地文件系统、AWS S3 和 Cloudflare R2任何 fsspec 支持的存储后端都可以按同样方式接入。准备工作在写远程持久化代码之前索引本身仍然按常规方式构建。文档中的示例从本地目录加载文档并构建向量索引import dotenv import s3fs import os dotenv.load_dotenv(../../../.env) # load documents documents SimpleDirectoryReader( ../../../examples/paul_graham_essay/data/ ).load_data() print(len(documents)) index VectorStoreIndex.from_documents(documents)这里有两处需要按你的环境替换dotenv.load_dotenv(../../../.env)文档中的相对路径是示例文档自身的目录位置应替换为你自己.env文件的实际路径或者像其他文档示例一样直接load_dotenv()。python-dotenv是加载它的依赖见 agent 入门示例 中的安装命令pip install python-dotenv。SimpleDirectoryReader的路径替换为你要建索引的本地数据目录。.env文件中需要提供代码后面读取的三个环境变量以文档给出的 R2 示例为准AWS_ACCESS_KEY_ID你的访问密钥ID AWS_SECRET_ACCESS_KEY你的密钥 R2_ACCOUNT_ID你的R2账户ID实例化 S3 文件系统文档给出的示例面向 Cloudflare R2通过 S3 兼容 endpoint 访问。实例化S3FileSystem并加上凭证断言# set up s3fs AWS_KEY os.environ[AWS_ACCESS_KEY_ID] AWS_SECRET os.environ[AWS_SECRET_ACCESS_KEY] R2_ACCOUNT_ID os.environ[R2_ACCOUNT_ID] assert AWS_KEY is not None and AWS_KEY ! s3 s3fs.S3FileSystem( keyAWS_KEY, secretAWS_SECRET, endpoint_urlfhttps://{R2_ACCOUNT_ID}.r2.cloudflarestorage.com, s3_additional_kwargs{ACL: public-read}, )参数说明仅依据文档出现内容endpoint_url文档中固定为 R2 的 S3 兼容地址模板。若你用的是 AWS S3 或其他 S3 兼容服务需要把 endpoint 换成对应服务的地址文档没有给出更多配置细节。s3_additional_kwargs{ACL: public-read}示例中对对象设置的附加 S3 参数直接照抄示例即可如果你的桶不允许 public-read ACL请去掉或调整该参数。assert语句只是防止凭证为空时静默失败可按需保留。持久化索引到远程桶persist_dir在文档示例中采用的是{bucket_name}/{index_name}的形式# If youre using 2 indexes with the same StorageContext, # run this to save the index to remote blob storage index.set_index_id(vector_index) # persist index to s3 s3_bucket_name llama-index/storage_demo # {bucket_name}/{index_name} index.storage_context.persist(persist_dirs3_bucket_name, fss3)两点边界需要注意index.set_index_id(vector_index)只在同一StorageContext下保存 2 个及以上索引时才必要单索引场景可以省略但仍建议设置方便重载时按 id 定位。当传入了fs时persist()内部会把persist_dir按字符串路径拼接各存储文件名docstore、index store、graph store、vector store 等分别落一个文件这一点在 storage_context.py 的persist实现中可以看到且源码注释标明fs模式下不支持 Windows 路径处理。从远程桶重新加载加载时用同一个persist_dir和同一个fs对象重建StorageContext再按index_id取出索引# load index from s3 index_from_s3 load_index_from_storage( StorageContext.from_defaults(persist_dirs3_bucket_name, fss3), index_idvector_index, )load_index_from_storage等便利函数从llama_index.core导入load_index_from_storage、load_indices_from_storage、load_graph_from_storage。如果存储上下文中只有一个索引可以不传index_id多个索引时按 id 精确加载# load a single index # need to specify index_id if multiple indexes are persisted to the same directory index load_index_from_storage(storage_context, index_idindex_id) # dont need to specify index_id if theres only one index in storage context index load_index_from_storage(storage_context) # load multiple indices indices load_indices_from_storage(storage_context) # loads all indices indices load_indices_from_storage( storage_context, index_ids[index_id1, ...] ) # loads specific indices结果判断与限制成功的判断依据persist之后远程桶的llama-index/storage_demo/路径下会写入 docstore、index store、graph store、vector store 等各自的文件load_index_from_storage能返回可用索引对象文档未给出额外的查询验证步骤以加载调用无异常、得到 index 对象为准。不传fs的默认行为不传文件系统时 LlamaIndex 一律按本地文件系统处理远程桶路径不会被识别。自定义后端存储的注意事项文档说明若使用MongoDB等默认自行持久化的替代存储后端storage_context.persist()将不产生任何写入这条 fsspec 远程路径主要针对默认的 Simple 系列存储。凭证与网络代码依赖.env中的AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEYR2 场景下为 R2 凭证和R2_ACCOUNT_IDassert失败或S3FileSystem连接失败通常意味着凭证缺失或 endpoint 配置与你实际使用的服务不匹配。多索引多个索引可以持久化到同一个目录/桶前缀下但加载时必须自行跟踪index_id。如果后续想进一步定制存储组件docstore、index store、vector store 的替换可以继续查看 Storing 模块指南 中的 Customization 与 Vector Stores 章节。【免费下载链接】llama_indexLlamaIndex is the leading document agent and OCR platform项目地址: https://gitcode.com/GitHub_Trending/ll/llama_index创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
📝

华诺云谱内容团队

资深建站顾问 · 行业研究员

10年+企业数字化服务经验,专注智能建站、SEO优化与品牌营销,持续输出建站技巧、行业洞察与营销干货,已帮助5000+企业实现数字化增长。

你可能需要的服务

订阅华诺云谱资讯周报

每周一封,精选建站技巧、SEO与营销干货,直达邮箱。已有 8,000+ 企业主订阅,助你少走弯路。