-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
57 lines (49 loc) · 1.52 KB
/
preload.js
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
const axios = require("axios");
const crypto = require("crypto");
const {apiKey} = require("./api-key");
// 密钥信息获取与帮助文档:https://www.aigcaas.cn/article/16.html
const secret_id = apiKey.secret_id; // 密钥信息
const secret_key = apiKey.secret_key; // 密钥信息
const application_name = "chatgpt_chat"; // 应用名称
const api_name = "chat_com"; // 接口名称
// 请求地址
const url = `https://api.aigcaas.cn/product/${application_name}/api/${api_name}`;
/**
* 请求GPT模型生成中文JSDoc的异步函数
* @async
* @function requestGPT
* @param {string} functionCode - 待生成中文JSDoc的代码
* @returns {Promise<Object>} 返回Promise对象,包含GPT模型生成的结果
*/
async function requestGPT(functionCode) {
// 构建请求头
const timestamp = Math.round(new Date() / 1000);
const nonce = 10000 + Math.round(Math.random() * 90000);
const hash = crypto.createHash("sha256");
hash.update(`${timestamp}${secret_key}${nonce}`);
const token = hash.digest("hex");
const headers = {
SecretID: secret_id,
Nonce: nonce,
Token: token,
Timestamp: timestamp,
"Content-Type": "application/json",
};
// 构建请求 body
const data = {
messages:
JSON.stringify([{
"role":"user",
"content":"为以下代码生成中文JSDoc " + functionCode
}])
};
const ret = await axios({
method: "post",
url: url,
headers: headers,
data: JSON.stringify(data),
})
console.log(ret)
return ret
}
window.requestGPT = requestGPT