如LangChain 快速入门所示,我正在尝试以下 Python 代码:
from langchain.prompts.chat import ChatPromptTemplate
template = "You are a helpful assistant that translates {input_language} to {output_language}."
human_template = "{text}"
chat_prompt = ChatPromptTemplate.from_messages([
("system", template),
("human", human_template),
])
chat_prompt.format_messages(input_language="English", output_language="French", text="I love programming.")
Run Code Online (Sandbox Code Playgroud)
但是当我运行上面的代码时,出现以下错误:
Traceback (most recent call last):
File "/home/yser364/Projets/SinappsIrdOpenaiQA/promptWorkout.py", line 6, in <module>
chat_prompt = ChatPromptTemplate.from_messages([
File "/home/yser364/.local/lib/python3.10/site-packages/langchain/prompts/chat.py", line 220, in from_messages
return cls(input_variables=list(input_vars), messages=messages)
File "/home/yser364/.local/lib/python3.10/site-packages/langchain/load/serializable.py", line 64, in __init__
super().__init__(**kwargs)
File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 4 validation errors for ChatPromptTemplate
messages -> 0
value is not a valid dict (type=type_error.dict)
messages -> 0
value is not a valid dict (type=type_error.dict)
messages -> 1
value is not a valid dict (type=type_error.dict)
messages -> 1
value is not a valid dict (type=type_error.dict)
Run Code Online (Sandbox Code Playgroud)
我使用Python 3.10.12。
小智 5
您的示例来自LangChain 快速入门教程的提示模板部分。我没有发现任何差异,所以它应该按给定的方式工作。
我自己尝试了该示例,并使用一个附加循环来输出由以下命令创建的消息chat_prompt.format_messages:
from langchain.prompts.chat import ChatPromptTemplate
template = "You are a helpful assistant that translates {input_language} to {output_language}."
human_template = "{text}"
chat_prompt = ChatPromptTemplate.from_messages([
("system", template),
("human", human_template),
])
messages = chat_prompt.format_messages(input_language="English", output_language="French", text="I love programming.")
for message in messages:
print(message.__repr__())
Run Code Online (Sandbox Code Playgroud)
该示例运行正常,没有任何错误。结果与教程中显示的结果非常相似,但并不完全相同:
SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={})
HumanMessage(content='I love programming.', additional_kwargs={}, example=False)
Run Code Online (Sandbox Code Playgroud)
我使用Python 3.9.5和LangChain 0.0.300(这是PyPI上的最新版本)进行了测试。根据 PyPI 的说法,它支持 Python >=3.8.1 和 <4.0。
也许您的 LangChain 版本或其依赖项之一已过时?尝试在全新安装 LangChain 的新venv中运行它。
| 归档时间: |
|
| 查看次数: |
3802 次 |
| 最近记录: |