Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ai security guard #1261

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Conversation

rinfx
Copy link
Collaborator

@rinfx rinfx commented Aug 29, 2024

更新内容安全插件:

  • 配置更新:用户可以选择只在request/response阶段检测,或者都检测。如果被内容安全拦截,根据请求是否为流式返回流式/非流式响应。如果response阶段进行检测,虽然响应是流式的,但是需要缓存所有的chunk提取大模型的完整回答进行检测,所以在前端页面上表现为非流式。
  • 增加metrics以及span attributes供用户排查问题使用。

@codecov-commenter
Copy link

codecov-commenter commented Aug 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 44.31%. Comparing base (ef31e09) to head (c73ed77).
Report is 90 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1261      +/-   ##
==========================================
+ Coverage   35.91%   44.31%   +8.40%     
==========================================
  Files          69       75       +6     
  Lines       11576     9823    -1753     
==========================================
+ Hits         4157     4353     +196     
+ Misses       7104     5142    -1962     
- Partials      315      328      +13     

see 90 files with indirect coverage changes

| `accessKey` | string | requried | - | Aliyun accesskey |
| `secretKey` | string | requried | - | Aliyun secretkey |
| `checkRequest` | bool | optional | false | check if the input is leagal |
| `checkResponse` | bool | optional | false | check if the output is leagal |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

legal 拼写错误。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢董老师捉虫

## 配置示例
### check if the input is leagal
## Examples of configuration
### Check if the input is leagal
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

拼写问题同上

| `accessKey` | string | requried | - | 阿里云AK |
| `secretKey` | string | requried | - | 阿里云SK |
| `checkRequest` | bool | optional | false | 检查提问内容是否合规 |
| `checkResponse` | bool | optional | false | 检查大模型的回答内容是否合规,生效时会使流式响应变为非流式 |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

生效时会使流式响应变为非流式 这个可能会影响客户端的行为。是否有办法用流式响应返回对应的结果呢?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个倒不是技术上的问题,如果要流式返回的话,一个响应要调很多次阿里云内容安全服务了,成本太高了

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦,技术上其实也有问题,stream里面发起http请求时,不能够返回actionpause等待请求执行完

config.incrementCounter("ai_sec_request_deny", 1)
proxywasm.SendHttpResponse(200, [][2]string{{"content-type", "text/event-stream;charset=UTF-8"}}, jsonData, -1)
} else {
jsonData := []byte(fmt.Sprintf(NormalResponseFormat, "很抱歉,我不能对您的问题做出回答。"))
jsonData := []byte(fmt.Sprintf(NormalResponseFormat, DefaultResponseIfNoAdvice))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种是不是没必要每次单独构造?

@@ -288,7 +289,7 @@ func onHttpResponseBody(ctx wrapper.HttpContext, config AISecurityConfig, body [
reqParams.Add(k, v)
}
reqParams.Add("Signature", signature)
config.client.Post(fmt.Sprintf("/?%s", reqParams.Encode()), nil, nil,
config.client.Post(fmt.Sprintf("/?%s", reqParams.Encode()), [][2]string{{"User-Agent", "CIPFrom/AIGateway"}}, nil,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UA 弄个常量吧

} else {
jsonData = []byte(fmt.Sprintf(NormalResponseFormat, "很抱歉,我不能对您的问题做出回答。"))
jsonData = []byte(fmt.Sprintf(NormalResponseFormat, DefaultResponseIfNoAdvice))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

counter = proxywasm.DefineCounterMetric(metricName)
config.metrics[metricName] = counter
}
counter.Increment(inc)
}

type StandardResponse struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 struct 没有用到了吗?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个确实没再用到了,我删除一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants