You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LlamaIndex 0.6.5无法导入load_index_from_storage:解决方法及替代方案

解决LlamaIndex 0.6.5中load_index_from_storage导入错误的方案

错误原因

在LlamaIndex 0.6.5版本中,load_index_from_storage函数并未放在根模块llama_index下,而是归属于llama_index.indices.loading子模块,直接从根模块导入会触发ImportError。

正确导入方式

将导入语句修改为从对应子模块导入:

from llama_index.indices.loading import load_index_from_storage

修改后的完整示例代码

from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader, StorageContext
from llama_index.indices.loading import load_index_from_storage
from IPython.display import Markdown, display
import openai
import os
import sys

os.environ['OPENAI_API_KEY']= "sk-***"


documents = SimpleDirectoryReader('data').load_data()
index = GPTVectorStoreIndex.from_documents(documents)

# save index to disk
index.set_index_id("vector_index")
index.storage_context.persist('storage')

# rebuild storage context
storage_context = StorageContext.from_defaults(persist_dir='storage')
# load index
index = load_index_from_storage(storage_context, index_id="vector_index")

# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
print(response)

额外说明

若后续升级至LlamaIndex 0.7.x及以上版本,该函数会被迁移至根模块llama_index下,此时可直接使用原示例中的根模块导入方式。

内容的提问来源于stack exchange,提问作者David Seo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 15:35:34