Prompt Templates
Reusable prompts with variables for consistent AI responses.
What You'll Learn
- Overview - What prompt templates are
- Creating Templates - Define and create templates
- Using Templates - Apply templates with variables
Overview
Prompt templates let you define reusable prompts with variables. Use them for consistent responses across your applications.
Why Use Templates
| Benefit | Description |
|---|---|
| Consistency | Same prompt structure every time |
| Maintainability | Update once, use everywhere |
| Variables | Dynamic 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 Case | Example |
|---|---|
| Customer Support | Consistent support responses |
| Documentation | Standard 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
- Descriptive names - Use clear template names
- Required variables - Document all variables
- Default values - Provide sensible defaults
- Test templates - Verify variable substitution
- Version tracking - Track template changes
- Validation - Validate variable inputs
Next Steps
- Creating Templates - Define and manage templates
- Using Templates - Apply templates in applications