Skip to main content

Prompt Templates

Reusable prompts with variables for consistent AI responses.

What You'll Learn

Overview

Prompt templates let you define reusable prompts with variables. Use them for consistent responses across your applications.

Why Use Templates

BenefitDescription
ConsistencySame prompt structure every time
MaintainabilityUpdate once, use everywhere
VariablesDynamic content with placeholders
Reusability - Share templates across projects
Version Control - Track prompt changes

Template Structure

Templates use {{variable}} syntax for placeholders:

You are a {{role}} for {{company}}.
Your tone should be {{tone}}.
Help users with their {{issue_type}} issues.

Use Cases

Use CaseExample
Customer SupportConsistent support responses
DocumentationStandard explanations and guides
Code Generation - Reusable code patterns
Data Analysis - Consistent analysis formats
Reports - Standardized report structure

Quick Example

# Create a template
curl -X POST http://localhost/api/v1/prompt-templates \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "customer_support",
"description": "Customer support template",
"template": "You are a customer support agent for {{company}}. Your tone should be {{tone}}. Help with {{issue_type}} issues."
}'

# Use the template
curl -X POST http://localhost/api/v1/prompt-templates/customer_support/use \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"variables": {
"company": "Acme Corp",
"tone": "professional",
"issue_type": "technical"
}
}'

Python Example

import requests

# Create template
response = requests.post(
"http://localhost/api/v1/prompt-templates",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"name": "code_reviewer",
"description": "Code review template",
"template": """You are a code reviewer. Review the following {{language}} code and provide feedback on:
- Security issues
- Performance optimizations
- Best practices
- Code style

Code to review:
{{code}}"""
}
)

template_id = response.json()["id"]

# Use template
use_response = requests.post(
f"http://localhost/api/v1/prompt-templates/{template_id}/use",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"variables": {
"language": "Python",
"code": "def add(a, b): return a + b"
}
}
)

print(use_response.json()["prompt"])

JavaScript Example

class PromptTemplateClient {
constructor(apiKey) {
this.apiKey = apiKey;
this.baseUrl = 'http://localhost/api/v1';
}

async createTemplate(config) {
const response = await fetch(`${this.baseUrl}/prompt-templates`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(config)
});

return await response.json();
}

async useTemplate(name, variables) {
const response = await fetch(
`${this.baseUrl}/prompt-templates/${name}/use`,
{
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ variables })
}
);

return await response.json();
}
}

const client = new PromptTemplateClient('YOUR_API_KEY');

const template = await client.createTemplate({
name: 'blog_post',
description: 'Blog post template',
template: 'Write a {{tone}} blog post about {{topic}} for {{audience}} audience. Include {{sections}} sections.'
});

const result = await client.useTemplate('blog_post', {
tone: 'engaging',
topic: 'AI best practices',
audience: 'developers',
sections: 5
});

console.log(result.prompt);

Template Variables

Basic Variables

Hello {{name}},
Your order {{order_id}} is {{status}}.

Optional Variables

{{#if show_details}}
Order details: {{details}}
{{/if}}

Lists

The items are:
{{#each items}}
- {{this}}
{{/each}}

Default Values

Hello {{name | default: "Guest"}},
Welcome to our service.

Best Practices

  1. Descriptive names - Use clear template names
  2. Required variables - Document all variables
  3. Default values - Provide sensible defaults
  4. Test templates - Verify variable substitution
  5. Version tracking - Track template changes
  6. Validation - Validate variable inputs

Next Steps