cannot import name 'ChatPromptTemplate' from 'langchain_core' (D:\software\Pytho...

2025年09月25日 16:49 响应: 19.42秒 状态: success

🚨 错误信息

Traceback (most recent call last): File "D:\新建文件夹\langchain-base\langchain_learn.py", line 8, in <module> from langchain_core import ChatPromptTemplate ImportError: cannot import name 'ChatPromptTemplate' from 'langchain_core' (D:\software\Python\Lib\site-packages\langchain_core\__init__.py)

🤖 AI解决方案

ImportError: cannot import name 'ChatPromptTemplate' from 'langchain_core' (D:\software\Python\Lib\site-packages\langchain_core\__init__.py)

错误翻译:

ImportError: 无法从 'langchain_core' 导入名称 'ChatPromptTemplate' ('D:\software\Python\Lib\site-packages\langchain_core\__init__.py' 中的 'langchain_core')

原因分析:

ChatPromptTemplate 并非直接从 langchain_core 导入,而是位于 langchain_core.prompts 模块下。

解决方案:

修改导入语句:

from langchain_core.prompts import ChatPromptTemplate

预防措施:

  • 查阅官方文档: 在导入新模块或类时,优先查阅 LangChain 的官方文档,了解正确的导入路径。
  • 使用IDE的代码提示: 利用集成开发环境 (IDE) 的代码自动补全和提示功能,可以帮助你找到正确的模块和类名。