-
Notifications
You must be signed in to change notification settings - Fork 240
/
Copy pathtestPluginDemo.js
199 lines (186 loc) · 7.63 KB
/
testPluginDemo.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
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
191
192
193
194
195
196
197
198
199
const https = require("https");
const http = require("http");
// 请求接口的option对象
const options = {
'testPlugin': {
storage:[]
// 自己定义你想要存消息的格式, 例如:
// [{"role": "user", "content": message}]
// 清空的时候自己定义清空的函数
,
displayName: '测试插件',
name: 'testPlugin',
url: 'https://localhost/api/chat-stream',
method: 'POST',
protocol: 'http',
type: 'plugin',
headers: {
'Content-Type': 'application/json',
// 'Content-Length': Buffer.byteLength(postData),
'authority': 'localhost',
'path': "v1/chat/completions",
'accept': '*/*',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
'origin': 'http://localhost',
'pragma': 'no-cache',
'referer': 'https://localhost/',
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
},
options: function () {
return {
hostname: 'localhost',
path: '/api/chat-stream',
method: 'POST',
headers: {
'Content-Type': 'application/json',
// 'Content-Length': Buffer.byteLength(postData),
'authority': 'localhost',
'path': "v1/chat/completions",
'accept': '*/*',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
'origin': 'https://localhost',
'pragma': 'no-cache',
'referer': 'https://localhost/',
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
}
}
},
body: function (messages) {
return JSON.stringify({
messages: messages,
stream: true,
model: 'gpt-3.5-turbo',
temperature: 1,
presence_penalty: 0,
})
},
// 入参,event为回显事件,messages为当前聊天记录,格式[{"role": "user", "content": message}],message为当前输入的消息
fn: function (controller,event, messages, message) {
const postData = JSON.stringify({
messages: messages,
stream: true,
model: 'gpt-3.5-turbo',
temperature: 1,
presence_penalty: 0,
});
const options = {
hostname: 'localhost',
path: '/api/chat-stream',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
'authority': 'localhost',
'accept': '*/*',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
'origin': 'http://localhost',
'path': "v1/chat/completions",
'pragma': 'no-cache',
'referer': 'http://localhost/',
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
}
};
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
let data = '';
res.setEncoding('utf8');
// 接收响应数据
res.on('data', (chunk) => {
data += chunk;
event.sender.send('reply', data)
});
res.on('end', () => {
this.storage.push({"role": "assistant", "content": data})
event.sender.send('reply', data + '\n**end**')
})
});
req.on('error', (error) => {
console.error(error);
});
// 发送请求数据
req.write(postData);
req.end();
},
testSpeedFn: function (callback) {
const postData = JSON.stringify({
messages: messages,
stream: true,
model: 'gpt-3.5-turbo',
temperature: 1,
presence_penalty: 0,
});
const options = {
hostname: 'localhost',
path: '/api/chat-stream',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
'authority': 'localhost',
'accept': '*/*',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
'origin': 'http://localhost',
'path': "v1/chat/completions",
'pragma': 'no-cache',
'referer': 'http://localhost/',
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
}
};
const start = new Date().getTime();
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
let data = '';
res.setEncoding('utf8');
// 接收响应数据
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
const end = new Date().getTime();
const time = end - start;
callback(time, res)
})
});
req.on('error', (error) => {
console.error(error);
});
// 发送请求数据
req.write(postData);
req.end();
},
clear: function () {
},
testSpeed: 1,
}
};
module.exports = {
options: options,
};