This repository was archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
85 lines (75 loc) · 2.17 KB
/
init.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
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
var __POST = "%s";
var __ADDR = "http://127.0.0.1:" + __POST;
window.localStorage.getItem = function (key) {
//sync ajax
var xhr = new XMLHttpRequest();
key = encodeURIComponent(key);
xhr.open("GET", `${__ADDR}/localStorage/getItem?key=${key}`, false);
xhr.send();
var data = JSON.parse(xhr.responseText);
return data?.data;
};
window.localStorage.setItem = function (key, value) {
//sync ajax
var xhr = new XMLHttpRequest();
key = encodeURIComponent(key);
value = encodeURIComponent(value);
xhr.open(`${__ADDR}/localStorage/setItem?key=${key}&value=${value}`, false);
xhr.send();
};
window.localStorage.removeItem = function (key) {
//sync ajax
var xhr = new XMLHttpRequest();
key = encodeURIComponent(key);
xhr.open("GET", `${__ADDR}/localStorage/removeItem?key=${key}`, false);
xhr.send();
};
var nlp_model = null;
function checkModel() {
while (true) {
if (!nlp_model || nlp_model == "null") {
nlp_model = prompt("请输入模型路径", localStorage.getItem("nlp_model"));
localStorage.setItem("nlp_model", nlp_model);
} else {
console.log("模型路径", localStorage.getItem("nlp_model"));
break;
}
}
}
var _fetch = window.fetch;
window.fetch = function (url, options) {
if (url.indexOf("/v1/chat/completions") > -1) {
checkModel();
if (options.body) {
var body = JSON.parse(options.body);
body["model"] = nlp_model;
options.body = JSON.stringify(body);
}
}
return _fetch(url, options);
};
ah.proxy({
//请求发起前进入
onRequest: (config, handler) => {
if (config.url.indexOf("/v1/chat/completions") > -1) {
checkModel();
if (config.body) {
var body = JSON.parse(config.body);
body["model"] = nlp_model;
config.body = JSON.stringify(body);
}
}
handler.next(config);
},
//请求发生错误时进入,比如超时;注意,不包括http状态码错误,如404仍然会认为请求成功
onError: (err, handler) => {
console.log(err.type);
handler.next(err);
},
//请求成功后进入
onResponse: (response, handler) => {
console.log(response.response);
handler.next(response);
},
});
checkModel();