🤔 Introducing APISIX AI Gateway – Built for LLMs and AI workloads. Learn More

ai-prompt-decorator

描述

ai-prompt-decorator 插件通过在用户输入提示前后添加预设提示来修改用户输入,以在内容生成中设定上下文。这种做法有助于模型在交互过程中按照预期的指导方针运行。

插件属性

名称类型必选项默认值有效值描述
prependarray条件必选*要前置的提示对象数组。
prepend.rolestring[system, user, assistant]消息的角色。
prepend.contentstring长度 >= 1消息的内容(提示)。
appendarray条件必选*要追加的提示对象数组。
append.rolestring[system, user, assistant]消息的角色。
append.contentstring长度 >= 1消息的内容(提示)。

* 条件必选:必须提供 prependappend 中的至少一个。

示例

以下示例将使用 OpenAI 作为上游服务提供商。在开始之前,请创建一个 OpenAI 账户 和一个 API 密钥。你可以选择将密钥保存到环境变量中:

export OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>

如果你使用的是其他 LLM 提供商,请参阅该提供商的文档以获取 API 密钥。

前置和追加消息

以下示例演示了如何配置 ai-prompt-decorator 插件,在用户输入消息前添加一条系统消息,并在其后追加一条用户消息。该插件与 ai-proxy 插件配合使用,将请求转发到 OpenAI。

NOTE

你可以使用以下命令从 config.yaml 中获取 admin_key 并保存到环境变量中:

admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')

Admin API

创建一个路由到聊天补全端点,并配置预设提示装饰器:

curl "http://127.0.0.1:9180/apisix/admin/routes/1" -X PUT \
  -H "X-API-KEY: ${admin_key}" \
  -d '{
    "uri": "/openai-chat",
    "methods": ["POST"],
    "plugins": {
      "ai-proxy": {
        "provider": "openai",
        "auth": {
          "header": {
            "Authorization": "Bearer '"$OPENAI_API_KEY"'"
          }
        }
      },
      "ai-prompt-decorator": {
        "prepend":[
          {
            "role": "system",
            "content": "Answer briefly and conceptually."
          }
        ],
        "append":[
          {
            "role": "user",
            "content": "End the answer with a simple analogy."
          }
        ]
      }
    }
  }'

ADC

创建一个配置了 ai-proxyai-prompt-decorator 插件的路由:

adc.yaml
services:
  - name: prompt-decorator-service
    routes:
      - name: prompt-decorator-route
        uris:
          - /openai-chat
        methods:
          - POST
        plugins:
          ai-proxy:
            provider: openai
            auth:
              header:
                Authorization: "Bearer ${OPENAI_API_KEY}"
          ai-prompt-decorator:
            prepend:
              - role: system
                content: "Answer briefly and conceptually."
            append:
              - role: user
                content: "End the answer with a simple analogy."

将配置同步到网关:

adc sync -f adc.yaml

Ingress Controller

Gateway API

创建一个配置了 ai-proxyai-prompt-decorator 插件的路由:

ai-prompt-decorator-ic.yaml
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
  namespace: aic
  name: ai-prompt-decorator-plugin-config
spec:
  plugins:
    - name: ai-proxy
      config:
        provider: openai
        auth:
          header:
            Authorization: "Bearer your-api-key"
    - name: ai-prompt-decorator
      config:
        prepend:
          - role: system
            content: "Answer briefly and conceptually."
        append:
          - role: user
            content: "End the answer with a simple analogy."
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  namespace: aic
  name: prompt-decorator-route
spec:
  parentRefs:
    - name: apisix
  rules:
    - matches:
        - path:
            type: Exact
            value: /openai-chat
          method: POST
      filters:
        - type: ExtensionRef
          extensionRef:
            group: apisix.apache.org
            kind: PluginConfig
            name: ai-prompt-decorator-plugin-config

APISIX Ingress Controller

创建一个配置了 ai-proxyai-prompt-decorator 插件的路由:

ai-prompt-decorator-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  namespace: aic
  name: prompt-decorator-route
spec:
  ingressClassName: apisix
  http:
    - name: prompt-decorator-route
      match:
        paths:
          - /openai-chat
        methods:
          - POST
      plugins:
        - name: ai-proxy
          enable: true
          config:
            provider: openai
            auth:
              header:
                Authorization: "Bearer your-api-key"
        - name: ai-prompt-decorator
          enable: true
          config:
            prepend:
              - role: system
                content: "Answer briefly and conceptually."
            append:
              - role: user
                content: "End the answer with a simple analogy."

将配置应用到集群:

kubectl apply -f ai-prompt-decorator-ic.yaml

向路由发送 POST 请求,在请求体中指定模型和示例消息:

curl "http://127.0.0.1:9080/openai-chat" -X POST \
  -H "Content-Type: application/json" \
  -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
  }
}