Skip to main content

Version: 3.10.x

a7-plugin-ai-prompt-template

Overview

The ai-prompt-template plugin pre-configures prompt templates with {{variable}} placeholders. Clients submit only the template name and variable values; the plugin fills the template and produces a complete chat-completion request. This enforces prompt structure and prevents clients from sending arbitrary system prompts.

Priority: 1071 (runs before ai-prompt-decorator at 1070 and ai-proxy at 1040).

When to Use

  • Enforce a fixed prompt structure across all clients
  • Accept user inputs only for specific fields (fill-in-the-blank)
  • Prevent prompt injection by controlling the system message
  • Build prompt libraries that clients select by name
  • Standardize prompt templates directly on services or routes

Plugin Configuration Reference

FieldTypeRequiredDescription
templatesarrayYesArray of template objects (min 1)
templates[].namestringYesTemplate identifier (min length 1)
templates[].templateobjectYesTemplate specification
templates[].template.modelstringYesAI model name
templates[].template.messagesarrayYesArray of message objects (min 1)
templates[].template.messages[].rolestringYessystem, user, or assistant
templates[].template.messages[].contentstringYesPrompt content with {{variable}} placeholders

Client Request Format

Instead of sending a standard messages array, clients send:

{
"template_name": "my-template",
"variable1": "value1",
"variable2": "value2"
}

Step-by-Step: Create a Templated Route

1. Create a route with ai-prompt-template and ai-proxy

All runtime resources must be scoped to a gateway group using --gateway-group or -g.

a7 service create -g default -f - <<'EOF'
{
"id": "ai-chat-service",
"name": "AI Chat Service"
}
EOF

a7 route create -g default -f - <<'EOF'
{
"id": "templated-chat",
"uri": "/v1/chat/completions",
"service_id": "ai-chat-service",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer sk-your-key"
}
},
"options": {
"model": "gpt-4"
}
},
"ai-prompt-template": {
"templates": [
{
"name": "code-review",
"template": {
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You are an expert {{language}} code reviewer. Review the code for bugs, performance issues, and style."
},
{
"role": "user",
"content": "Review this code:\n\n{{code}}"
}
]
}
}
]
}
}
}
EOF

2. Send a request with template variables

curl http://127.0.0.1:9080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"template_name": "code-review",
"language": "Python",
"code": "def add(a, b): return a + b"
}'

Using Services

You can define standard prompt templates on a service and attach routes to that service.

a7 service create -g default -f - <<'EOF'
{
"id": "global-ai-prompts",
"name": "Global AI Prompts",
"plugins": {
"ai-prompt-template": {
"templates": [
{
"name": "summarize",
"template": {
"model": "gpt-4",
"messages": [
{"role": "system", "content": "Summarize this in one sentence."},
{"role": "user", "content": "{{text}}"}
]
}
}
]
}
}
}
EOF

Config Sync Example

Config sync is scoped by gateway group:

a7 config sync -f config.yaml --gateway-group default
version: "1"
services:
- id: ai-chat-service
name: AI Chat Service
routes:
- id: templated-chat
uri: /v1/chat/completions
service_id: ai-chat-service
methods:
- POST
plugins:
ai-proxy:
provider: openai
auth:
header:
Authorization: Bearer sk-your-key
options:
model: gpt-4
ai-prompt-template:
templates:
- name: code-review
template:
model: gpt-4
messages:
- role: system
content: "You are an expert {{language}} code reviewer."
- role: user
content: "Review this code:\n\n{{code}}"

Troubleshooting

SymptomCauseFix
400 "template not found"template_name doesn't match configured templateCheck spelling and case
Unfilled {{variable}}Variable key missing from request bodyInclude all variables in the request JSON
404 Not FoundMissing --gateway-groupEnsure all runtime commands include -g <group>
Plugin not transformingWrong plugin nameVerify plugin name is ai-prompt-template

This page is generated from a7-plugin-ai-prompt-template/SKILL.md in the api7/a7 repository. Browse all skills on the AI Agent Skills page.

API7.ai Logo

The digital world is connected by APIs,
API7.ai exists to make APIs more efficient, reliable, and secure.

Sign up for API7 newsletter

Product

API7 Gateway

SOC2 Type IIISO 27001HIPAAGDPRRed Herring

Copyright © APISEVEN PTE. LTD 2019 – 2026. Apache, Apache APISIX, APISIX, and associated open source project names are trademarks of the Apache Software Foundation