A tagged template function for creating formatted strings.
This utility provides a clean, template literal-based API for string formatting that can be used for prompts, descriptions, and other text formatting needs.
It automatically handles whitespace normalization and indentation, making it ideal for multi-line strings in code.
When using this utility, it will:
\\n (newline), \\`` (backtick), \$(dollar),{` (brace)context(strings: TemplateStringsArray, values: unknown[]): string| Name | Type | Description |
|---|---|---|
strings* | TemplateStringsArray | |
values* | unknown[] |
import { context } from "@langchain/core/utils/context";
const role = "agent";
const prompt = context`
You are an ${role}.
Your task is to help users.
`;
// Returns: "You are an agent.\nYour task is to help users."// Multi-line interpolated values are aligned
const items = "- Item 1\n- Item 2\n- Item 3";
const message = context`
Shopping list:
${items}
End of list.
`;
// The items will be indented to match " " (4 spaces)