-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkimi.go
97 lines (84 loc) · 3.83 KB
/
kimi.go
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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
// 定义请求参数结构体
type ChatCompletionRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Temperature float32 `json:"temperature"`
}
// 定义消息结构体
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
// 定义响应结果结构体
type ChatCompletionResponse struct {
Choices []Choice `json:"choices"`
}
type Choice struct {
Message Message `json:"message"`
}
func kimi(respA, respB string) (string, error) {
// 设置 API Key 和请求 URL
apiURL := "https://api.moonshot.cn/v1/chat/completions"
// 创建请求参数
request := ChatCompletionRequest{
Model: "moonshot-v1-8k",
Messages: []Message{
{
Role: "system",
Content: "{\"role\": \"你是一个AI,负责通过比较两个HTTP响应数据包来检测潜在的越权行为,并自行做出判断。\",\"inputs\": {\"responseA\": \"账号A请求某接口的响应。\",\"responseB\": \"将响应A中的Cookie替换为账号B的Cookie后,重放请求得到的响应。\"},\"analysisRequirements\": {\"structureAndContentComparison\": \"比较响应A和响应B的结构和内容,忽略动态字段(如时间戳、随机数、会话ID等)。\",\"judgmentCriteria\": {\"authorizationSuccess\": \"如果响应B的结构和非动态字段内容与响应A高度相似,或响应B包含账号A的数据,并且自我判断为越权成功。\",\"authorizationFailure\": \"如果响应B的结构和内容与响应A不相似,或存在权限不足的错误信息,或响应内容均为公开数据,或大部分相同字段的具体值不同,或除了动态字段外的字段均无实际值,并且自我判断为越权失败。\",\"unknown\": \"其他情况,或无法确定是否存在越权,并且自我判断为无法确定。\"}},\"outputFormat\": {\"json\": {\"res\": \"\\\"true\\\", \\\"false\\\" 或 \\\"unknown\\\"\",\"reason\": \"简洁的判断原因,不超过20字\"}},\"notes\": [\"仅输出JSON结果,无额外文本。\",\"确保JSON格式正确,便于后续处理。\",\"保持客观,仅根据响应内容进行分析。\"],\"process\": [\"接收并理解响应A和响应B。\",\"分析响应A和响应B,忽略动态字段。\",\"基于响应的结构、内容和相关性进行自我判断,包括但不限于:\",\"- 识别响应中可能的敏感数据或权限信息。\",\"- 评估响应与预期结果之间的一致性。\",\"- 确定是否存在明显的越权迹象。\",\"输出指定格式的JSON结果,包括判断和判断原因。\"]}",
},
{
Role: "user",
Content: "A:" + respA + "B:" + respB,
},
},
Temperature: 0.3,
}
// 将请求参数编码为 JSON
jsonData, err := json.Marshal(request)
if err != nil {
fmt.Println("JSON 编码失败:", err)
return "", err
}
// 创建 HTTP 请求
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("创建请求失败:", err)
return "", err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("发送请求失败:", err)
return "", err
}
defer resp.Body.Close()
// 解析响应结果
var response ChatCompletionResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
fmt.Println("解析响应失败:", err)
return "", err
}
// 输出回复内容
if len(response.Choices) > 0 {
result := response.Choices[0].Message.Content
return result, nil
} else {
// 处理 Choices 为空的情况,例如返回一个默认值或错误
return "", errors.New("error: choices is empty")
}
// result := response.Choices[0].Message.Content
// return result, nil
}