(msg) ValueError: Invalid input type <class 'langchain_core.messages.human.Human...
🚨 错误信息
Traceback (most recent call last):
File "D:\新建文件夹\langchain-base\Chatbot.py", line 10, in <module>
model.invoke(HumanMessage(content="Hello, I'm Niki"))
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\Python\Lib\site-packages\langchain_core\language_models\chat_models.py", line 396, in invoke
[self._convert_input(input)],
~~~~~~~~~~~~~~~~~~~^^^^^^^
File "D:\software\Python\Lib\site-packages\langchain_core\language_models\chat_models.py", line 381, in _convert_input
raise ValueError(msg)
ValueError: Invalid input type <class 'langchain_core.messages.human.HumanMessage'>. Must be a PromptValue, str, or list of BaseMessages.
🤖 AI解决方案
ValueError: Invalid input type . Must be a PromptValue, str, or list of BaseMessages.
中文翻译:
ValueError: 输入类型无效 。必须是 PromptValue、str 或 BaseMessages 列表。
原因分析:model.invoke() 方法期望的输入类型不匹配,它接收到了一个 HumanMessage 对象,而期望的是 PromptValue、str 或 list[BaseMessage]。
解决方案:
将 HumanMessage 对象包装在一个列表中传递给 invoke 方法。
python
from langchain_core.messages import HumanMessage... (你的模型初始化代码)
假设 model 已经正确初始化
model.invoke([HumanMessage(content="Hello, I'm Niki")])
预防措施:
model.invoke() 或 model.stream() 等方法时,务必仔细检查传入参数的类型是否符合方法签名要求。