"use strict" const crypto = require('crypto'); const { v4: uuidv4 } = require('uuid'); const UserAgent = require('fake-useragent'); const {HttpsProxyAgent} = require('https-proxy-agent'); class ChatCompletion { static async create(messages) { const url = "https://chat.getgpt.world/api/chat/stream"; // const url = "https://api.ipify.org"; const headers = { "Content-Type": "application/json", "Referer": "https://chat.getgpt.world/", 'User-Agent': UserAgent(), "agent": new HttpsProxyAgent("https://qmvnozfp:jgr74lg0t6hn@185.199.229.156:5074") }; const data = JSON.stringify({ "messages": messages, "frequency_penalty": 0, "max_tokens": 5000, "model": "gpt-3.5-turbo", "presence_penalty": 0, "temperature": 1, "top_p": 1, // "stream": true, "uuid": uuidv4() }); const signature = ChatCompletion.encrypt(data); const response = await fetch(url, { method: 'post', headers: headers, body: JSON.stringify({ "signature": signature }), }); const reader = response.body.getReader(); let content = ''; while (true) { const { done, value } = await reader.read(); if (done) break; const chunk = new TextDecoder().decode(value); content += chunk; } const chunks = content.split('data: '); let final = ""; for (let i = 1; i < chunks.length; i++) { const data = chunks[i]; if (!data || data.includes("[DONE]")) { return final; } const dataJson = JSON.parse(data); const content = dataJson.choices[0].delta.content; if (content) { final += content } } } static randomToken(e) { const token = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; const n = token.length; let result = ""; for (let i = 0; i < e; i++) { const randomIndex = Math.floor(Math.random() * n); result += token.charAt(randomIndex); } return result; } static encrypt(e) { const t = ChatCompletion.randomToken(16); const n = ChatCompletion.randomToken(16); const r = Buffer.from(e, 'utf-8'); const cipher = crypto.createCipheriv('aes-128-cbc', t, n); let ciphertext = cipher.update(r, 'utf-8', 'hex'); ciphertext += cipher.final('hex'); return ciphertext + t + n; } static __padData(data) { const block_size = 16; const padding_size = block_size - (data.length % block_size); const padding = Buffer.alloc(padding_size, padding_size); return Buffer.concat([data, padding]); } } module.exports = ChatCompletion