如何部分格式化提示模板
¥How to partially format prompt templates
与将参数部分绑定到函数一样,将 "partial" 添加到提示模板也是有意义的。 - 例如,传入所需值的子集,以便创建一个只需要剩余值子集的新提示模板。
¥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 通过两种方式支持此功能:
¥LangChain supports this in two ways:
使用字符串值进行部分格式化。
¥Partial formatting with string values.
使用返回字符串值的函数进行部分格式化。
¥Partial formatting with functions that return string values.
在下面的示例中,我们将讨论这两个用例的动机以及如何在 LangChain 中执行这些操作。
¥In the examples below, we go over the motivations for both use cases as well as how to do it in LangChain.
部分 With 字符串
¥Partial with strings
一个常见的使用场景是,如果你需要先访问提示中的某些变量,然后再访问其他变量,那么部分使用提示模板就很常见。例如,假设你有一个提示模板,它需要两个变量 foo
和 baz
。如果你在链的早期获取 foo
值,但在后期获取 baz
值,则在整个链中传递这两个变量可能会很不方便。你可以将提示模板的部分内容设置为 foo
值,然后将部分内容的提示模板传递过去并直接使用。下面是执行此操作的示例:
¥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:
import { PromptTemplate } from "langchain/prompts";
const prompt = new PromptTemplate({
template: "{foo}{bar}",
inputVariables: ["foo", "bar"],
});
const partialPrompt = await prompt.partial({
foo: "foo",
});
const formattedPrompt = await partialPrompt.format({
bar: "baz",
});
console.log(formattedPrompt);
// foobaz
你也可以仅使用部分变量初始化提示。
¥You can also just initialize the prompt with the partialed variables.
const prompt = new PromptTemplate({
template: "{foo}{bar}",
inputVariables: ["bar"],
partialVariables: {
foo: "foo",
},
});
const formattedPrompt = await prompt.format({
bar: "baz",
});
console.log(formattedPrompt);
// foobaz
部分 With 函数
¥Partial With Functions
你还可以使用函数进行部分调用。这种用法的用例是,当你有一个变量,并且你知道你总是希望以一种通用的方式获取它时。一个典型的例子是日期或时间。想象一下,你有一个提示符,你始终希望它显示当前日期。你无法在提示符中对其进行硬编码,并且将其与其他输入变量一起传递可能会很繁琐。在本例中,使用始终返回当前日期的函数来部分处理提示符非常方便。
¥You can also 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 can be tedious. In this case, it's very handy to be able to partial the prompt with a function that always returns the current date.
const getCurrentDate = () => {
return new Date().toISOString();
};
const prompt = new PromptTemplate({
template: "Tell me a {adjective} joke about the day {date}",
inputVariables: ["adjective", "date"],
});
const partialPrompt = await prompt.partial({
date: getCurrentDate,
});
const formattedPrompt = await partialPrompt.format({
adjective: "funny",
});
console.log(formattedPrompt);
// Tell me a funny joke about the day 2023-07-13T00:54:59.287Z
你也可以仅使用部分变量初始化提示:
¥You can also just initialize the prompt with the partialed variables:
const prompt = new PromptTemplate({
template: "Tell me a {adjective} joke about the day {date}",
inputVariables: ["adjective"],
partialVariables: {
date: getCurrentDate,
},
});
const formattedPrompt = await prompt.format({
adjective: "funny",
});
console.log(formattedPrompt);
// Tell me a funny joke about the day 2023-07-13T00:54:59.287Z
后续步骤
¥Next steps
现在你已经学习了如何将变量部分应用于提示模板。
¥You've now learned how to partially apply variables to your prompt templates.
接下来,查看本节中关于提示模板(例如 在提示模板中添加少样本示例)的其他操作指南。
¥Next, check out the other how-to guides on prompt templates in this section, like adding few-shot examples to your prompt templates.