- Personalizing prompts with user-specific data (name, account tier, history) at runtime.
- Reusing a single prompt template across many contexts without duplicating it.
- Separating prompt logic from runtime data for cleaner, testable code.
- Injecting dynamic content (current date, retrieved chunks) without string concatenation.
Replace variables in prompt messages using
{{variableName}} syntax for dynamic content injection.
Quick Start
curl -X POST https://api.orq.ai/v3/router/responses \
-H "Authorization: Bearer $ORQ_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o",
"input": "Hello {{customer_name}}, your {{product_name}} subscription expires soon.",
"variables": {
"customer_name": "John Smith",
"product_name": "Premium Plan"
}
}'
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ORQ_API_KEY,
baseURL: "https://api.orq.ai/v3/router",
});
const response = await client.responses.create({
model: "openai/gpt-4o",
input: "Hello {{customer_name}}, your {{product_name}} subscription expires soon.",
variables: {
customer_name: "John Smith",
product_name: "Premium Plan",
},
});
console.log(response.output_text);
from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ.get("ORQ_API_KEY"),
base_url="https://api.orq.ai/v3/router",
)
response = client.responses.create(
model="openai/gpt-4o",
input="Hello {{customer_name}}, your {{product_name}} subscription expires soon.",
extra_body={
"variables": {
"customer_name": "John Smith",
"product_name": "Premium Plan",
}
},
)
print(response.output_text)
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ORQ_API_KEY,
baseURL: "https://api.orq.ai/v3/router",
});
const response = await client.chat.completions.create({
model: "openai/gpt-4o",
messages: [{
role: "user",
content: "Hello {{customer_name}}, your {{product_name}} subscription expires soon.",
}],
variables: {
customer_name: "John Smith",
product_name: "Premium Plan",
},
});
"Hello John Smith, your Premium Plan subscription expires soon."
Configuration
| Parameter | Type | Required | Description |
|---|---|---|---|
variables | object | No | Key-value pairs to replace {{key}} variables in messages |
{{variableName}} (case-sensitive, alphanumeric + underscore)
Use Cases
| Scenario | Variables | Example Input |
|---|---|---|
| Customer Support | {{customer_name}}, {{issue_type}} | {customer_name: "Alice", issue_type: "billing"} |
| Product Recommendations | {{user_preferences}}, {{budget}} | {user_preferences: "eco-friendly", budget: "$500"} |
| Onboarding | {{user_name}}, {{plan_type}} | {user_name: "Bob", plan_type: "Enterprise"} |
| Content Generation | {{topic}}, {{audience}} | {topic: "AI trends", audience: "developers"} |
Implementation Examples
Customer Support Automation
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ORQ_API_KEY,
baseURL: "https://api.orq.ai/v3/router",
});
const supportTicket = {
customerName: "Sarah Johnson",
issueType: "billing",
accountType: "premium",
ticketId: "TICKET-001"
};
const response = await client.chat.completions.create({
model: "openai/gpt-4o",
messages: [{
role: "system",
content: "You are a helpful customer support agent for {{company_name}}."
}, {
role: "user",
content: "Hi, I'm {{customer_name}} and I have a {{issue_type}} issue with my {{account_type}} account. Ticket: {{ticket_id}}"
}],
variables: {
company_name: "Acme Corp",
customer_name: supportTicket.customerName,
issue_type: supportTicket.issueType,
account_type: supportTicket.accountType,
ticket_id: supportTicket.ticketId
},
});
Personalized Email Generation
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ORQ_API_KEY,
baseURL: "https://api.orq.ai/v3/router",
});
const emailTemplate = {
recipient: "Marketing Team",
campaign: "Q4 Product Launch",
metrics: "25% increase in engagement"
};
const response = await client.chat.completions.create({
model: "openai/gpt-4o",
messages: [{
role: "user",
content: `Create a performance report email for {{recipient}} about the {{campaign}} campaign.
Highlight that we achieved {{metrics}} and include actionable next steps.`
}],
variables: {
recipient: emailTemplate.recipient,
campaign: emailTemplate.campaign,
metrics: emailTemplate.metrics
},
});
Multi-Language Support
curl -X POST https://api.orq.ai/v3/router/responses \
-H "Authorization: Bearer $ORQ_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o",
"input": "Generate a {{content_type}} about {{topic}} for {{target_audience}}",
"variables": {
"content_type": "blog post",
"topic": "sustainable technology",
"target_audience": "enterprise decision makers"
}
}'
curl -X POST https://api.orq.ai/v3/router/chat/completions \
-H "Authorization: Bearer $ORQ_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o",
"messages": [{
"role": "user",
"content": "Generate a {{content_type}} about {{topic}} for {{target_audience}}"
}],
"variables": {
"content_type": "blog post",
"topic": "sustainable technology",
"target_audience": "enterprise decision makers"
}
}'
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ORQ_API_KEY,
baseURL: "https://api.orq.ai/v3/router",
});
const response = await client.responses.create({
model: "openai/gpt-4o",
input: "Generate a {{content_type}} about {{topic}} for {{target_audience}}",
variables: {
content_type: "blog post",
topic: "sustainable technology",
target_audience: "enterprise decision makers",
},
});
console.log(response.output_text);
from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ.get("ORQ_API_KEY"),
base_url="https://api.orq.ai/v3/router",
)
response = client.responses.create(
model="openai/gpt-4o",
input="Generate a {{content_type}} about {{topic}} for {{target_audience}}",
extra_body={
"variables": {
"content_type": "blog post",
"topic": "sustainable technology",
"target_audience": "enterprise decision makers",
}
},
)
print(response.output_text)
Advanced Patterns
Dynamic Template Loading
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ORQ_API_KEY,
baseURL: "https://api.orq.ai/v3/router",
});
interface TemplateConfig {
template: string;
requiredInputs: string[];
defaultInputs?: Record<string, string>;
}
const templates: Record<string, TemplateConfig> = {
welcome: {
template: "Welcome {{user_name}}! Your {{plan_type}} account is now active.",
requiredInputs: ["user_name", "plan_type"]
},
reminder: {
template: "Hi {{user_name}}, your {{service}} subscription expires in {{days}} days.",
requiredInputs: ["user_name", "service", "days"],
defaultInputs: { service: "Premium Plan" }
}
};
async function generateFromTemplate(
templateKey: string,
inputs: Record<string, string>
) {
const template = templates[templateKey];
if (!template) {
throw new Error(`Template ${templateKey} not found`);
}
// Validate required inputs
const missing = template.requiredInputs.filter(key => !inputs[key]);
if (missing.length > 0) {
throw new Error(`Missing required inputs: ${missing.join(", ")}`);
}
// Merge with defaults
const finalInputs = { ...template.defaultInputs, ...inputs };
return await client.chat.completions.create({
model: "openai/gpt-4o",
messages: [{
role: "user",
content: template.template
}],
variables: finalInputs,
});
}
// Usage
const response = await generateFromTemplate("welcome", {
user_name: "Alice Johnson",
plan_type: "Enterprise"
});
Conditional Content Generation
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ORQ_API_KEY,
baseURL: "https://api.orq.ai/v3/router",
});
// Generate content based on user tier
const generateTieredContent = async (userTier, baseContent, inputs) => {
const tierSpecificPrompts = {
free: "Keep the response brief and mention premium features are available.",
premium: "Provide detailed information and include premium tips.",
enterprise: "Include advanced strategies and enterprise-specific recommendations."
};
const enhancedInputs = {
...inputs,
user_tier: userTier,
tier_instruction: tierSpecificPrompts[userTier]
};
return await client.chat.completions.create({
model: "openai/gpt-4o",
messages: [{
role: "system",
content: "{{tier_instruction}}"
}, {
role: "user",
content: baseContent
}],
variables: enhancedInputs,
});
};
Batch Input Processing
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ORQ_API_KEY,
baseURL: "https://api.orq.ai/v3/router",
});
const batchInputs = [
{ customer_name: "John Doe", product: "Analytics Pro", status: "trial_ending" },
{ customer_name: "Jane Smith", product: "CRM Plus", status: "payment_failed" },
{ customer_name: "Bob Wilson", product: "Marketing Suite", status: "renewal_due" }
];
const template = "Hi {{customer_name}}, your {{product}} account has status: {{status}}. Please take action.";
const responses = await Promise.all(
batchInputs.map(async (inputs) => {
return client.chat.completions.create({
model: "openai/gpt-4o",
messages: [{ role: "user", content: template }],
variables: inputs,
});
})
);
Input Validation
Type Safety with TypeScript
interface EmailInputs {
recipient_name: string;
company_name: string;
meeting_date: string;
meeting_time: string;
}
interface ProductInputs {
product_name: string;
price: string;
features: string;
target_audience: string;
}
function validateInputs<T>(inputs: T, required: (keyof T)[]): void {
const missing = required.filter(key => !inputs[key]);
if (missing.length > 0) {
throw new Error(`Missing required inputs: ${missing.join(", ")}`);
}
}
// Usage
const emailInputs: EmailInputs = {
recipient_name: "John Smith",
company_name: "Acme Inc",
meeting_date: "December 15",
meeting_time: "2:00 PM"
};
validateInputs(emailInputs, ["recipient_name", "company_name", "meeting_date"]);
Runtime Validation
// Validate input format and constraints
function validateInputValue(key, value) {
const validators = {
email: (v) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v),
phone: (v) => /^\+?[\d\s-()]+$/.test(v),
date: (v) => !isNaN(Date.parse(v)),
price: (v) => /^\$?\d+(\.\d{2})?$/.test(v),
name: (v) => v.length >= 2 && v.length <= 50
};
const validator = validators[key];
if (validator && !validator(value)) {
throw new Error(`Invalid ${key} format: ${value}`);
}
}
// Sanitize inputs
function sanitizeInputs(inputs) {
const sanitized = {};
for (const [key, value] of Object.entries(inputs)) {
// Remove HTML tags and trim whitespace
const cleanValue = String(value)
.replace(/<[^>]*>/g, '')
.trim()
.substring(0, 1000); // Limit length
validateInputValue(key, cleanValue);
sanitized[key] = cleanValue;
}
return sanitized;
}
Error Handling
Template Variable Detection
// Extract variables from template
function extractVariables(template) {
const matches = template.match(/\{\{([^}]+)\}\}/g) || [];
return matches.map(match => match.slice(2, -2).trim());
}
// Validate all variables have inputs
function validateTemplate(template, inputs) {
const variables = extractVariables(template);
const missing = variables.filter(variable => !inputs.hasOwnProperty(variable));
if (missing.length > 0) {
throw new Error(`Missing inputs for variables: ${missing.join(", ")}`);
}
return true;
}
// Usage
const template = "Hello {{name}}, your {{product}} expires on {{date}}";
const inputs = { name: "John", product: "Premium Plan" }; // Missing 'date'
try {
validateTemplate(template, inputs);
} catch (error) {
console.error(error.message); // "Missing inputs for variables: date"
}
Safe Input Substitution
// Preview substitution without making API call
function previewSubstitution(template, inputs) {
let result = template;
// Replace known variables
for (const [key, value] of Object.entries(inputs)) {
const regex = new RegExp(`\\{\\{${key}\\}\\}`, 'g');
result = result.replace(regex, value);
}
// Highlight missing variables
const remaining = result.match(/\{\{[^}]+\}\}/g) || [];
return {
preview: result,
missingVariables: remaining.map(v => v.slice(2, -2)),
isComplete: remaining.length === 0
};
}
// Usage
const preview = previewSubstitution(
"Hi {{name}}, your {{product}} status is {{status}}",
{ name: "Alice", product: "Pro Plan" }
);
console.log(preview.preview); // "Hi Alice, your Pro Plan status is {{status}}"
console.log(preview.missingVariables); // ["status"]
console.log(preview.isComplete); // false
Performance Optimization
Input Caching
// Cache frequently used input combinations
const inputCache = new Map();
function getCachedInputs(cacheKey, inputGenerator) {
if (inputCache.has(cacheKey)) {
return inputCache.get(cacheKey);
}
const inputs = inputGenerator();
inputCache.set(cacheKey, inputs);
// Auto-expire cache entries
setTimeout(() => inputCache.delete(cacheKey), 5 * 60 * 1000); // 5 minutes
return inputs;
}
// Usage
const userId = "user-123";
const user = { name: "Alice", subscription: { tier: "pro" }, lastLogin: new Date() };
const userInputs = getCachedInputs(`user-${userId}`, () => ({
user_name: user.name,
user_tier: user.subscription.tier,
last_login: user.lastLogin.toDateString()
}));
Template Compilation
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ORQ_API_KEY,
baseURL: "https://api.orq.ai/v3/router",
});
function extractVariables(template) {
const matches = template.match(/\{\{([^}]+)\}\}/g) || [];
return matches.map(match => match.slice(2, -2).trim());
}
function validateTemplate(template, inputs) {
const variables = extractVariables(template);
const missing = variables.filter(v => !Object.prototype.hasOwnProperty.call(inputs, v));
if (missing.length > 0) {
throw new Error(`Missing inputs for variables: ${missing.join(", ")}`);
}
return true;
}
// Pre-compile templates for better performance
class TemplateCompiler {
constructor() {
this.compiledTemplates = new Map();
}
compile(templateId, template) {
const variables = extractVariables(template);
this.compiledTemplates.set(templateId, {
template,
variables,
requiredInputs: variables
});
}
async execute(templateId, inputs, model = "openai/gpt-4o") {
const compiled = this.compiledTemplates.get(templateId);
if (!compiled) {
throw new Error(`Template ${templateId} not found`);
}
validateTemplate(compiled.template, inputs);
return await client.chat.completions.create({
model,
messages: [{ role: "user", content: compiled.template }],
variables: inputs,
});
}
}
// Usage
const compiler = new TemplateCompiler();
compiler.compile("welcome", "Welcome {{user_name}} to {{service_name}}!");
const response = await compiler.execute("welcome", {
user_name: "John",
service_name: "AI Assistant"
});
Best Practices
- Variable Naming: Use descriptive, snake_case variable names
- Input Validation: Always validate inputs before sending requests
- Template Testing: Test templates with sample data before production
- Security: Sanitize user inputs to prevent injection attacks
- Performance: Cache frequently used input combinations
- Documentation: Document required inputs for each template
Troubleshooting
Variables not replaced- Cause: Typo in variable name or syntax.
- Solution: Check
{{variableName}}format matches input keys.
- Cause: Required inputs not provided.
- Solution: Validate all template variables have corresponding inputs.
- Cause: HTML/special characters in inputs.
- Solution: Sanitize inputs before sending.
- Cause: Large input objects.
- Solution: Limit input size and cache frequently used values.
Limitations
| Limitation | Description | Workaround |
|---|---|---|
| Variable Syntax | Only {{variableName}} format supported | Use consistent naming convention |
| Nested Objects | No support for {{user.name}} syntax | Flatten object structure |
| Input Size | Large inputs increase token usage | Keep inputs concise and relevant |
| Special Characters | Some characters may need escaping | Sanitize inputs appropriately |
| Case Sensitivity | Variable names are case-sensitive | Use consistent casing throughout |
Integration Examples
CMS Integration
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ORQ_API_KEY,
baseURL: "https://api.orq.ai/v3/router",
});
const getCmsClient = () => ({ getTemplate: async (id: string) => ({ template: "Announcing {{product_name}}..." }) }); // replace with your CMS client
// Integrate with content management systems
const cms = getCmsClient();
const cmsContent = await cms.getTemplate("product-announcement");
const response = await client.chat.completions.create({
model: "openai/gpt-4o",
messages: [{ role: "user", content: cmsContent.template }],
variables: {
product_name: "AI Analytics Pro",
release_date: "January 2024",
key_features: "Advanced reporting, Real-time insights, Custom dashboards",
target_audience: "Enterprise customers"
},
});
Database Integration
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ORQ_API_KEY,
baseURL: "https://api.orq.ai/v3/router",
});
const database = { users: { findById: async (id: string) => ({ fullName: "Jane Doe", subscription: { type: "pro" } }) }, templates: { findById: async (id: string) => ({ content: "Hello {{user_name}}" }) } }; // replace with your database client
const getUserUsageStats = async (userId: string) => ({ requests: 0 }); // replace with your stats lookup
const getPersonalizedRecommendations = async (userId: string) => ([]); // replace with your recommendations lookup
// Pull dynamic content from database
async function generatePersonalizedContent(userId, templateId) {
const [user, template] = await Promise.all([
database.users.findById(userId),
database.templates.findById(templateId)
]);
const inputs = {
user_name: user.fullName,
account_type: user.subscription.type,
usage_stats: await getUserUsageStats(userId),
recommendations: await getPersonalizedRecommendations(userId)
};
return await client.chat.completions.create({
model: "openai/gpt-4o",
messages: [{ role: "user", content: template.content }],
variables: inputs,
});
}