Skip to main content

格式化模板输出 Format template output

format 方法的输出可以作为字符串、消息列表和 ChatPromptValue

作为字符串:

output = chat_prompt.format(input_language="English", output_language="French", text="I love programming.")
output

'System: You are a helpful assistant that translates English to French.\nHuman: I love programming.'

# 或者另一种方式
output_2 = chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_string()

assert output == output_2

作为 ChatPromptValue

chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.")

ChatPromptValue(messages=[SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}), HumanMessage(content='I love programming.', additional_kwargs={})])

作为消息对象列表:

chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages()

[SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}),
HumanMessage(content='I love programming.', additional_kwargs={})]