|
| 1 | +const https = require("https"); |
| 2 | +const http = require("http"); |
| 3 | +// 请求接口的option对象 |
| 4 | +const options = { |
| 5 | + 'testPlugin': { |
| 6 | + storage:[] |
| 7 | + // 自己定义你想要存消息的格式, 例如: |
| 8 | + // [{"role": "user", "content": message}] |
| 9 | + // 清空的时候自己定义清空的函数 |
| 10 | + , |
| 11 | + displayName: '测试插件', |
| 12 | + name: 'testPlugin', |
| 13 | + url: 'https://localhost/api/chat-stream', |
| 14 | + method: 'POST', |
| 15 | + protocol: 'http', |
| 16 | + type: 'plugin', |
| 17 | + headers: { |
| 18 | + 'Content-Type': 'application/json', |
| 19 | + // 'Content-Length': Buffer.byteLength(postData), |
| 20 | + 'authority': 'localhost', |
| 21 | + 'path': "v1/chat/completions", |
| 22 | + 'accept': '*/*', |
| 23 | + 'accept-language': 'zh-CN,zh;q=0.9', |
| 24 | + 'cache-control': 'no-cache', |
| 25 | + 'origin': 'http://localhost', |
| 26 | + 'pragma': 'no-cache', |
| 27 | + 'referer': 'https://localhost/', |
| 28 | + 'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"', |
| 29 | + 'sec-ch-ua-mobile': '?0', |
| 30 | + 'sec-ch-ua-platform': '"macOS"', |
| 31 | + 'sec-fetch-dest': 'empty', |
| 32 | + 'sec-fetch-mode': 'cors', |
| 33 | + 'sec-fetch-site': 'same-origin', |
| 34 | + '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' |
| 35 | + }, |
| 36 | + options: function () { |
| 37 | + return { |
| 38 | + hostname: 'localhost', |
| 39 | + path: '/api/chat-stream', |
| 40 | + method: 'POST', |
| 41 | + headers: { |
| 42 | + 'Content-Type': 'application/json', |
| 43 | + // 'Content-Length': Buffer.byteLength(postData), |
| 44 | + 'authority': 'localhost', |
| 45 | + 'path': "v1/chat/completions", |
| 46 | + 'accept': '*/*', |
| 47 | + 'accept-language': 'zh-CN,zh;q=0.9', |
| 48 | + 'cache-control': 'no-cache', |
| 49 | + 'origin': 'https://localhost', |
| 50 | + 'pragma': 'no-cache', |
| 51 | + 'referer': 'https://localhost/', |
| 52 | + 'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"', |
| 53 | + 'sec-ch-ua-mobile': '?0', |
| 54 | + 'sec-ch-ua-platform': '"macOS"', |
| 55 | + 'sec-fetch-dest': 'empty', |
| 56 | + 'sec-fetch-mode': 'cors', |
| 57 | + 'sec-fetch-site': 'same-origin', |
| 58 | + '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' |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + body: function (messages) { |
| 63 | + return JSON.stringify({ |
| 64 | + messages: messages, |
| 65 | + stream: true, |
| 66 | + model: 'gpt-3.5-turbo', |
| 67 | + temperature: 1, |
| 68 | + presence_penalty: 0, |
| 69 | + }) |
| 70 | + }, |
| 71 | + // 入参,event为回显事件,messages为当前聊天记录,格式[{"role": "user", "content": message}],message为当前输入的消息 |
| 72 | + fn: function (controller,event, messages, message) { |
| 73 | + const postData = JSON.stringify({ |
| 74 | + messages: messages, |
| 75 | + stream: true, |
| 76 | + model: 'gpt-3.5-turbo', |
| 77 | + temperature: 1, |
| 78 | + presence_penalty: 0, |
| 79 | + }); |
| 80 | + |
| 81 | + const options = { |
| 82 | + hostname: 'localhost', |
| 83 | + path: '/api/chat-stream', |
| 84 | + method: 'POST', |
| 85 | + headers: { |
| 86 | + 'Content-Type': 'application/json', |
| 87 | + 'Content-Length': Buffer.byteLength(postData), |
| 88 | + 'authority': 'localhost', |
| 89 | + 'accept': '*/*', |
| 90 | + 'accept-language': 'zh-CN,zh;q=0.9', |
| 91 | + 'cache-control': 'no-cache', |
| 92 | + 'origin': 'http://localhost', |
| 93 | + 'path': "v1/chat/completions", |
| 94 | + 'pragma': 'no-cache', |
| 95 | + 'referer': 'http://localhost/', |
| 96 | + 'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"', |
| 97 | + 'sec-ch-ua-mobile': '?0', |
| 98 | + 'sec-ch-ua-platform': '"macOS"', |
| 99 | + 'sec-fetch-dest': 'empty', |
| 100 | + 'sec-fetch-mode': 'cors', |
| 101 | + 'sec-fetch-site': 'same-origin', |
| 102 | + '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' |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + const req = https.request(options, (res) => { |
| 107 | + console.log(`statusCode: ${res.statusCode}`); |
| 108 | + let data = ''; |
| 109 | + res.setEncoding('utf8'); |
| 110 | + // 接收响应数据 |
| 111 | + res.on('data', (chunk) => { |
| 112 | + data += chunk; |
| 113 | + event.sender.send('reply', data) |
| 114 | + }); |
| 115 | + |
| 116 | + res.on('end', () => { |
| 117 | + this.storage.push({"role": "assistant", "content": data}) |
| 118 | + event.sender.send('reply', data + '\n**end**') |
| 119 | + |
| 120 | + }) |
| 121 | + }); |
| 122 | + |
| 123 | + req.on('error', (error) => { |
| 124 | + console.error(error); |
| 125 | + }); |
| 126 | + |
| 127 | + // 发送请求数据 |
| 128 | + req.write(postData); |
| 129 | + req.end(); |
| 130 | + }, |
| 131 | + testSpeedFn: function (callback) { |
| 132 | + const postData = JSON.stringify({ |
| 133 | + messages: messages, |
| 134 | + stream: true, |
| 135 | + model: 'gpt-3.5-turbo', |
| 136 | + temperature: 1, |
| 137 | + presence_penalty: 0, |
| 138 | + }); |
| 139 | + |
| 140 | + const options = { |
| 141 | + hostname: 'localhost', |
| 142 | + path: '/api/chat-stream', |
| 143 | + method: 'POST', |
| 144 | + headers: { |
| 145 | + 'Content-Type': 'application/json', |
| 146 | + 'Content-Length': Buffer.byteLength(postData), |
| 147 | + 'authority': 'localhost', |
| 148 | + 'accept': '*/*', |
| 149 | + 'accept-language': 'zh-CN,zh;q=0.9', |
| 150 | + 'cache-control': 'no-cache', |
| 151 | + 'origin': 'http://localhost', |
| 152 | + 'path': "v1/chat/completions", |
| 153 | + 'pragma': 'no-cache', |
| 154 | + 'referer': 'http://localhost/', |
| 155 | + 'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"', |
| 156 | + 'sec-ch-ua-mobile': '?0', |
| 157 | + 'sec-ch-ua-platform': '"macOS"', |
| 158 | + 'sec-fetch-dest': 'empty', |
| 159 | + 'sec-fetch-mode': 'cors', |
| 160 | + 'sec-fetch-site': 'same-origin', |
| 161 | + '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' |
| 162 | + } |
| 163 | + }; |
| 164 | + const start = new Date().getTime(); |
| 165 | + const req = https.request(options, (res) => { |
| 166 | + console.log(`statusCode: ${res.statusCode}`); |
| 167 | + let data = ''; |
| 168 | + res.setEncoding('utf8'); |
| 169 | + // 接收响应数据 |
| 170 | + res.on('data', (chunk) => { |
| 171 | + data += chunk; |
| 172 | + }); |
| 173 | + |
| 174 | + res.on('end', () => { |
| 175 | + const end = new Date().getTime(); |
| 176 | + const time = end - start; |
| 177 | + callback(time, res) |
| 178 | + }) |
| 179 | + }); |
| 180 | + |
| 181 | + req.on('error', (error) => { |
| 182 | + console.error(error); |
| 183 | + }); |
| 184 | + |
| 185 | + // 发送请求数据 |
| 186 | + req.write(postData); |
| 187 | + req.end(); |
| 188 | + }, |
| 189 | + clear: function () { |
| 190 | + |
| 191 | + }, |
| 192 | + testSpeed: 1, |
| 193 | + } |
| 194 | +}; |
| 195 | + |
| 196 | + |
| 197 | +module.exports = { |
| 198 | + options: options, |
| 199 | +}; |
0 commit comments