langchain (Components) v0.2文档(15)如何部分格式化提示模板(中英对照)

84 阅读3分钟

This guide assumes familiarity with the following concepts:

Like partially binding arguments to a function, it can make sense to "partial" a prompt template - e.g. pass in a subset of the required values, as to create a new prompt template which expects only the remaining subset of values.

就像将参数部分绑定到函数一样,“部分”提示模板也是有意义的 - 例如传入所需值的子集,以创建一个新的提示模板,该模板仅需要剩余的值子集。

LangChain supports this in two ways:

  1. Partial formatting with string values. 使用字符串值进行部分格式化
  2. Partial formatting with functions that return string values. 使用返回字符串值的函数进行部分格式化。

In the examples below, we go over the motivations for both use cases as well as how to do it in LangChain.

Partial with strings

One common use case for wanting to partial a prompt template is if you get access to some of the variables in a prompt before others. For example, suppose you have a prompt template that requires two variables, foo and baz. If you get the foo value early on in your chain, but the baz value later, it can be inconvenient to pass both variables all the way through the chain. Instead, you can partial the prompt template with the foo value, and then pass the partialed prompt template along and just use that. Below is an example of doing this:

想要部分化提示模板的一个常见用例是,如果您先于其他变量访问提示中的某些变量。例如,假设您有一个需要两个变量 foo 和 baz 的提示模板。如果您在链的早期获得 foo 值,但稍后获得 baz 值,则在整个链中传递这两个变量可能会很不方便。相反,您可以使用 foo 值部分化提示模板,然后传递部分提示模板并使用它。下面是执行此操作的示例:

rom langchain_core.prompts import PromptTemplate

prompt = PromptTemplate.from_template("{foo}{bar}")
partial_prompt = prompt.partial(foo="foo")
print(partial_prompt.format(bar="baz"))

API Reference: PromptTemplate

foobaz

You can also just initialize the prompt with the partialed variables.

prompt = PromptTemplate(
    template="{foo}{bar}", input_variables=["bar"], partial_variables={"foo": "foo"}
)
print(prompt.format(bar="baz"))
foobaz

Partial with functions 偏功能

The other common use is to partial with a function. The use case for this is when you have a variable you know that you always want to fetch in a common way. A prime example of this is with date or time. Imagine you have a prompt which you always want to have the current date. You can't hard code it in the prompt, and passing it along with the other input variables is inconvenient. In this case, it's handy to be able to partial the prompt with a function that always returns the current date.

另一个常见用途是使用函数进行部分处理。这种情况的用例是当您有一个变量时,您知道您总是希望以一种通用的方式获取它。一个典型的例子是日期或时间。假设您有一个提示,您总是希望它具有当前日期。您无法在提示中对其进行硬编码,并且将其与其他输入变量一起传递很不方便。在这种情况下,能够使用始终返回当前日期的函数对提示进行部分处理非常方便。

from datetime import datetime

def _get_datetime():
    now = datetime.now()
    return now.strftime("%m/%d/%Y, %H:%M:%S")

prompt = PromptTemplate(
    template="Tell me a {adjective} joke about the day {date}",
    input_variables=["adjective", "date"],
)
partial_prompt = prompt.partial(date=_get_datetime)
print(partial_prompt.format(adjective="funny"))
Tell me a funny joke about the day 04/21/2024, 19:43:57

You can also just initialize the prompt with the partialed variables, which often makes more sense in this workflow.

您还可以使用部分变量初始化提示,这通常在此工作流程中更有意义。

prompt = PromptTemplate(
    template="Tell me a {adjective} joke about the day {date}",
    input_variables=["adjective"],
    partial_variables={"date": _get_datetime},
)
print(prompt.format(adjective="funny"))

Tell me a funny joke about the day 04/21/2024, 19:43:57