createPrompt
The createPrompt
utility provides a flexible way to manage and generate dynamic prompt strings for language models. It allows you to define templates with placeholders and inject variables at runtime, making prompt tuning and management easier, especially for complex agent interactions.
Key Concepts
Templates
Prompts are defined using template strings. Placeholders for dynamic content are specified using double curly braces: {{variableName}}
.
const template = "You are a helpful assistant that {{role}}. Task: {{task}}";
Variables
Variables provide the actual content for the placeholders. createPrompt
supports both default variables (defined when creating the prompt function) and custom variables (provided when calling the generated function).
- Default Variables: Set up initial values for placeholders.
- Custom Variables: Override default values or provide values for placeholders not covered by defaults.
Custom variables always take precedence over default variables. If a placeholder doesn't have a corresponding variable in either the defaults or customs, it's replaced with an empty string.
Usage
Importing
import { createPrompt } from "@voltagent/core";
Basic Example
Define a template with default variables.
import { createPrompt } from "@voltagent/core";
// Define the prompt template and default variables
const basicPrompt = createPrompt({
template: `You are a helpful assistant that {{role}}.
Task: {{task}}`,
variables: { role: "simplifies complex topics", task: "summarize text" },
});
// Generate prompt using only default variables
const prompt1 = basicPrompt();
console.log(prompt1);
/*
Output:
You are a helpful assistant that simplifies complex topics.
Task: summarize text
*/
Overriding Defaults
Provide custom variables when calling the generated function.
// Generate prompt with a custom 'task'
const prompt2 = basicPrompt({ task: "Explain quantum computing to a 10-year-old" });
console.log(prompt2);
/*
Output:
You are a helpful assistant that simplifies complex topics.
Task: Explain quantum computing to a 10-year-old
*/
// Override both 'role' and 'task'
const prompt3 = basicPrompt({
role: "translates languages",
task: "Translate 'hello' to French",
});
console.log(prompt3);
/*
Output:
You are a helpful assistant that translates languages.
Task: Translate 'hello' to French
*/
More Complex Example (Agent Prompt)
createPrompt
is useful for structuring complex prompts, like those for AI agents.
import { createPrompt } from "@voltagent/core";
const agentPrompt = createPrompt({
template: `You are an AI agent with the following capabilities: {{capabilities}}.
Your current goal is: {{goal}}
Available context: {{context}}
Task: {{task}}`,
variables: {
capabilities: "web search, code execution",
goal: "Answer user queries",
context: "No specific context yet",
task: "", // Default task is empty
},
});
const agentTaskPrompt = agentPrompt({
goal: "Help the user solve a programming problem",
context: "User is working with Node.js and Express",
task: "Debug an error in a REST API endpoint",
});
console.log(agentTaskPrompt);
/*
Output:
You are an AI agent with the following capabilities: web search, code execution.
Your current goal is: Help the user solve a programming problem
Available context: User is working with Node.js and Express
Task: Debug an error in a REST API endpoint
*/
API Reference
createPrompt(options: PromptTemplate)
Creates a reusable prompt generation function.
options
:PromptTemplate
- An object containing the template string and optional default variables.- Returns:
(customVariables?: PromptVariables) => string
- A function that takes optional custom variables and returns the final formatted prompt string.
PromptTemplate
Type
An object defining the prompt structure.
type PromptTemplate = {
template: string; // The template string with {{placeholders}}
variables?: PromptVariables; // Optional default variables
};
PromptVariables
Type
An object representing key-value pairs for template variables.
type PromptVariables = Record<string, string | number | boolean | undefined>;
Returned Function
The function generated by createPrompt
.
(customVariables?: PromptVariables) => string;
customVariables
(optional):PromptVariables
- An object containing variables to override defaults or fill placeholders.- Returns:
string
- The final prompt string with all variables substituted.