ai-prompt-decorator
ai-prompt-decorator 插件通过在用户输入的提示词前后追加预设的提示词来修饰用户输入,从而在内容生成中设定上下文。这种做法有助于模型在交互过程中在预期的准则内运行。
示例
以下示例将使用 OpenAI 作为上游服务提供商。在开始之前,请 创建一个 OpenAI 账号 并获取 API Key。你可以选择将密钥保存到环境变量中,如下所示:
export OPENAI_API_KEY=<YOUR_OPENAI_API_KEY> # 替换为你的 API 密钥
如果你使用其他 LLM 提供商,请参考该提供商的文档获取 API Key。
前置和后置消息
以下示例演示了如何配置 ai-prompt-decorator 插件,在用户输入消息前追加系统消息,并在之后追加用户消息。
创建一个带有预配置提示词模板的聊天完成端点路由:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-prompt-decorator-route",
"uri": "/v1/chat/completions",
"methods": ["POST"],
"plugins": {
"proxy-rewrite": {
"headers": {
"set": {
// Annotate 1
"Authorization": "Bearer '"$OPENAI_API_KEY"'"
}
}
},
"ai-prompt-decorator": {
"prepend":[
{
"role": "system",
// Annotate 2
"content": "Answer briefly and conceptually."
}
],
"append":[
{
"role": "user",
// Annotate 3
"content": "End the answer with a simple analogy."
}
]
}
},
},
"upstream": {
"type": "roundrobin",
"nodes": {
"api.openai.com:443": 1
},
"scheme": "https"
}
}'
❶ 在 proxy-rewrite 插件中配置 OpenAI API Key。或者,如果你不希望在 APISIX 中配置密钥,也可以选择在每个客户端请求中附带 API Key。
❷ 前置一条系统消息以设定助手的行为。
❸ 在用户定义的提示词后追加额外的用户消息。
向该路由发送一个 POST 请求,在请求体中指定模型和示例消息:
curl "http://127.0.0.1:9080/v1/chat/completions" -X POST \
-H "Content-Type: application/json" \
-H "Host: api.openai.com:443" \
-d '{
"model": "gpt-4",
"messages": [{ "role": "user", "content": "What is mTLS authentication?" }]
}'
你应该收到类似以下的响应:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Mutual TLS (mTLS) authentication is a security protocol that ensures both the client and server authenticate each other's identity before establishing a connection. This mutual authentication is achieved through the exchange and verification of digital certificates, which are cryptographically signed credentials proving each party's identity. In contrast to standard TLS, where only the server is authenticated, mTLS adds an additional layer of trust by verifying the client as well, providing enhanced security for sensitive communications.\n\nThink of mTLS as a secret handshake between two friends meeting at a club. Both must know the handshake to get in, ensuring they recognize and trust each other before entering.",
"role": "assistant"
}
}
],
"created": 1723193502,
"id": "chatcmpl-9uFdWDlwKif6biCt9DpG0xgedEamg",
"model": "gpt-4o-2024-05-13",
"object": "chat.completion",
"system_fingerprint": "fp_abc28019ad",
"usage": {
"completion_tokens": 124,
"prompt_tokens": 31,
"total_tokens": 155
}
}