ai-prompt-template
ai-prompt-template 插件支持以“填空”的方式预配置提示词模板,只接受用户在指定模板变量中输入的内容。
示例
以下示例将使用 OpenAI 作为上游服务提供商。在开始之前,请创建一个 OpenAI 账 号 并获取 API Key。你可以选择将密钥保存到环境变量中,如下所示:
export OPENAI_API_KEY=<YOUR_OPENAI_API_KEY> # 替换为你的 API 密钥
如果你使用其他 LLM 提供商,请参考该提供商的文档获取 API Key。
配置自定义复杂度的开放式问题模板
以下示例演示了如何使用 ai-prompt-template 插件配置一个模板,该模板可用于回答开放式问题并接受用户指定的回答复杂度。
创建一个带有预配置提示词模板的聊天完成端点路由:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-prompt-template-route",
"uri": "/v1/chat/completions",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
// Annotate 1
"Authorization": "Bearer '"$OPENAI_API_KEY"'"
}
},
"options": {
"model": "gpt-4"
}
},
"ai-prompt-template": {
"templates": [
{
// Annotate 2
"name": "QnA with complexity",
"template": {
// Annotate 3
"model": "gpt-4",
"messages": [
{
"role": "system",
// Annotate 4
"content": "Answer in {{complexity}}."
},
{
"role": "user",
// Annotate 5
"content": "Explain {{prompt}}."
}
]
}
}
]
}
}
}'
❶ 在 ai-proxy 插件中配置 OpenAI API Key。
❷ 命名模板。请求该路由时,请求应包含模板名称。
❸ 指定模型标识符。
❹ 配置一个提示词,从请求体键 complexity 中获取用户定义的回答复杂度。
❺ 配置一个提示词,从请求体键 prompt 中获取用户定义的问题。
现在,该路由可以被重复使用,以响应不同用户指定复杂度的各种问题。
向该路由发送一个 POST 请求,请求体中包含示例问题和期望的回答复杂度:
curl "http://127.0.0.1:9080/v1/chat/completions" -X POST \
-H "Content-Type: application/json" \
-d '{
"template_name": "QnA with complexity",
"complexity": "brief",
"prompt": "quick sort"
}'
你应该收到类似以下的响应:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Quick sort is a highly efficient sorting algorithm that uses a divide-and-conquer approach to arrange elements in a list or array in order. Here’s a brief explanation:\n\n1. **Choose a Pivot**: Select an element from the list as a 'pivot'. Common methods include choosing the first element, the last element, the middle element, or a random element.\n\n2. **Partitioning**: Rearrange the elements in the list such that all elements less than the pivot are moved before it, and all elements greater than the pivot are moved after it. The pivot is now in its final position.\n\n3. **Recursively Apply**: Recursively apply the same process to the sub-lists of elements to the left and right of the pivot.\n\nThe base case of the recursion is lists of size zero or one, which are already sorted.\n\nQuick sort has an average-case time complexity of O(n log n), making it suitable for large datasets. However, its worst-case time complexity is O(n^2), which occurs when the smallest or largest element is always chosen as the pivot. This can be mitigated by using good pivot selection strategies or randomization.",
"role": "assistant"
}
}
],
"created": 1723194057,
"id": "chatcmpl-9uFmTYN4tfwaXZjyOQwcp0t5law4x",
"model": "gpt-4o-2024-05-13",
"object": "chat.completion",
"system_fingerprint": "fp_abc28019ad",
"usage": {
"completion_tokens": 234,
"prompt_tokens": 18,
"total_tokens": 252
}
}
配置多个模板
以下示例演示了如何在同一路由上配置多个模板。请求该路由时,用户可以通过指定模板名称向不同的模板传递自定义输入。
该示例接续上一个示例。更新插件配置添加另一个模板:
curl "http://127.0.0.1:9180/apisix/admin/routes/ai-prompt-template-route" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"plugins": {
"ai-prompt-template": {
"templates": [
{
"name": "QnA with complexity",
"template": {
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "Answer in {{complexity}}."
},
{
"role": "user",
"content": "Explain {{prompt}}."
}
]
}
},
{
"name": "echo",
"template": {
"model": "gpt-4",
"messages": [
{
"role": "user",
"content": "Echo {{prompt}}."
}
]
}
}
]
}
}
}'
现在你应该可以通过同一路由使用这两个模板。
向该路由发送一个 POST 请求并使用第一个模板:
curl "http://127.0.0.1:9080/v1/chat/completions" -X POST \
-H "Content-Type: application/json" \
-d '{
"template_name": "QnA with complexity",
"complexity": "brief",
"prompt": "quick sort"
}'
你应该收到类似以下的响应:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Quick sort is a highly efficient sorting algorithm that uses a divide-and-conquer approach to arrange elements in a list or array in order. Here’s a brief explanation:\n\n1. **Choose a Pivot**: Select an element from the list as a 'pivot'. Common methods include choosing the first element, the last element, the middle element, or a random element.\n\n2. **Partitioning**: Rearrange the elements in the list such that all elements less than the pivot are moved before it, and all elements greater than the pivot are moved after it. The pivot is now in its final position.\n\n3. **Recursively Apply**: Recursively apply the same process to the sub-lists of elements to the left and right of the pivot.\n\nThe base case of the recursion is lists of size zero or one, which are already sorted.\n\nQuick sort has an average-case time complexity of O(n log n), making it suitable for large datasets. However, its worst-case time complexity is O(n^2), which occurs when the smallest or largest element is always chosen as the pivot. This can be mitigated by using good pivot selection strategies or randomization.",
"role": "assistant"
}
}
],
...
}
向该路由发送一个 POST 请求并使用第二个模板:
curl "http://127.0.0.1:9080/v1/chat/completions" -X POST \
-H "Content-Type: application/json" \
-d '{
"template_name": "echo",
"prompt": "hello APISIX"
}'
你应该收到类似以下的响应:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "hello APISIX",
"role": "assistant"
}
}
],
...
}