a6-plugin-ai-prompt-template
概览
ai-prompt-template 插件通过预配置提示词模板和 {{variable}} 占位符处理 Chat Completions 请求。客户端只需提交模板名称和变量值,插件就会填充模板并生成完整请求。这样可以强制统一提示词结构,避免客户端发送任意系统提示词。
优先级:1071
该插件在优先级为 1070 的 ai-prompt-decorator 和优先级为 1040 的 ai-proxy 之前运行。
适用场景
- 为所有客户端强制使用统一的提示词结构
- 仅接受指定字段的用户输入并填充模板占位符
- 通过受控的系统消息降低提示词注入风险
- 构建可由客户端按名称选择的提示词模板库
插件配置参考
| 字段 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
templates | array | 是 | 模板对象数组(至少 1 个) |
templates[].name | string | 是 | 模板标识符(最小长度为 1) |
templates[].template | object | 是 | 模板规格 |
templates[].template.model | string | 是 | AI 模型名称 |
templates[].template.messages | array | 是 | 消息对象数组(至少 1 个) |
templates[].template.messages[].role | string | 是 | system, user, 或 assistant |
templates[].template.messages[].content | string | 是 | 使用 {{variable}} 占位符的提示词内容 |
模板变量语法
使用双花括号: {{variable_name}}
变量被客户端请求正文中的匹配键替换。
插件在内部使用body-transformer插件进行替换。
客户端请求格式
客户端没有发送标准messages数组,而是发送:
{
"template_name": "my-template",
"variable1": "value1",
"variable2": "value2"
}
插件按名称查找模板,填写变量, 生成一个完整的聊天完成请求正文。
分步指南:创建模板化路由
1. 创建启用 ai-prompt-template 和 ai-proxy 的路由
a6 route create -f - <<'EOF'
{
"id": "templated-chat",
"uri": "/v1/chat/completions",
"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. 使用模板变量发送请求
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"
}'
3.插件发送给OpenAI的内容
{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You are an expert Python code reviewer. Review the code for bugs, performance issues, and style."
},
{
"role": "user",
"content": "Review this code:\n\ndef add(a, b): return a + b"
}
]
}
常见模式
一条路由上有多个模板
{
"plugins": {
"ai-prompt-template": {
"templates": [
{
"name": "translate",
"template": {
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "Translate the following text from {{source_lang}} to {{target_lang}}. Return only the translation."
},
{
"role": "user",
"content": "{{text}}"
}
]
}
},
{
"name": "summarize",
"template": {
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "Summarize the following text in {{style}} style, using at most {{max_sentences}} sentences."
},
{
"role": "user",
"content": "{{text}}"
}
]
}
}
]
}
}
}
客户按名称选择模板:
# Translation
curl http://127.0.0.1:9080/v1/chat/completions \
-d '{"template_name":"translate","source_lang":"English","target_lang":"Chinese","text":"Hello world"}'
# Summarization
curl http://127.0.0.1:9080/v1/chat/completions \
-d '{"template_name":"summarize","style":"concise","max_sentences":"3","text":"Long article..."}'
结合ai-prompt-decorator
管道优先执行 :
ai-prompt-template(1071) —填充变量ai-prompt-decorator(1070) —前置/追加消息ai-proxy(1040) —发送给LLM
{
"plugins": {
"ai-prompt-template": {
"templates": [
{
"name": "qa",
"template": {
"model": "gpt-4",
"messages": [
{"role": "user", "content": "{{question}}"}
]
}
}
]
},
"ai-prompt-decorator": {
"prepend": [
{"role": "system", "content": "Be concise and factual."}
],
"append": [
{"role": "system", "content": "Cite sources if possible."}
]
},
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {"Authorization": "Bearer sk-your-key"}
}
}
}
}
配置同步示例
version: "1"
routes:
- id: templated-chat
uri: /v1/chat/completions
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}}"
- name: explain
template:
model: gpt-4
messages:
- role: system
content: "Explain {{topic}} at a {{level}} level."
- role: user
content: "{{question}}"
故障排查
| 现象 | 原因 | 解决方法 |
|---|---|---|
| 400 "template not found" | template_name 与已配置模板不匹配 | 检查拼写和大小写 |
{{variable}} 未填充 | 请求体中缺少变量键 | 在请求 JSON 中包含所有变量 |
| 插件未转换请求 | 插件名称错误或配置有误 | 确认插件名称为 ai-prompt-template,而不是 prompt-template |
| 与直接消息冲突 | 客户端同时发送 template_name 和 messages | 只发送 template_name 和变量;插件会替换整个请求正文 |
本文根据 api7/a6 仓库中的 a6-plugin-ai-prompt-template/SKILL.md 生成。可在 AI Agent Skills 页面浏览全部 Skill。