When activated, the template restricts LLM usage to the predefined templates. They are defined in the following format:
curl -X POST https://{region}.api.konghq.com/v1/ai-gateways/{AIGatewayId}/policies \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"display_name": "AI Prompt Template - Sample Template",
"name": "ai-prompt-template",
"type": "ai-prompt-template",
"config": {
"templates": [
{
"name": "sample-template",
"template": "{\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Explain to me what {{thing}} is.\"\n }\n ]\n}"
}
]
}
}
'
ai_gateway_policies:
- ref: ai-prompt-template
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
display_name: AI Prompt Template - Sample Template
name: ai-prompt-template
type: ai-prompt-template
config:
templates:
- name: sample-template
template: |-
{
"messages": [
{
"role": "user",
"content": "Explain to me what {{thing}} is."
}
]
}
Make sure to replace the following placeholders with your own values:
-
AI_GATEWAY_ID: The id of your AI Gateway.
When calling a template, replace the content of messages (llm/v1/chat) or prompt (llm/v1/completions) with a template reference, using the following format:
{
"messages": "{template://sample-template}",
"properties": {
"thing": "gravity"
}
}
By default, requests that don’t use a template are still be passed to the LLM. However, this can be configured using the config.allow_untemplated_requests parameter. If this parameter is set to false, requests that don’t use a template will return a 400 Bad Request response.