Prompt Template¶
The Prompt Template is a custom Synapse mediator for the WSO2 API Manager Universal Gateway, designed to translate incoming requests into structured, context-aware instructions for AI services. It dynamically applies configurable prompt templates to user inputs, helping align requests with the desired format, tone, and task-specific guidance without violating the underlying AI service’s OpenAPI specification.
Prompt templates are a foundational prompt engineering technique used to standardize and structure prompts before they are sent to a language model. By combining user inputs with reusable instruction patterns, prompt templates guide the model’s behavior more effectively, helping ensure that responses are relevant, coherent, and contextually appropriate. They are especially valuable for applications that require task clarity, tone consistency, or compliance with operational rules.
Features¶
- Apply structured prompt templates to both text and chat-based payloads
- Preserve compatibility with downstream AI service OpenAPI schemas
- Centrally manage templates at the AI API level
- Automatically substitute placeholders with user-provided values
- Improve clarity, consistency, and task alignment in LLM interactions
How to Use¶
Important
This policy is available from the following WSO2 API Manager product update levels onward:
wso2am: Update level greater than 12wso2am-universal-gw: Update level greater than 12wso2am-acp: Update level greater than 13wso2am-tm: Update level greater than 12
Follow these steps to integrate the Prompt Template Guardrail policy into your WSO2 API Manager instance:
-
Download the latest Prompt Template policy
Tip
The downloaded archive contains the following
File Name Description org.wso2.am.policies.mediation.ai.prompt-template-<version>The compiled mediator JAR file policy-definition.jsonPolicy metadata definition artifact.j2Synapse template file -
Copy the mediator JAR file into the API Manager's dropins directory
<APIM_HOME>/repository/components/dropins -
Register the policy in the Publisher portal using the provided
policy-definition.jsonandartifact.j2files via the Publisher REST APIs.- To register the policy common to all AI APIs, follow Add a new common operation policy
- To register the policy specific to a given API, follow Add an API specific operation policy
-
Apply and Deploy the Policy
- Open the API Publisher Portal
(https://<host>:<port>/publisher) - Select your API
- Navigate to Develop > API Configurations > Policies > Request/Response Flow
- Click Add Policy and select Prompt Template Guardrail
- Provide the required configuration (template, placeholder mapping, etc.)
- Save and Deploy the API
- Open the API Publisher Portal
Example Policy Configuration¶
This example demonstrates how the Prompt Template policy can be used to standardize AI behavior across two distinct use cases: customer support and internal data analysis. By applying structured templates with dynamic placeholders (e.g., [[customer_message]], [[report_content]]), the assistant is guided to respond professionally, empathetically, and task-appropriately. This ensures consistent tone, clarity of instruction, and relevance of output whether the AI is handling customer queries or summarizing internal reports.
- Create an AI API with Mistral AI.
-
Add the Prompt Template policy to the API with the following configuration:
Field Example Template NameTemplate DirectivesPrompt Template Configuration:[ { "name": "customer-support-template", "prompt": "You are a professional customer support agent for SwiftTech Electronics. A customer has submitted the following message:\n\n\"[[customer_message]]\"\n\nCraft a helpful and empathetic response addressing the issue. Use a polite and understanding tone. If the issue is the company's fault, apologize sincerely. Offer a resolution or next steps where possible. Sign off in a friendly and professional manner." }, { "name": "data-analyst-template", "prompt": "You are a data analyst at SwiftTech Electronics. Analyze the following internal report:\n\n\"[[report_content]]\"\n\nSummarize the key points in 3-5 bullet points. Identify any actionable insights or recommendations. At the end, classify the overall sentiment of the report as Positive, Neutral, or Negative based on its content. Keep the tone professional and concise." } ]ℹ️ Note: Template names must be unique and comply with valid URL hostname rules, allowing only letters, digits, hyphens (
-), and periods (.). Underscores (_) and other invalid characters are not permitted to ensure correct URI parsing and compatibility. -
Save and re-deploy the API.
-
Invoke the API's
chat/completionendpoint with the following payloads:To invoke the data analysis template:
{ "model": "mistral-small-latest", "messages": [ { "role": "system", "content": "You are an agent working for SwiftTech Electronics. Your responses must align with the company’s organizational guidelines:\n\n- Provide clear, concise, and professional summaries.\n- Ensure insights and recommendations are actionable and data-driven.\n- Maintain confidentiality and avoid speculation.\n- Use a formal tone suitable for internal business communication.\n- Highlight key trends and risks without exaggeration." }, { "role": "user", "content": "template://data-analyst-template?report_content=Sales%20in%20Q2%20grew%20by%208%25%2C%20driven%20by%20strong%20performance%20in%20the%20accessories%20category.%20However%2C%20customer%20satisfaction%20scores%20dropped%20slightly%20due%20to%20shipping%20delays%20in%20May.%20Regional%20performance%20shows%20growth%20in%20Europe%2C%20while%20APAC%20lagged%20behind%20expectations.%20Inventory%20turnover%20improved%20by%2015%25%20following%20system%20upgrades." } ] }To invoke the customer support template:
{ "model": "mistral-small-latest", "messages": [ { "role": "system", "content": "You are an agent working for SwiftTech Electronics. Your responses must align with the company’s organizational guidelines:\n\n- Provide clear, concise, and professional summaries.\n- Ensure insights and recommendations are actionable and data-driven.\n- Maintain confidentiality and avoid speculation.\n- Use a formal tone suitable for internal business communication.\n- Highlight key trends and risks without exaggeration." }, { "role": "user", "content": "template://customer-support-template?customer_message=I%20received%20my%20order%20today%2C%20but%20the%20charger%20is%20missing%20from%20the%20box.%20I%20need%20it%20urgently." } ] }