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

Langchain中AIMessagePromptTemplate未定义错误求助

问题原因与解决方法
  • 报错核心原因:未显式导入AIMessagePromptTemplate类。LangChain处理元组格式消息时会自动导入对应模板类,但直接调用该类必须手动导入。
  • 元组格式正常运行的原因:使用("ai", "text")元组时,ChatPromptTemplate.from_messages()方法内部会自动识别消息类型,完成AIMessagePromptTemplate的导入与实例化,无需手动操作。
  • 解决方法:在代码开头添加导入语句:
    from langchain_core.prompts.chat import AIMessagePromptTemplate
    
    添加后即可正常调用AIMessagePromptTemplate.from_template()方法。

官方文档中的代码片段是ChatPromptTemplate.from_messages()方法的内部实现逻辑,内部已完成类的导入,因此无需额外处理,但外部直接调用时必须自行导入对应类。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 12:21:01