-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathmain.go
More file actions
190 lines (168 loc) · 5.14 KB
/
Copy pathmain.go
File metadata and controls
190 lines (168 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2023-2026 Beijing Jingdong Shangke Information Technology Co., Ltd.
package main
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/google/uuid"
)
const (
AK = "YOUR_AK"
SK = "YOUR_SK"
)
const (
LLM_SEC_DETECT_BASE_URL = "http://safety-api:8007/llmsec/api/defense/v2"
BUSINESS_TYPE_TO_B = "toB"
RESPONSE_MODE_SYNC = "sync"
RESPONSE_MODE_FREE_TAXI = "free_taxi"
CONTENT_TYPE_TEXT = "text"
ROLE_ROBOT = "robot"
ROLE_USER = "user"
ACCESS_TARGET_DEFENSE_V2 = "defenseV2"
PARAM_KEY_ACCESS_KEY = "accessKey"
PARAM_KEY_ACCESS_TARGET = "accessTarget"
PARAM_KEY_REQUEST_ID = "requestId"
PARAM_KEY_TIMESTAMP = "timestamp"
PARAM_KEY_PLAIN_TEXT = "plainText"
MESSAGE_TYPE_INPUT = "input"
MESSAGE_TYPE_OUTPUT = "output"
)
type llmSecDetectReq struct {
RequestID string `json:"requestId"`
Timestamp int64 `json:"timestamp"`
AccessKey string `json:"accessKey"`
PlainText string `json:"plainText"`
Signature string `json:"signature"`
BusinessType string `json:"businessType"`
ResponseMode string `json:"responseMode"`
ContentType string `json:"contentType"`
Content string `json:"content"`
MessageInfo messageInfo `json:"messageInfo"`
}
type messageInfo struct {
SessionID string `json:"sessionId"`
MessageID int `json:"messageId"`
SliceID int `json:"sliceId"`
FromRole string `json:"fromRole"`
FromID string `json:"fromId"`
ToRole string `json:"toRole"`
ToID string `json:"toId"`
Ext map[string]interface{} `json:"ext"`
}
type llmSecDetectResp struct {
Code int `json:"code"`
Message string `json:"message"`
Cost int `json:"cost"`
Data []struct {
Requests []struct {
SessionID string `json:"sessionId"`
MessageID int `json:"messageId"`
SliceID int `json:"sliceId"`
FromRole string `json:"fromRole"`
FromID string `json:"fromId"`
ToRole string `json:"toRole"`
ToID string `json:"toId"`
Ext map[string]interface{} `json:"ext"`
} `json:"requests"`
CheckedContent string `json:"checkedContent"`
RiskCode int `json:"riskCode"`
RiskMessage string `json:"riskMessage"`
RiskCheckType string `json:"riskCheckType"`
RiskCheckName string `json:"riskCheckName"`
RiskCheckResult struct {
Type string `json:"type"`
RiskCode int `json:"riskCode"`
RiskMessage string `json:"riskMessage"`
SrcName string `json:"srcName"`
CheckedContent string `json:"checkedContent"`
Cost int `json:"cost"`
Probability float32 `json:"probability"`
Detail []struct {
RiskCode int `json:"riskCode"`
RiskMessage string `json:"riskMessage"`
Probability float64 `json:"probability"`
} `json:"detail"`
} `json:"riskCheckResult"`
} `json:"data"`
}
func main() {
result, err := LLMSecDetect(MESSAGE_TYPE_INPUT, "测试文本")
if err != nil {
panic(err)
}
fmt.Printf("%+v", result)
}
func LLMSecDetect(messageType string, text string) (*llmSecDetectResp, error) {
req := llmSecDetectReq{
RequestID: uuid.New().String(),
Timestamp: time.Now().UnixMilli(),
AccessKey: AK,
BusinessType: BUSINESS_TYPE_TO_B,
ResponseMode: RESPONSE_MODE_SYNC,
ContentType: CONTENT_TYPE_TEXT,
Content: text,
PlainText: text,
MessageInfo: messageInfo{},
}
if messageType == MESSAGE_TYPE_INPUT {
req.MessageInfo.FromRole = ROLE_USER
req.MessageInfo.ToRole = ROLE_ROBOT
} else {
req.MessageInfo.FromRole = ROLE_ROBOT
req.MessageInfo.ToRole = ROLE_USER
}
req.Signature = genSignature(req)
data, err := json.Marshal(req)
if err != nil {
return nil, err
}
url := fmt.Sprintf(
"%s/%s",
LLM_SEC_DETECT_BASE_URL,
AK)
r, err := http.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
return nil, err
}
defer r.Body.Close()
var resp llmSecDetectResp
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &resp); err != nil {
return nil, err
}
if resp.Code != 0 {
return nil, errors.New(resp.Message)
}
return &resp, nil
}
func genSignature(req llmSecDetectReq) string {
sequence := []string{
PARAM_KEY_ACCESS_KEY, PARAM_KEY_ACCESS_TARGET, PARAM_KEY_REQUEST_ID,
PARAM_KEY_TIMESTAMP, PARAM_KEY_PLAIN_TEXT}
m := map[string]string{
PARAM_KEY_ACCESS_KEY: req.AccessKey,
PARAM_KEY_ACCESS_TARGET: ACCESS_TARGET_DEFENSE_V2,
PARAM_KEY_REQUEST_ID: req.RequestID,
PARAM_KEY_TIMESTAMP: fmt.Sprintf("%d", req.Timestamp),
PARAM_KEY_PLAIN_TEXT: req.PlainText,
}
var params []string
for _, key := range sequence {
params = append(params, fmt.Sprintf("%s=%s", key, m[key]))
}
mac := hmac.New(sha1.New, []byte(SK))
mac.Write([]byte(strings.Join(params, "&")))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}