Core LangChain.js abstractions and schemas for building applications with Large Language Models
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Template system for creating dynamic prompts with variable substitution and formatting. Prompt templates enable reusable and parameterized prompt construction.
Abstract base class for all prompt templates.
/**
* Abstract base class for prompt templates
* @template RunInput - Input values type
* @template RunOutput - Prompt value output type
*/
abstract class BasePromptTemplate<RunInput extends InputValues = InputValues, RunOutput extends PromptValue = PromptValue> extends Runnable<RunInput, RunOutput> {
/** Input variables required by this template */
inputVariables: string[];
/** Optional variables that can be used */
optionalVariables?: string[];
/** Output parser for post-processing */
outputParser?: BaseOutputParser;
/** Additional template data */
templateFormat?: string;
/** Partial variables with pre-filled values */
partialVariables?: Record<string, unknown>;
constructor(input: BasePromptTemplateInput);
/** Format template to string */
abstract format(values: InputValues): Promise<string>;
/** Format template to PromptValue */
abstract formatPromptValue(values: InputValues): Promise<RunOutput>;
/** Create template with partial variables */
partial(values: Record<string, unknown>): Promise<BasePromptTemplate<RunInput, RunOutput>>;
/** Merge with another template */
pipe<NewRunOutput>(coerceable: RunnableLike<RunOutput, NewRunOutput>): Runnable<RunInput, NewRunOutput>;
}Template for generating string prompts.
/**
* Template for string-based prompts
*/
class PromptTemplate extends BasePromptTemplate<InputValues, StringPromptValue> {
/** Template string with variable placeholders */
template: string;
/** Format for template variables (f-string, mustache, etc.) */
templateFormat: TemplateFormat;
/** Function to validate input variables */
validateTemplate?: boolean;
constructor(input: PromptTemplateInput);
/** Format template with input values */
async format(values: InputValues): Promise<string>;
/** Format to PromptValue object */
async formatPromptValue(values: InputValues): Promise<StringPromptValue>;
/** Create template from string */
static fromTemplate(template: string, options?: Partial<PromptTemplateInput>): PromptTemplate;
/** Create from examples */
static fromExamples(examples: string[], suffix: string, inputVariables: string[], options?: Partial<PromptTemplateInput>): PromptTemplate;
}Usage Examples:
import { PromptTemplate } from "@langchain/core/prompts";
// Simple template
const template = PromptTemplate.fromTemplate(
"What is a good name for a company that makes {product}?"
);
const prompt = await template.format({ product: "colorful socks" });
console.log(prompt); // "What is a good name for a company that makes colorful socks?"
// Template with multiple variables
const complexTemplate = PromptTemplate.fromTemplate(`
You are a {role} helping a user with {task}.
The user's experience level is {experience_level}.
Please provide {response_type} advice.
User question: {question}
`);
const complexPrompt = await complexTemplate.format({
role: "senior developer",
task: "debugging JavaScript",
experience_level: "beginner",
response_type: "step-by-step",
question: "Why is my variable undefined?"
});Template for generating chat message sequences.
/**
* Template for chat message sequences
*/
class ChatPromptTemplate extends BasePromptTemplate<InputValues, ChatPromptValue> {
/** Array of message templates */
promptMessages: BaseMessagePromptTemplate[];
/** Whether to validate template variables */
validateTemplate?: boolean;
constructor(input: ChatPromptTemplateInput);
/** Format to chat messages */
async formatMessages(values: InputValues): Promise<BaseMessage[]>;
/** Format to PromptValue */
async formatPromptValue(values: InputValues): Promise<ChatPromptValue>;
/** Format to string representation */
async format(values: InputValues): Promise<string>;
/** Create from array of message templates */
static fromMessages(promptMessages: (BaseMessagePromptTemplate | BaseMessageLike)[]): ChatPromptTemplate;
/** Create from template string */
static fromTemplate(template: string, options?: Partial<ChatPromptTemplateInput>): ChatPromptTemplate;
}Usage Examples:
import { ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate } from "@langchain/core/prompts";
// From template string
const chatTemplate = ChatPromptTemplate.fromTemplate(
"You are a helpful assistant. Help the user with: {task}"
);
// From message templates
const complexChatTemplate = ChatPromptTemplate.fromMessages([
SystemMessagePromptTemplate.fromTemplate("You are a {role}"),
HumanMessagePromptTemplate.fromTemplate("Help me with {request}"),
["ai", "I'll help you with {request}. What specifically do you need?"],
["human", "{followup_question}"]
]);
const messages = await complexChatTemplate.formatMessages({
role: "helpful assistant",
request: "writing code",
followup_question: "How do I handle errors?"
});Templates for specific message types.
/**
* Template for human messages
*/
class HumanMessagePromptTemplate extends BaseMessagePromptTemplate {
static fromTemplate(template: string, options?: Partial<PromptTemplateInput>): HumanMessagePromptTemplate;
}
/**
* Template for AI messages
*/
class AIMessagePromptTemplate extends BaseMessagePromptTemplate {
static fromTemplate(template: string, options?: Partial<PromptTemplateInput>): AIMessagePromptTemplate;
}
/**
* Template for system messages
*/
class SystemMessagePromptTemplate extends BaseMessagePromptTemplate {
static fromTemplate(template: string, options?: Partial<PromptTemplateInput>): SystemMessagePromptTemplate;
}Template for few-shot learning with examples.
/**
* Template with examples for few-shot learning
*/
class FewShotPromptTemplate extends BasePromptTemplate {
/** Array of example objects */
examples: Record<string, string>[];
/** Template for each example */
examplePrompt: PromptTemplate;
/** Separator between examples */
exampleSeparator: string;
/** Prefix before examples */
prefix?: string;
/** Suffix after examples */
suffix?: string;
/** Example selector for dynamic selection */
exampleSelector?: BaseExampleSelector;
constructor(input: FewShotPromptTemplateInput);
}Usage Examples:
import { FewShotPromptTemplate, PromptTemplate } from "@langchain/core/prompts";
const examplePrompt = PromptTemplate.fromTemplate(
"Input: {input}\nOutput: {output}"
);
const fewShotTemplate = new FewShotPromptTemplate({
examples: [
{ input: "happy", output: "sad" },
{ input: "tall", output: "short" },
{ input: "hot", output: "cold" }
],
examplePrompt,
suffix: "Input: {word}\nOutput:",
inputVariables: ["word"]
});
const prompt = await fewShotTemplate.format({ word: "big" });
// Returns formatted examples + "Input: big\nOutput:"/**
* Base class for prompt values
*/
abstract class BasePromptValue {
/** Convert to string representation */
abstract toString(): string;
/** Convert to chat messages */
abstract toChatMessages(): BaseMessage[];
}
/**
* String-based prompt value
*/
class StringPromptValue extends BasePromptValue {
value: string;
constructor(value: string);
toString(): string;
toChatMessages(): BaseMessage[];
}
/**
* Chat message sequence prompt value
*/
class ChatPromptValue extends BasePromptValue {
messages: BaseMessage[];
constructor(messages: BaseMessage[]);
toString(): string;
toChatMessages(): BaseMessage[];
}interface InputValues {
[key: string]: unknown;
}
type TemplateFormat = "f-string" | "mustache" | "jinja2";
interface BasePromptTemplateInput {
inputVariables: string[];
optionalVariables?: string[];
partialVariables?: Record<string, unknown>;
outputParser?: BaseOutputParser;
}
interface PromptTemplateInput extends BasePromptTemplateInput {
template: string;
templateFormat?: TemplateFormat;
validateTemplate?: boolean;
}
interface ChatPromptTemplateInput extends BasePromptTemplateInput {
promptMessages: BaseMessagePromptTemplate[];
validateTemplate?: boolean;
}
interface FewShotPromptTemplateInput extends BasePromptTemplateInput {
examples: Record<string, string>[];
examplePrompt: PromptTemplate;
exampleSeparator?: string;
prefix?: string;
suffix?: string;
exampleSelector?: BaseExampleSelector;
}
type BaseMessageLike = BaseMessage | string | [string, string];