Managing Prompts with ChatPromptTemplates
Hardcoding raw prompt strings inside functions limits flexibility. LangChain introduces Prompt Templates to parameterize inputs and manage system-user role allocations.
1. Creating a ChatPromptTemplate
A chat completion model processes an array of messages rather than a single string. Declare a multi-role chat prompt:
import { ChatPromptTemplate } from "@langchain/core/prompts";
// 1. Declare messages using role-content tuples
const promptTemplate = ChatPromptTemplate.fromMessages([
["system", "You are a friendly customer service helper for a retail store named {storeName}."],
["user", "Hi, I have a question about {productName}."]
]);
// 2. Format the template by supplying values
const formattedMessages = await promptTemplate.formatMessages({
storeName: "WebShop Pro",
productName: "Wireless Headphones",
});2. Using MessagePlaceholders for History
If you need to inject a dynamic array of chat history messages inside a prompt template, use MessagePlaceholder:
// src/services/chatPrompt.ts
import { ChatPromptTemplate, MessagePlaceholder } from "@langchain/core/prompts";
import { AIMessage, HumanMessage } from "@langchain/core/messages";
const conversationalPrompt = ChatPromptTemplate.fromMessages([
["system", "You are a helpful database coding assistant."],
// 1. Placeholder for history messages array
new MessagePlaceholder("chatHistory"),
["user", "{userInput}"]
]);
export async function testPromptFormating() {
const result = await conversationalPrompt.formatMessages({
chatHistory: [
new HumanMessage("What is PostgreSQL?"),
new AIMessage("It is a relational database system.")
],
userInput: "Show me how to index columns.",
});
console.log("Formatted Prompt Message Array length:", result.length);
}3. Formatting Rules
- Curly Braces as Variables: LangChain parses words inside curly braces (like
{storeName}) as dynamic variables that must be supplied during execution. - Double Curly Braces Escaping: If your system prompt requires literal curly brace output (for example, generating Javascript code blocks), escape them by doubling the brace characters.
Published on Last updated: