Skip to main content

Additional Functionality: Prompt Templates

We offer a number of extra features for prompt templates, as shown below:

Prompt Values

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 supports this in two ways:

  1. Partial formatting with string values.
  2. Partial formatting with functions that return string values.

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 intructions. */
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:
*/
};