Skip to main content

ai-prompt-template

Builds the prompt from a template and the request's variables.

Category AIAdd it to Route, Service, Consumer, Consumer group, Global rule, Plugin configEngine plugin ai-prompt-templatePriority 1071

What it does​

ai-prompt-template replaces the body a client sends on a route with a prompt you wrote under Templates. The client names a template under template_name and sends the values it needs. Each name in double braces is filled with the value the request carries under that name.

The client sends values, not the prompt. In the example it sends only the ticket text, and the request that goes on carries the system message and the user message of the template, the ticket text filled in.

When to use it​

  • A ticket summary the help desk asks for many times a day, written once as a template and filled with the ticket text of each request.
  • Prompts the product team owns, changed at the gateway without a new release of the mobile app.
  • Clients that may only use the prompts you offer, each picked by name, never a prompt of their own.
  • One model per task, named in each template, for example a small model for summaries.

Not for adding fixed messages around the client's own prompt — see ai-prompt-decorator (Prepends or appends messages to the prompt sent to the LLM) or changing the path or headers of a request — see proxy-rewrite (Rewrites the request URI, method, host and headers before proxying).

Set it up​

Offer a named template that fills the ticket text into a fixed prompt​

A request picks the template with template_name and carries the ticket text under ticket; quotes, apostrophes and ampersands in the text reach the model as HTML entities.

Fill Templates as in the JSON below.

ai-prompt-template · as the console saves it
{
"templates": [
{
"name": "summarize-ticket",
"template": {
"model": "gpt-4o-mini",
"messages": [
{
"role": "system",
"content": "You summarize support tickets in two sentences."
},
{
"role": "user",
"content": "Ticket: {{ticket}}"
}
]
}
}
]
}

Things to know​

  • Template — A request that names summarize-ticket and carries ticket reaches the backend with You summarize support tickets in two sentences and the user message, Order 4471 arrived with a cracked screen in place of the braces.
  • Templates — Checked only when a request arrives. A template that does not compile is saved anyway, and every request then answers 503.
  • Name — When two rows carry one name, the first answers and the second is never reached.
  • Template — A template whose list of messages is empty is refused when you save.
  • Request body limit — Every request must carry a JSON body naming a stored template under template_name, or it is refused with 400.

Settings​

The fields as the console shows them, section by section.

Templatestemplatesarray of objectsRequired
at least 1 item
Each item:
Nametemplates[].namestringRequired

The name a request sends under template_name to pick this template. When two rows carry one name, the first answers and the second is never reached.

at least 1 character
Templatetemplates[].templateobjectRequired

The request body sent on, where a name in double braces is replaced by the value the request carried under that name. Other keys are stored as written.

example {"model":"gpt-4o","messages":[{"role":"system","content":"Answer briefly."},{"role":"user","content":"Summarise: {{text}}"}]}
messagestemplates[].template.messagesarray
at least 1 item
modeltemplates[].template.modelstring
at least 1 character
Request body limitmax_req_body_sizeintegerdefault 67108864

The largest request body in bytes that is read, where a larger one is refused with 400.

at least 1

Every plugin also has Enabled, Priority, Filter, Error response and Pre-function: see Plugin execution settings.

G8 dev · Apache APISIX® 3.19.0
Apache, Apache APISIX and APISIX are trademarks of The Apache Software Foundation.
Facts
Category
AI
Add it to
Route, Service, Consumer, Consumer group, Global rule, Plugin config
Engine plugin
ai-prompt-template
Priority
1071