diff --git a/backend/sdk/src/main/resources/plugins/ai-security-guard/README.md b/backend/sdk/src/main/resources/plugins/ai-security-guard/README.md index 5a8f753f..122d9e36 100644 --- a/backend/sdk/src/main/resources/plugins/ai-security-guard/README.md +++ b/backend/sdk/src/main/resources/plugins/ai-security-guard/README.md @@ -28,8 +28,21 @@ description: 阿里云内容安全检测 | `responseContentJsonPath` | string | optional | `choices.0.message.content` | 指定要检测内容在响应body中的jsonpath | | `responseStreamContentJsonPath` | string | optional | `choices.0.delta.content` | 指定要检测内容在流式响应body中的jsonpath | | `denyCode` | int | optional | 200 | 指定内容非法时的响应状态码 | -| `denyMessage` | string | optional | openai格式的流失/非流式响应,回答内容为阿里云内容安全的建议回答 | 指定内容非法时的响应内容 | - +| `denyMessage` | string | optional | openai格式的流式/非流式响应 | 指定内容非法时的响应内容 | +| `protocol` | string | optional | openai | 协议格式,非openai协议填`original` | + +补充说明一下 `denyMessage`,对于openai格式的请求,对非法请求的处理逻辑为: +- 如果配置了 `denyMessage` + - 优先返回阿里云内容安全的建议回答,格式为openai格式的流式/非流式响应 + - 如果阿里云内容安全未返回建议的回答,返回内容为 `denyMessage` 配置内容,格式为openai格式的流式/非流式响应 +- 如果没有配置 `denyMessage` + - 优先返回阿里云内容安全的建议回答,格式为openai格式的流式/非流式响应 + - 如果阿里云内容安全未返回建议的回答,返回内容为内置的兜底回答,内容为`"很抱歉,我无法回答您的问题"`,格式为openai格式的流式/非流式响应 + +如果用户使用了非openai格式的协议,应当配置 `denyMessage`,此时对非法请求的处理逻辑为: +- 返回用户配置的 `denyMessage` 内容,用户可以配置其为序列化后的json字符串,以保持与正常请求接口返回格式的一致性 +- 如果 `denyMessage` 为空,优先返回阿里云内容安全的建议回答,格式为纯文本 +- 如果阿里云内容安全未返回建议回答,返回内置的兜底回答,内容为`"很抱歉,我无法回答您的问题"`,格式为纯文本 ## 配置示例 ### 前提条件 @@ -91,6 +104,7 @@ requestContentJsonPath: "input.prompt" responseContentJsonPath: "output.text" denyCode: 200 denyMessage: "很抱歉,我无法回答您的问题" +protocol: original ``` ## 可观测 @@ -123,7 +137,7 @@ curl http://localhost/v1/chat/completions \ ```json { - "id": "chatcmpl-123", + "id": "chatcmpl-AAy3hK1dE4ODaegbGOMoC9VY4Sizv", "object": "chat.completion", "created": 1677652288, "model": "gpt-4o-mini", diff --git a/backend/sdk/src/main/resources/plugins/ai-security-guard/README_EN.md b/backend/sdk/src/main/resources/plugins/ai-security-guard/README_EN.md index 450b5541..0367686a 100644 --- a/backend/sdk/src/main/resources/plugins/ai-security-guard/README_EN.md +++ b/backend/sdk/src/main/resources/plugins/ai-security-guard/README_EN.md @@ -29,8 +29,8 @@ Plugin Priority: `300` | `responseContentJsonPath` | string | optional | `choices.0.message.content` | Specify the jsonpath of the content to be detected in the response body | | `responseStreamContentJsonPath` | string | optional | `choices.0.delta.content` | Specify the jsonpath of the content to be detected in the streaming response body | | `denyCode` | int | optional | 200 | Response status code when the specified content is illegal | -| `denyMessage` | string | optional | Drainage/non-streaming response in openai format, the answer content is the suggested answer from Alibaba Cloud content security - | Response content when the specified content is illegal | +| `denyMessage` | string | optional | Drainage/non-streaming response in openai format, the answer content is the suggested answer from Alibaba Cloud content security | Response content when the specified content is illegal | +| `protocol` | string | optional | openai | protocol format, `openai` or `original` | ## Examples of configuration diff --git a/backend/sdk/src/main/resources/plugins/ai-security-guard/spec.yaml b/backend/sdk/src/main/resources/plugins/ai-security-guard/spec.yaml index 6991749e..b2df3c3c 100644 --- a/backend/sdk/src/main/resources/plugins/ai-security-guard/spec.yaml +++ b/backend/sdk/src/main/resources/plugins/ai-security-guard/spec.yaml @@ -22,24 +22,22 @@ spec: openAPIV3Schema: type: object example: - serviceSource: dns - serviceName: safecheck + serviceName: safecheck.dns # 请先创建好相应服务 servicePort: 443 - domain: green-cip.cn-shanghai.aliyuncs.com - ak: xxxxxxxxxxxxxxxxxxxx - sk: xxxxxxxxxxxxxxxxxxxx + serviceHost: green-cip.cn-hangzhou.aliyuncs.com + accessKey: "XXXXXXXXX" + secretKey: "XXXXXXXXXXXXXXX" checkRequest: true checkResponse: false routeConfigSchema: openAPIV3Schema: type: object example: - serviceSource: dns - serviceName: safecheck + serviceName: safecheck.dns # 请先创建好相应服务 servicePort: 443 - domain: green-cip.cn-shanghai.aliyuncs.com - ak: xxxxxxxxxxxxxxxxxxxx - sk: xxxxxxxxxxxxxxxxxxxx + serviceHost: green-cip.cn-hangzhou.aliyuncs.com + accessKey: "XXXXXXXXX" + secretKey: "XXXXXXXXXXXXXXX" checkRequest: true checkResponse: false diff --git a/backend/sdk/src/main/resources/plugins/ai-statistics/spec.yaml b/backend/sdk/src/main/resources/plugins/ai-statistics/spec.yaml index 990b9ac8..84279848 100644 --- a/backend/sdk/src/main/resources/plugins/ai-statistics/spec.yaml +++ b/backend/sdk/src/main/resources/plugins/ai-statistics/spec.yaml @@ -22,9 +22,29 @@ spec: openAPIV3Schema: type: object example: - enable: true routeConfigSchema: openAPIV3Schema: type: object example: - enable: true + # attributes: + # - key: consumer # 配合认证鉴权记录consumer + # value_source: request_header + # value: x-mse-consumer + # apply_to_log: true # 是否将信息添加到日志中 + # apply_to_span: false # 是否将信息添加到链路追踪span attribute中 + # - key: question # 记录问题 + # value_source: request_body + # value: messages.@reverse.0.content + # apply_to_log: true + # apply_to_span: false + # - key: answer # 在流式响应中提取大模型的回答 + # value_source: response_streaming_body + # value: choices.0.delta.content + # rule: append + # apply_to_log: true + # apply_to_span: false + # - key: answer # 在非流式响应中提取大模型的回答 + # value_source: response_body + # value: choices.0.message.content + # apply_to_log: true + # apply_to_span: false