Creating Prompt Templates
Define and create prompt templates for consistent AI responses.
Create a Template
Using cURL
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 responses",
"template": "You are a customer support agent for {{company}}. Your tone should be {{tone}}. Help users with their {{issue_type}} issues.",
"variables": {
"company": {"type": "string", "required": true},
"tone": {"type": "string", "default": "professional"},
"issue_type": {"type": "string", "required": true}
}
}'
Using Python
import requests
response = requests.post(
"http://localhost/api/v1/prompt-templates",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"name": "customer_support",
"description": "Customer support responses",
"template": "You are a customer support agent for {{company}}. Your tone should be {{tone}}. Help users with their {{issue_type}} issues.",
"variables": {
"company": {"type": "string", "required": True},
"tone": {"type": "string", "default": "professional"},
"issue_type": {"type": "string", "required": True}
}
}
)
template = response.json()
print(f"Template ID: {template['id']}")
print(f"Template Name: {template['name']}")
Using JavaScript
const response = await fetch('http://localhost/api/v1/prompt-templates', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'customer_support',
description: 'Customer support responses',
template: 'You are a customer support agent for {{company}}. Your tone should be {{tone}}. Help users with their {{issue_type}} issues.',
variables: {
company: { type: 'string', required: true },
tone: { type: 'string', default: 'professional' },
issue_type: { type: 'string', required: true }
}
})
});
const template = await response.json();
console.log(`Template ID: ${template.id}`);
Template Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Unique template name |
description | string | No | Template description |
template | string | Yes | Template with variables |
variables | object | No | Variable definitions |
Variable Definitions
{
"variables": {
"company": {
"type": "string",
"required": true,
"description": "Company name"
},
"tone": {
"type": "string",
"default": "professional",
"description": "Response tone"
},
"max_length": {
"type": "integer",
"default": 500,
"description": "Maximum response length"
}
}
}
Template Examples
Customer Support Template
response = requests.post(
"http://localhost/api/v1/prompt-templates",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"name": "support_agent",
"description": "Customer support agent template",
"template": """You are a customer support agent for {{company}}.
Guidelines:
- Tone: {{tone | default: "professional and helpful"}}
- Response length: {{max_length | default: "300-500 words"}}
- Language: {{language | default: "English"}}
When helping users:
1. Listen carefully to their issue
2. Show empathy and understanding
3. Provide clear, actionable steps
4. Offer additional help if needed
Current user issue: {{user_issue}}
""",
"variables": {
"company": {"type": "string", "required": True},
"tone": {"type": "string"},
"max_length": {"type": "string"},
"language": {"type": "string"},
"user_issue": {"type": "string", "required": True}
}
}
)
Code Review 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.
Focus areas:
{{#if focus_areas}}
{{#each focus_areas}}
- {{this}}
{{/each}}
{{else}}
- Security
- Performance
- Best practices
- Code style
{{/if}}
Code to review:
```{{language}}
{{code}}
```
Provide feedback in a structured format:
1. Overall assessment
2. Specific issues (line by line)
3. Suggestions for improvement
4. Overall rating (1-10)
""",
"variables": {
"language": {"type": "string", "required": True},
"code": {"type": "string", "required": True},
"focus_areas": {"type": "array"}
}
}
)
Data Analysis Template
response = requests.post(
"http://localhost/api/v1/prompt-templates",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"name": "data_analyst",
"description": "Data analysis template",
"template": """You are a data analyst. Analyze the following data.
Data context: {{context}}
Analysis type: {{analysis_type | default: "general"}}
Metrics of interest: {{metrics | default: "all available"}}
Data:
{{data}}
Provide analysis:
1. Key findings
2. Trends and patterns
3. Anomalies or outliers
4. Recommendations
{{#if include_charts}}
Suggest visualizations for this data.
{{/if}}
""",
"variables": {
"context": {"type": "string", "required": True},
"analysis_type": {"type": "string"},
"metrics": {"type": "string"},
"data": {"type": "string", "required": True},
"include_charts": {"type": "boolean", "default": False}
}
}
)
Documentation Template
response = requests.post(
"http://localhost/api/v1/prompt-templates",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"name": "doc_writer",
"description": "Documentation writer template",
"template": """Write {{doc_type}} documentation for "{{feature_name}}".
Target audience: {{audience | default: "developers"}}
Tone: {{tone | default: "clear and concise"}}
Include examples: {{include_examples | default: true}}
Structure:
1. Overview
2. Prerequisites
3. Quick Start
4. Detailed explanation
{{#if include_examples}}
5. Examples
{{/if}}
6. Common issues
7. Troubleshooting
Additional notes: {{notes}}""",
"variables": {
"doc_type": {"type": "string", "required": True},
"feature_name": {"type": "string", "required": True},
"audience": {"type": "string"},
"tone": {"type": "string"},
"include_examples": {"type": "boolean"},
"notes": {"type": "string"}
}
}
)
Listing Templates
response = requests.get(
"http://localhost/api/v1/prompt-templates",
headers={"Authorization": "Bearer YOUR_API_KEY"}
)
templates = response.json()["templates"]
for template in templates:
print(f"Name: {template['name']}")
print(f"ID: {template['id']}")
print(f"Description: {template.get('description', 'N/A')}\n")
Getting Template Details
template_name = "customer_support"
response = requests.get(
f"http://localhost/api/v1/prompt-templates/{template_name}",
headers={"Authorization": "Bearer YOUR_API_KEY"}
)
template = response.json()
print(f"Name: {template['name']}")
print(f"Template: {template['template']}")
print(f"Variables: {template.get('variables', {})}")
Updating Templates
response = requests.put(
"http://localhost/api/v1/prompt-templates/customer_support",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"template": "Updated template content...",
"description": "Updated description"
}
)
print(f"Updated: {response.json()['message']}")
Deleting Templates
response = requests.delete(
"http://localhost/api/v1/prompt-templates/customer_support",
headers={"Authorization": "Bearer YOUR_API_KEY"}
)
print(f"Deleted: {response.json()['message']}")
Template Testing
def test_template(template_name, variables):
# Test template with variables
response = requests.post(
f"http://localhost/api/v1/prompt-templates/{template_name}/use",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"variables": variables}
)
if response.status_code == 200:
result = response.json()
print("Template test successful!")
print(f"Generated prompt:\n{result['prompt']}")
return True
else:
error = response.json()
print(f"Template test failed: {error.get('error', 'Unknown error')}")
return False
# Test template
success = test_template(
"customer_support",
{
"company": "Test Corp",
"tone": "friendly",
"issue_type": "billing"
}
)
Best Practices
Naming Conventions
Use descriptive, consistent names:
| Good Names | Bad Names |
|---|---|
customer_support_pro | template_1 |
code_reviewer_v2 | review |
data_analyst | analysis |
blog_post_writer | blog |
Variable Design
- Use clear, descriptive variable names
- Provide sensible defaults
- Mark required variables
- Include variable descriptions
- Use type hints for validation
Documentation
Add helpful descriptions:
{
"name": "api_doc_writer",
"description": """Generates API documentation following REST conventions.
Includes request/response examples, error codes, and authentication details.""",
"template": "...",
"variables": {...}
}
Troubleshooting
Template Name Exists
Problem: {"error": "Template name already exists"}
Solution:
- Choose a unique name
- Delete existing template
- Use version suffix (e.g.,
v2)
Variable Missing
Problem: {"error": "Missing required variable: xxx"}
Solution:
- Provide all required variables
- Check variable names match template
- Review template definition
Invalid Template Syntax
Problem: Template fails to parse
Solution:
- Check
{{variable}}syntax - Verify closing brackets
- Test template with sample values
- Review variable definitions
Next Steps
- Using Templates - Apply templates in applications
- Chatbot Documentation - Use templates in chatbots
- Agent Documentation - Use templates in agents