跳到主要内容

预定义提示词模板

在使用大型语言模型(LLM)时,管理员可能希望预先配置一个提示词模板,使用户只需在指定字段中填写内容即可使用。这种方式可以让同一个服务在组织内部被重复复用。

本文将介绍如何使用 ai-prompt-template 插件在 APISIX 中配置提示词模板,使用户能够以“填空”的方式向模型插入变量值并完成交互。本文示例以上游服务 OpenAI 为例,但同样适用于其他 LLM 服务提供商。

前提条件

  • 安装 Docker
  • 安装 cURL 用于向服务发送请求进行验证。
  • 按照 入门教程 在 Docker 或 Kubernetes 上启动一个新的 APISIX 实例。

获取 OpenAI API Key

在继续之前,请创建一个 OpenAI 账户 并生成一个 API Key

你也可以将该 Key 保存为环境变量:

export OPENAI_API_KEY=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   # 替换为你的 API Key

创建路由

创建一个指向 OpenAI API 端点的路由,并配置一个示例提示词模板。该模板接受用户定义的问题内容,并按照指定的复杂度生成回答。

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": "/anything",
"plugins": {
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer '"$OPENAI_API_KEY"'"
}
},
"options": {
"model": "gpt-4"
}
},
"ai-prompt-template": {
"templates": [
{
"name": "QnA with complexity",
"template": {
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "Answer in {{complexity}}."
},
{
"role": "user",
"content": "Explain {{prompt}}."
}
]
}
}
]
}
}
}'

❶ 为模板集合命名。调用该路由时,请在请求中包含模板名称。

❷ 指定模型标识符。

❸ 配置系统提示词,从请求体中的 complexity 字段获取用户指定的回答复杂度。

❹ 配置用户提示词,从请求体中的 prompt 字段获取用户提出的问题。

验证

现在,该路由可以复用,用于回答不同问题,并支持用户指定不同的回答复杂度。

发送 POST 请求,携带示例问题和期望的回答复杂度:

curl "http://127.0.0.1:9080/anything" -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
}
}

下一步

现在你已经学会如何在 APISIX 集成 LLM 服务提供商时预定义提示词模板,使同一个路由能够接收不同的用户输入并服务于多种场景。

如果你希望集成 OpenAI 的流式 API,可以使用 proxy-buffering 插件禁用 NGINX 的 proxy_buffering 指令,以避免服务器发送事件(SSE)被缓冲。

此外,你还可以结合 APISIX 提供的其他能力,例如限流缓存以提升系统可用性和用户体验。