Skip to main content

模板格式 Template Formats

PromptTemplate 默认使用 Python f-string 作为其模板格式。然而,它也可以使用其他格式,比如 jinja2,通过 template_format 参数指定。

要使用 jinja2 模板:

from langchain.prompts import PromptTemplate

jinja2_template = "告诉我一个关于{{ adjective }}的笑话,内容是{{ content }}"
prompt = PromptTemplate.from_template(jinja2_template, template_format="jinja2")

prompt.format(adjective="有趣的", content="鸡")
# 输出:告诉我一个有趣的笑话,内容是鸡

API 参考:

要使用 Python f-string 模板:

from langchain.prompts import PromptTemplate

fstring_template = """告诉我一个{adjective}的笑话,内容是{content}"""
prompt = PromptTemplate.from_template(fstring_template)

prompt.format(adjective="有趣的", content="鸡")
# 输出:告诉我一个有趣的笑话,内容是鸡

API 参考:

目前,只支持 jinja2f-string 格式。对于其他格式,请在 Github 页面 上提出问题。