Skip to main content

格式模板输出 chat_prompt.format

LangChain

格式方法的输出可作为字符串、消息列表和 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.'
# or alternatively
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={})]