function api_load_template_v2
Flask API endpoint that retrieves and returns an instruction template by name from the chat engine.
/tf/active/vicechatdev/vice_ai/complex_app.py
2411 - 2423
simple
Purpose
This function serves as a REST API endpoint to load predefined instruction templates for chat interactions. It queries the chat engine for a specific template by name and returns the template's instructions in JSON format. This is typically used to provide users with pre-configured instruction sets or prompts for different conversation scenarios.
Source Code
def api_load_template(template_name):
"""Load an instruction template"""
try:
if chat_engine and hasattr(chat_engine, 'get_instruction_template'):
template_instructions = chat_engine.get_instruction_template(template_name)
if template_instructions:
return jsonify({'instructions': template_instructions})
return jsonify({'error': 'Template not found'}), 404
except Exception as e:
logger.error(f"Load template error: {e}")
return jsonify({'error': 'Failed to load template'}), 500
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
template_name |
- | - | positional_or_keyword |
Parameter Details
template_name: String identifier for the instruction template to retrieve. This name is extracted from the URL path parameter and used to query the chat engine's template storage. Expected to be a valid template identifier that exists in the system.
Return Value
Returns a Flask JSON response object. On success (200): {'instructions': <template_content>} where template_content is the instruction text from the template. On template not found (404): {'error': 'Template not found'}. On server error (500): {'error': 'Failed to load template'}. The function always returns a tuple of (response, status_code) for error cases.
Dependencies
flasklogging
Required Imports
from flask import jsonify
import logging
Usage Example
# Assuming Flask app and dependencies are set up:
# GET request to endpoint
import requests
# Example 1: Successful template retrieval
response = requests.get(
'http://localhost:5000/api/templates/customer_service',
headers={'Authorization': 'Bearer <token>'}
)
if response.status_code == 200:
data = response.json()
instructions = data['instructions']
print(f"Template instructions: {instructions}")
# Example 2: Template not found
response = requests.get(
'http://localhost:5000/api/templates/nonexistent',
headers={'Authorization': 'Bearer <token>'}
)
if response.status_code == 404:
print(response.json()['error']) # 'Template not found'
# Example 3: Server error handling
response = requests.get(
'http://localhost:5000/api/templates/some_template',
headers={'Authorization': 'Bearer <token>'}
)
if response.status_code == 500:
print(response.json()['error']) # 'Failed to load template'
Best Practices
- Always check if chat_engine exists and has the required method before calling it to prevent AttributeError
- Ensure proper authentication is in place via the @require_auth decorator before allowing template access
- Log errors with sufficient context for debugging template loading issues
- Return appropriate HTTP status codes (404 for not found, 500 for server errors)
- Validate template_name parameter to prevent injection attacks if used in file system operations
- Consider implementing rate limiting on this endpoint to prevent abuse
- Cache frequently accessed templates to improve performance
- Implement proper access control to ensure users can only access authorized templates
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function api_load_template 97.1% similar
-
function api_templates_v2 87.5% similar
-
function api_templates 86.3% similar
-
function api_save_template 81.5% similar
-
function api_save_template_v1 81.5% similar