diff --git a/backend/sdk/src/main/resources/plugins/ai-proxy/README.md b/backend/sdk/src/main/resources/plugins/ai-proxy/README.md new file mode 100644 index 00000000..f671b841 --- /dev/null +++ b/backend/sdk/src/main/resources/plugins/ai-proxy/README.md @@ -0,0 +1,341 @@ +--- +title: AI 代理 +keywords: [ higress,ai,proxy,rag ] +description: AI 代理插件配置参考 +--- + +## 功能说明 + +`AI 代理`插件实现了基于 OpenAI API 契约的 AI 代理功能。目前支持 OpenAI、Azure OpenAI、月之暗面(Moonshot)和通义千问等 AI +服务提供商。 + +## 配置字段 + +### 基本配置 + +| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 | +|------------|--------|------|-----|------------------| +| `provider` | object | 必填 | - | 配置目标 AI 服务提供商的信息 | + +`provider`的配置字段说明如下: + +| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 | +|----------------|-------------------------|------|-----|----------------------------------------------------------------------------------| +| `type` | string | 必填 | - | AI 服务提供商名称。目前支持以下取值:openai, azure, moonshot, qwen | +| `apiTokens` | array of string | 必填 | - | 用于在访问 AI 服务时进行认证的令牌。如果配置了多个 token,插件会在请求时随机进行选择。部分服务提供商只支持配置一个 token。 | +| `timeout` | number | 非必填 | - | 访问 AI 服务的超时时间。单位为毫秒。默认值为 120000,即 2 分钟 | +| `modelMapping` | map of string to string | 非必填 | - | AI 模型映射表,用于将请求中的模型名称映射为服务提供商支持模型名称。
可以使用 "*" 为键来配置通用兜底映射关系 | +| `protocol` | string | 非必填 | - | 插件对外提供的 API 接口契约。目前支持以下取值:openai(默认值,使用 OpenAI 的接口契约)、original(使用目标服务提供商的原始接口契约) | +| `context` | object | 非必填 | - | 配置 AI 对话上下文信息 | + +`context`的配置字段说明如下: + +| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 | +|---------------|--------|------|-----|----------------------------------| +| `fileUrl` | string | 必填 | - | 保存 AI 对话上下文的文件 URL。仅支持纯文本类型的文件内容 | +| `serviceName` | string | 必填 | - | URL 所对应的 Higress 后端服务完整名称 | +| `servicePort` | number | 必填 | - | URL 所对应的 Higress 后端服务访问端口 | + +### 提供商特有配置 + +#### OpenAI + +OpenAI 所对应的 `type` 为 `openai`。它并无特有的配置字段。 + +#### Azure OpenAI + +Azure OpenAI 所对应的 `type` 为 `azure`。它特有的配置字段如下: + +| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 | +|-------------------|--------|------|-----|----------------------------------------------| +| `azureServiceUrl` | string | 必填 | - | Azure OpenAI 服务的 URL,须包含 `api-version` 查询参数。 | + +**注意:** Azure OpenAI 只支持配置一个 API Token。 + +#### 月之暗面(Moonshot) + +月之暗面所对应的 `type` 为 `moonshot`。它特有的配置字段如下: + +| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 | +|------------------|--------|------|-----|-------------------------------------------------------------| +| `moonshotFileId` | string | 非必填 | - | 通过文件接口上传至月之暗面的文件 ID,其内容将被用做 AI 对话的上下文。不可与 `context` 字段同时配置。 | + +#### 通义千问(Qwen) + +通义千问所对应的 `type` 为 `qwen`。它并无特有的配置字段。 + +## 用法示例 + +### 使用 OpenAI 协议代理 Azure OpenAI 服务 + +使用最基本的 Azure OpenAI 服务,不配置任何上下文。 + +**配置信息** + +```yaml +provider: + type: azure + apiTokens: + - "YOUR_AZURE_OPENAI_API_TOKEN" + azureServiceUrl: "https://YOUR_RESOURCE_NAME.openai.azure.com/openai/deployments/YOUR_DEPLOYMENT_NAME/chat/completions?api-version=2024-02-15-preview", +``` + +**请求示例** + +```json +{ + "model": "gpt-3", + "messages": [ + { + "role": "user", + "content": "你好,你是谁?" + } + ], + "temperature": 0.3 +} +``` + +**响应示例** + +```json +{ + "choices": [ + { + "content_filter_results": { + "hate": { + "filtered": false, + "severity": "safe" + }, + "self_harm": { + "filtered": false, + "severity": "safe" + }, + "sexual": { + "filtered": false, + "severity": "safe" + }, + "violence": { + "filtered": false, + "severity": "safe" + } + }, + "finish_reason": "stop", + "index": 0, + "logprobs": null, + "message": { + "content": "你好!我是一个AI助手,可以回答你的问题和提供帮助。有什么我可以帮到你的吗?", + "role": "assistant" + } + } + ], + "created": 1714807624, + "id": "chatcmpl-abcdefg1234567890", + "model": "gpt-35-turbo-16k", + "object": "chat.completion", + "prompt_filter_results": [ + { + "prompt_index": 0, + "content_filter_results": { + "hate": { + "filtered": false, + "severity": "safe" + }, + "self_harm": { + "filtered": false, + "severity": "safe" + }, + "sexual": { + "filtered": false, + "severity": "safe" + }, + "violence": { + "filtered": false, + "severity": "safe" + } + } + } + ], + "system_fingerprint": null, + "usage": { + "completion_tokens": 40, + "prompt_tokens": 15, + "total_tokens": 55 + } +} +``` + +### 使用 OpenAI 协议代理通义千问服务 + +使用通义千问服务,并配置从 OpenAI 大模型到通义千问的模型映射关系。 + +**配置信息** + +```yaml +provider: + type: qwen + apiTokens: + - "YOUR_QWEN_API_TOKEN" + modelMapping: + 'gpt-3': "qwen-turbo" + 'gpt-35-turbo': "qwen-plus" + 'gpt-4-turbo': "qwen-max" + '*': "qwen-turbo" +``` + +**请求示例** + +```json +{ + "model": "gpt-3", + "messages": [ + { + "role": "user", + "content": "你好,你是谁?" + } + ], + "temperature": 0.3 +} +``` + +**响应示例** + +```json +{ + "id": "c2518bd3-0f46-97d1-be34-bb5777cb3108", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "我是通义千问,由阿里云开发的AI助手。我可以回答各种问题、提供信息和与用户进行对话。有什么我可以帮助你的吗?" + }, + "finish_reason": "stop" + } + ], + "created": 1715175072, + "model": "qwen-turbo", + "object": "chat.completion", + "usage": { + "prompt_tokens": 24, + "completion_tokens": 33, + "total_tokens": 57 + } +} +``` + +### 使用通义千问配合纯文本上下文信息 + +使用通义千问服务,同时配置纯文本上下文信息。 + +**配置信息** + +```yaml +provider: + type: qwen + apiTokens: + - "YOUR_QWEN_API_TOKEN" + modelMapping: + "*": "qwen-turbo" + context: + - fileUrl: "http://file.default.svc.cluster.local/ai/context.txt", + serviceName: "file.dns", + servicePort: 80 +``` + +**请求示例** + +```json +{ + "model": "gpt-3", + "messages": [ + { + "role": "user", + "content": "请概述文案内容" + } + ], + "temperature": 0.3 +} +``` + +**响应示例** + +```json +{ + "id": "cmpl-77861a17681f4987ab8270dbf8001936", + "object": "chat.completion", + "created": 9756990, + "model": "moonshot-v1-128k", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "这份文案是一份关于..." + }, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 20181, + "completion_tokens": 439, + "total_tokens": 20620 + } +} +``` + +### 使用月之暗面配合其原生的文件上下文 + +提前上传文件至月之暗面,以文件内容作为上下文使用其 AI 服务。 + +**配置信息** + +```yaml +provider: + type: moonshot + apiTokens: + - "YOUR_MOONSHOT_API_TOKEN" + moonshotFileId: "YOUR_MOONSHOT_FILE_ID", + modelMapping: + '*': "moonshot-v1-32k" +``` + +**请求示例** + +```json +{ + "model": "gpt-4-turbo", + "messages": [ + { + "role": "user", + "content": "请概述文案内容" + } + ], + "temperature": 0.3 +} +``` + +**响应示例** + +```json +{ + "id": "cmpl-e5ca873642ca4f5d8b178c1742f9a8e8", + "object": "chat.completion", + "created": 1872961, + "model": "moonshot-v1-128k", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "文案内容是关于一个名为“xxxx”的支付平台..." + }, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 11, + "completion_tokens": 498, + "total_tokens": 509 + } +} +``` diff --git a/backend/sdk/src/main/resources/plugins/ai-proxy/spec.yaml b/backend/sdk/src/main/resources/plugins/ai-proxy/spec.yaml new file mode 100644 index 00000000..48fb51c5 --- /dev/null +++ b/backend/sdk/src/main/resources/plugins/ai-proxy/spec.yaml @@ -0,0 +1,142 @@ +apiVersion: 1.0.0 +info: + category: custom + name: ai-proxy + title: AI代理 + x-title-i18n: + zh-CN: AI代理 + description: 通过AI助手提供智能对话服务 + x-description-i18n: + zh-CN: 通过AI助手提供智能对话服务 + iconUrl: https://img.alicdn.com/imgextra/i1/O1CN018iKKih1iVx287RltL_!!6000000004419-2-tps-42-42.png + version: 1.0.0 + contact: + name: CH3CHO + url: https://github.com/CH3CHO + email: ch3cho@qq.com +spec: + phase: UNSPECIFIED_PHASE + priority: 100 + configSchema: + openAPIV3Schema: + type: object + required: + - providerConfig + properties: + provider: + type: object + title: AI服务提供商配置 + x-title-i18n: + zh-CN: AI服务提供商配置 + description: AI服务提供商配置,包含API接口、模型和知识库文件等信息 + x-description-i18n: + zh-CN: AI服务提供商配置,包含API接口、模型和知识库文件等信息 + required: + - typ + properties: + apiToken: + type: array + title: API Tokens + x-title-i18n: + zh-CN: API Tokens + description: 在请求AI服务时用于认证的API Token列表。不同的AI服务提供商可能有不同的名称。部分供应商只支持配置一个API Token(如Azure OpenAI)。 + x-description-i18n: + zh-CN: 在请求AI服务时用于认证的API Token列表。不同的AI服务提供商可能有不同的名称。部分供应商只支持配置一个API Token(如Azure OpenAI)。 + items: + type: array + title: apiToken + x-title-i18n: + en-US: apiToken + azureServiceUrl: + type: string + title: Azure OpenAI Service URL + x-title-i18n: + zh-CN: Azure OpenAI Service URL + description: 仅适用于Azure OpenAI服务。要请求的OpenAI服务的完整URL,包含api-version等参数 + x-description-i18n: + zh-CN: 仅适用于Azure OpenAI服务。要请求的OpenAI服务的完整URL,包含api-version等参数 + context: + type: object + title: 模型对话上下文 + x-title-i18n: + zh-CN: 模型对话上下文 + description: 配置一个外部获取对话上下文的文件来源,用于在AI请求中补充对话上下文 + x-description-i18n: + zh-CN: 配置一个外部获取对话上下文的文件来源,用于在AI请求中补充对话上下文 + required: + - fileUrl + - serviceName + - servicePort + properties: + serviceName: + type: string + title: 上游服务名称 + x-title-i18n: + zh-CN: 上游服务名称 + description: 文件服务所对应的网关内上游服务名称 + x-description-i18n: + zh-CN: 文件服务所对应的网关内上游服务名称 + servicePort: + type: integer + title: 上游服务端口 + x-title-i18n: + zh-CN: 上游服务端口 + description: 文件服务所对应的网关内上游服务名称 + x-description-i18n: + zh-CN: 文件服务所对应的网关内上游服务名称 + url: + type: string + title: 文件URL + x-title-i18n: + zh-CN: 文件URL + description: 用于获取对话上下文的文件的URL。目前仅支持HTTP和HTTPS协议,纯文本格式文件 + x-description-i18n: + zh-CN: 用于获取对话上下文的文件的URL。目前仅支持HTTP和HTTPS协议,纯文本格式文件 + modelMapping: + type: object + title: 模型名称映射表 + x-title-i18n: + zh-CN: 模型名称映射表 + description: 用于将请求中的模型名称映射为目标AI服务商支持的模型名称。支持通过“*”来配置全局映射 + x-description-i18n: + zh-CN: 用于将请求中的模型名称映射为目标AI服务商支持的模型名称。支持通过“*”来配置全局映射 + additionalProperties: + type: string + moonshotFileId: + type: string + title: Moonshot File ID + x-title-i18n: + zh-CN: Moonshot File ID + description: 仅适用于Moonshot AI服务。Moonshot AI服务的文件 ID,其内容用于补充 AI 请求上下文 + x-description-i18n: + zh-CN: 仅适用于Moonshot AI服务。Moonshot AI服务的文件 ID,其内容用于补充 AI 请求上下文 + protocol: + type: string + title: 对外接口协议 + x-title-i18n: + zh-CN: 对外接口协议 + description: 通过本插件对外提供的AI服务接口协议。默认值为“openai”,即OpenAI的接口协议。如需保留原有接口协议,可配置为“original" + x-description-i18n: + zh-CN: 通过本插件对外提供的AI服务接口协议。默认值为“openai”,即OpenAI的接口协议。如需保留原有接口协议,可配置为“original" + timeout: + type: integer + title: 请求超时 + x-title-i18n: + zh-CN: 请求超时 + description: 请求AI服务的超时时间,单位为毫秒。默认值为120000,即2分钟 + x-description-i18n: + zh-CN: 请求AI服务的超时时间,单位为毫秒。默认值为120000,即2分钟 + type: + type: string + title: AI服务提供商 + x-title-i18n: + zh-CN: AI服务提供商 + description: AI服务提供商类型,目前支持的取值为:"moonshot"、"qwen"、"openai"、"azure" + x-description-i18n: + zh-CN: AI服务提供商类型,目前支持的取值为:"moonshot"、"qwen"、"openai"、"azure" + example: + provider: + apiToken: YOUR_DASHSCOPE_API_TOKEN + modelMapping: + '*': qwen-turbo + type: qwen diff --git a/backend/sdk/src/main/resources/plugins/plugins.properties b/backend/sdk/src/main/resources/plugins/plugins.properties index 27bc90ce..8efe24ed 100644 --- a/backend/sdk/src/main/resources/plugins/plugins.properties +++ b/backend/sdk/src/main/resources/plugins/plugins.properties @@ -11,6 +11,7 @@ # specific language governing permissions and limitations under the License. # +ai-proxy=oci://higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/ai-proxy:1.0.0 basic-auth=oci://higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/basic-auth:1.0.0 key-auth=oci://higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/key-auth:1.0.0 hmac-auth=oci://higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/hmac-auth:1.0.0