Skip to main content

ai-prompt-decorator

Prepends or appends messages to the prompt sent to the LLM.

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

What it does​

ai-prompt-decorator adds fixed messages to the chat request a client sends on a route, before the request goes on. Messages under Prepend come before the client's own, those under Append after them, each with a role and a content.

The client sends only its own question, and what receives the request gets your messages with it. In the example that is the route's own backend, which echoes the system prompt back.

When to use it​

  • A support assistant for the orders API that must keep to orders, shipments and refunds, whatever the client asks.
  • One instruction on length or tone, for example answers in at most three sentences, kept at the gateway instead of in every client.
  • A request added after the client's messages, for example a short summary at the end of every answer.
  • Several clients, a mobile app and a partner portal for example, that share one model and must all get the same instructions.

Not for building the whole prompt from a named template — see ai-prompt-template (Builds the prompt from a template and the request's variables), refusing prompts that carry words or patterns you name — see ai-prompt-guard (Allows or denies prompts that match configured patterns) 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​

Put a system prompt in front of every conversation​

Fill Prepend as in the JSON below.

ai-prompt-decorator · as the console saves it
{
"prepend": [
{
"role": "system",
"content": "You are the support assistant for the orders API. Answer only questions about orders, shipments and refunds."
}
]
}

Things to know​

  • Prepend — In the example a client sends only Where is my order?, and the backend echoes the request it received with the system prompt in it, You are the support assistant for the orders API.
  • Prepend — A list that is there but empty satisfies the requirement and adds nothing to the prompt.
  • Role — A role is system, user or assistant, and tool is refused when you save.
  • Content — A message with empty content is refused when you save.
  • Request body limit — Every request through this object has its body read and decoded as JSON, whatever it is: one with no body at all, and one that is not JSON, are both refused with 400.
  • AdviceKeep the instructions short, in one system message under Prepend.

Settings​

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

PrependprependarrayRequired unless Append is set

Messages put before the client's messages, each with a role and content, such as a system instruction.

Each item:
Contentprepend[].contentstringRequired
at least 1 character
Roleprepend[].rolestringRequired
one of assistant, system, user
AppendappendarrayRequired unless Prepend is set
Each item:
Contentappend[].contentstringRequired
at least 1 character
Roleappend[].rolestringRequired
one of assistant, system, user
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-decorator
Priority
1070