|
| 1 | +const https = require("https"); |
| 2 | +const http = require("http"); |
| 3 | +// 请求接口的option对象 |
| 4 | +const options = { |
| 5 | + '105': { |
| 6 | + displayName: '测试插件105', |
| 7 | + name: '105', |
| 8 | + url: 'https://154.40.59.105:3006/api/chat-process', |
| 9 | + method: 'POST', |
| 10 | + protocol: 'https', |
| 11 | + type: 'plugin', |
| 12 | + parentMessageId : '', |
| 13 | + conversationId : '', |
| 14 | + headers: { |
| 15 | + 'Accept': 'application/json, text/plain, */*', |
| 16 | + 'Accept-Language': 'zh-CN,zh;q=0.9', |
| 17 | + 'Cache-Control': 'no-cache', |
| 18 | + 'Content-Type': 'application/json', |
| 19 | + 'Origin': 'https://154.40.59.105:3006', |
| 20 | + 'Pragma': 'no-cache', |
| 21 | + 'Proxy-Connection': 'keep-alive', |
| 22 | + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36', |
| 23 | + }, |
| 24 | + body: function (message, parentMessageId) { |
| 25 | + return JSON.stringify({ |
| 26 | + prompt: message, |
| 27 | + options: { |
| 28 | + parentMessageId: parentMessageId |
| 29 | + }, |
| 30 | + systemMessage: 'You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.\nKnowledge cutoff: 2021-09-01\nCurrent date: 2023-03-29', |
| 31 | + temperature: 0.8, |
| 32 | + top_p: 1, |
| 33 | + }) |
| 34 | + }, |
| 35 | + fn: function (controller,event,messages, message) { |
| 36 | + const opt = Object.assign({ |
| 37 | + method: this.method, |
| 38 | + headers: this.headers, |
| 39 | + }, { |
| 40 | + signal: controller.signal, |
| 41 | + }); |
| 42 | + |
| 43 | + let req = null |
| 44 | + |
| 45 | + const httpCallBack = (res) => { |
| 46 | + let data = ''; |
| 47 | + let reply = '' |
| 48 | + let endJson = {} |
| 49 | + res.on('data', (chunk) => { |
| 50 | + try { |
| 51 | + data += chunk; |
| 52 | + let strings = data.split('&KFw6loC9Qvy&'); |
| 53 | + if (strings.length > 1) { |
| 54 | + reply = strings[1] |
| 55 | + }else if (strings.length > 2){ |
| 56 | + endJson = JSON.parse(strings[2]) |
| 57 | + } |
| 58 | + console.log(reply); |
| 59 | + event.sender.send('reply', reply) |
| 60 | + |
| 61 | + } catch (e) { |
| 62 | + console.error(e) |
| 63 | + } |
| 64 | + }); |
| 65 | + res.on('end', () => { |
| 66 | + try { |
| 67 | + this.parentMessageId = endJson.id |
| 68 | + if (endJson.conversationId){ |
| 69 | + this.conversationId = endJson.conversationId |
| 70 | + } |
| 71 | + messages.push({"role": "assistant", "content": reply}) |
| 72 | + event.sender.send('reply', reply + '\n**end**') |
| 73 | + } catch (e) { |
| 74 | + console.error(e) |
| 75 | + } |
| 76 | + |
| 77 | + }); |
| 78 | + req.on('error', (error) => { |
| 79 | + // 判断如果是取消 |
| 80 | + if (error.name === 'AbortError') { |
| 81 | + console.log('请求被取消了') |
| 82 | + event.sender.send('reply', data + '\n**end**') |
| 83 | + return |
| 84 | + } |
| 85 | + console.error(error); |
| 86 | + }); |
| 87 | + } |
| 88 | + if ("https" === this.protocol) { |
| 89 | + req = https.request(this.url, opt, httpCallBack); |
| 90 | + } else { |
| 91 | + req = http.request(this.url, opt, httpCallBack); |
| 92 | + } |
| 93 | + const postData = this.body(message, this.parentMessageId,this.conversationId); |
| 94 | + |
| 95 | + req.write(postData); |
| 96 | + req.end(); |
| 97 | + }, |
| 98 | + testSpeedFn: function (callback) { |
| 99 | + const opt = { |
| 100 | + method: this.method, |
| 101 | + headers: this.headers, |
| 102 | + }; |
| 103 | + |
| 104 | + let req = null |
| 105 | + const start = new Date().getTime(); |
| 106 | + const httpCallBack = (res) => { |
| 107 | + let data = ''; |
| 108 | + res.on('data', (chunk) => { |
| 109 | + try { |
| 110 | + data += chunk; |
| 111 | + |
| 112 | + } catch (e) { |
| 113 | + console.error(e) |
| 114 | + } |
| 115 | + }); |
| 116 | + res.on('end', () => { |
| 117 | + try { |
| 118 | + const end = new Date().getTime(); |
| 119 | + const time = end - start; |
| 120 | + callback(time, res) |
| 121 | + } catch (e) { |
| 122 | + console.error(e) |
| 123 | + } |
| 124 | + |
| 125 | + }); |
| 126 | + req.on('error', (error) => { |
| 127 | + // 判断如果是取消 |
| 128 | + callback(null, res) |
| 129 | + console.error(error); |
| 130 | + }); |
| 131 | + } |
| 132 | + if ("https" === this.protocol) { |
| 133 | + req = https.request(this.url, opt, httpCallBack); |
| 134 | + } else { |
| 135 | + req = http.request(this.url, opt, httpCallBack); |
| 136 | + } |
| 137 | + const postData = this.body('你好', '',''); |
| 138 | + req.write(postData); |
| 139 | + req.end(); |
| 140 | + }, |
| 141 | + testSpeed: 1, |
| 142 | + } |
| 143 | +}; |
| 144 | + |
| 145 | + |
| 146 | +module.exports = { |
| 147 | + options: options, |
| 148 | +}; |
0 commit comments