|
| 1 | +const https = require("https"); |
| 2 | +const http = require("http"); |
| 3 | +// 请求接口的option对象 |
| 4 | +const options = { |
| 5 | + 'demo1': { |
| 6 | + displayName: '演示插件', |
| 7 | + name: 'demo1', |
| 8 | + url: 'https://gpt.aigcfast.com/api/chat-stream', |
| 9 | + method: 'POST', |
| 10 | + protocol: 'https', |
| 11 | + type: 'plugin', |
| 12 | + parentMessageId: '', |
| 13 | + conversationId: '', |
| 14 | + messages:[], |
| 15 | + headers: { |
| 16 | + 'authority': 'gpt.aigcfast.com', |
| 17 | + 'accept': '*/*', |
| 18 | + 'accept-language': 'zh-CN,zh;q=0.9', |
| 19 | + 'cache-control': 'no-cache', |
| 20 | + 'content-type': 'application/json', |
| 21 | + 'origin': 'https://gpt.aigcfast.com', |
| 22 | + 'pragma': 'no-cache', |
| 23 | + 'referer': 'https://gpt.aigcfast.com/', |
| 24 | + 'sec-ch-ua': '"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"', |
| 25 | + 'sec-ch-ua-mobile': '?0', |
| 26 | + 'sec-ch-ua-platform': '"macOS"', |
| 27 | + 'sec-fetch-dest': 'empty', |
| 28 | + 'sec-fetch-mode': 'cors', |
| 29 | + 'sec-fetch-site': 'same-origin', |
| 30 | + 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36', |
| 31 | + 'path': 'v1/chat/completions', |
| 32 | + }, |
| 33 | + body: function (message) { |
| 34 | + return JSON.stringify({ |
| 35 | + messages: [ |
| 36 | + { |
| 37 | + role: 'user', |
| 38 | + content: message, |
| 39 | + } |
| 40 | + ], |
| 41 | + stream: true, |
| 42 | + model: 'gpt-3.5-turbo', |
| 43 | + temperature: 1, |
| 44 | + presence_penalty: 0, |
| 45 | + }) |
| 46 | + }, |
| 47 | + fn: function (controller, event, messages, message) { |
| 48 | + let _this = this; |
| 49 | + // _this.messages.push({"role": "user", "content": message}) |
| 50 | + const opt = Object.assign({ |
| 51 | + method: _this.method, |
| 52 | + headers: _this.headers, |
| 53 | + }, { |
| 54 | + signal: controller.signal, |
| 55 | + }); |
| 56 | + |
| 57 | + let req = null |
| 58 | + |
| 59 | + const httpCallBack = (res) => { |
| 60 | + let data = ''; |
| 61 | + res.on('data', (chunk) => { |
| 62 | + try { |
| 63 | + data += chunk; |
| 64 | + event.sender.send('reply', data) |
| 65 | + |
| 66 | + } catch (e) { |
| 67 | + console.error(e) |
| 68 | + } |
| 69 | + }); |
| 70 | + res.on('end', () => { |
| 71 | + try { |
| 72 | + event.sender.send('reply', data + '\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); |
| 94 | + // log.info('========================' + postData) |
| 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 = JSON.stringify(Object.assign(this.body(),{messages:'你好'})); |
| 138 | + req.write(postData); |
| 139 | + req.end(); |
| 140 | + }, |
| 141 | + clear:function (){ |
| 142 | + this.messages.length = 0 |
| 143 | + }, |
| 144 | + testSpeed: 0, |
| 145 | + } |
| 146 | +}; |
| 147 | + |
| 148 | + |
| 149 | +module.exports = { |
| 150 | + options: options, |
| 151 | +}; |
0 commit comments