Skip to main content

提示模板下的额外功能(Additional Functionality: Prompt Templates)

我们提供了一些额外的功能,以便在提示模板中展示,如下所示:(We offer a number of extra features for prompt templates as shown below)

提示值(Prompt Values)

PromptValue是由PromptTemplateformatPromptValue方法返回的对象。它可以被转换为一个字符串或一组 ChatMessage 对象。(A PromptValue is an object returned by the formatPromptValue of a PromptTemplate. It can be converted to a string or list of ChatMessage objects.)

import {
ChatPromptTemplate,
HumanMessagePromptTemplate,
PromptTemplate,
SystemMessagePromptTemplate,
} from "langchain/prompts";

export const run = async () => {
const template = "What is a good name for a company that makes {product}?";
const promptA = new PromptTemplate({ template, inputVariables: ["product"] });

// The `formatPromptValue` method returns a `PromptValue` object that can be used to format the prompt as a string or a list of `ChatMessage` objects.
const responseA = await promptA.formatPromptValue({
product: "colorful socks",
});
const responseAString = responseA.toString();
console.log({ responseAString });
/*
{
responseAString: 'What is a good name for a company that makes colorful socks?'
}
*/

const responseAMessages = responseA.toChatMessages();
console.log({ responseAMessages });
/*
{
responseAMessages: [
HumanChatMessage {
text: 'What is a good name for a company that makes colorful socks?'
}
]
}
*/

const chatPrompt = ChatPromptTemplate.fromPromptMessages([
SystemMessagePromptTemplate.fromTemplate(
"You are a helpful assistant that translates {input_language} to {output_language}."
),
HumanMessagePromptTemplate.fromTemplate("{text}"),
]);

// `formatPromptValue` also works with `ChatPromptTemplate`.
const responseB = await chatPrompt.formatPromptValue({
input_language: "English",
output_language: "French",
text: "I love programming.",
});
const responseBString = responseB.toString();
console.log({ responseBString });
/*
{
responseBString: '[{"text":"You are a helpful assistant that translates English to French."},{"text":"I love programming."}]'
}
*/

const responseBMessages = responseB.toChatMessages();
console.log({ responseBMessages });
/*
{
responseBMessages: [
SystemChatMessage {
text: 'You are a helpful assistant that translates English to French.'
},
HumanChatMessage { text: 'I love programming.' }
]
}
*/
};

部分值(Partial Values)

与其他方法一样,“部分化”提示模板是有意义的,例如将所需的值的子集传递给其余值的子集以创建只期望该子集的新提示模板。(Like other methods, it can make sense to "partial" a prompt template - eg 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:)

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

这两种不同的方式支持不同的用例。在下面的示例中,我们探讨了两种使用情况以及如何在LangChain中实现它。(These two different ways support different use cases. In the examples below, we go over the motivations for both use cases as well as how to do it in LangChain.)

import { PromptTemplate } from "langchain/prompts";

export const run = async () => {
// The `partial` method returns a new `PromptTemplate` object that can be used to format the prompt with only some of the input variables.
const promptA = new PromptTemplate({
template: "{foo}{bar}",
inputVariables: ["foo", "bar"],
});
const partialPromptA = await promptA.partial({ foo: "foo" });
console.log(await partialPromptA.format({ bar: "bar" }));
// foobar

// You can also explicitly specify the partial variables when creating the `PromptTemplate` object.
const promptB = new PromptTemplate({
template: "{foo}{bar}",
inputVariables: ["foo"],
partialVariables: { bar: "bar" },
});
console.log(await promptB.format({ foo: "foo" }));
// foobar

// You can also use partial formatting with function inputs instead of string inputs.
const promptC = new PromptTemplate({
template: "Tell me a {adjective} joke about the day {date}",
inputVariables: ["adjective", "date"],
});
const partialPromptC = await promptC.partial({
date: () => new Date().toLocaleDateString(),
});
console.log(await partialPromptC.format({ adjective: "funny" }));
// Tell me a funny joke about the day 3/22/2023

const promptD = new PromptTemplate({
template: "Tell me a {adjective} joke about the day {date}",
inputVariables: ["adjective"],
partialVariables: { date: () => new Date().toLocaleDateString() },
});
console.log(await promptD.format({ adjective: "funny" }));
// Tell me a funny joke about the day 3/22/2023
};

少量示例提示模板(Few-Shot Prompt Templates)

少量示例提示模板指的是可以通过示例构建的提示模板。(A few-shot prompt template is a prompt template you can build with examples.)

import { FewShotPromptTemplate, PromptTemplate } from "langchain/prompts";

export const run = async () => {
// First, create a list of few-shot examples.
const examples = [
{ word: "happy", antonym: "sad" },
{ word: "tall", antonym: "short" },
];

// Next, we specify the template to format the examples we have provided.
const exampleFormatterTemplate = "Word: {word}\nAntonym: {antonym}\n";
const examplePrompt = new PromptTemplate({
inputVariables: ["word", "antonym"],
template: exampleFormatterTemplate,
});
// Finally, we create the `FewShotPromptTemplate`
const fewShotPrompt = new FewShotPromptTemplate({
/* These are the examples we want to insert into the prompt. */
examples,
/* This is how we want to format the examples when we insert them into the prompt. */
examplePrompt,
/* The prefix is some text that goes before the examples in the prompt. Usually, this consists of instructions. */
prefix: "Give the antonym of every input",
/* The suffix is some text that goes after the examples in the prompt. Usually, this is where the user input will go */
suffix: "Word: {input}\nAntonym:",
/* The input variables are the variables that the overall prompt expects. */
inputVariables: ["input"],
/* The example_separator is the string we will use to join the prefix, examples, and suffix together with. */
exampleSeparator: "\n\n",
/* The template format is the formatting method to use for the template. Should usually be f-string. */
templateFormat: "f-string",
});

// We can now generate a prompt using the `format` method.
console.log(await fewShotPrompt.format({ input: "big" }));
/*
Give the antonym of every input

Word: happy
Antonym: sad


Word: tall
Antonym: short


Word: big
Antonym:
*/
};