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

Llama Index无法导入SubQuestionQueryEngine等类,如何解决?

问题原因与解决方法

核心原因

你遇到的导入错误本质是Llama Index版本不匹配:官方文档基于最新版本编写,但你本地安装的版本较旧,缺少SubQuestionQueryEngine和ToolMetadata类,或者这些类的导入路径与文档不一致。

解决步骤

1. 检查当前安装版本

执行以下命令查看本地Llama Index版本:

pip show llama-index

如果版本低于0.8.0,大概率会出现这类导入问题——SubQuestionQueryEngine等功能是在较新版本中加入或调整了导入路径的。

2. 升级到最新稳定版

执行升级命令:

pip install --upgrade llama-index

如果你的环境依赖特定版本,可以指定版本号(比如安装最新的稳定版):

pip install llama-index==0.10.40

3. 调整导入路径(针对模块化架构版本)

如果升级后仍出现导入问题,可能是因为Llama Index采用了模块化拆分(核心功能移至llama-index-core包),此时需要调整导入语句为:

from llama_index.core import GPTVectorStoreIndex, SimpleDirectoryReader
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.query_engine import SubQuestionQueryEngine

4. 验证安装与导入

升级/调整导入后,运行简单的测试代码确认问题解决:

try:
    from llama_index.core.tools import QueryEngineTool, ToolMetadata
    from llama_index.core.query_engine import SubQuestionQueryEngine
    print("导入成功")
except ImportError as e:
    print(f"仍有导入错误:{e}")

内容的提问来源于stack exchange,提问作者Thauany Moedano

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 23:25:11