From 391d4b8c796f9c7e1e09ffc7e7687b154edf6966 Mon Sep 17 00:00:00 2001 From: adamlui Date: Fri, 31 May 2024 07:40:51 +0000 Subject: [PATCH] =?UTF-8?q?Added=20`demo-yj7h.onrender.com`=20API=20endpoi?= =?UTF-8?q?nt/routines=20+=20refactored=20`createHeaders()`=20for=20readab?= =?UTF-8?q?ility=20=E2=86=9E=20[auto-sync=20from=20`adamlui/chatgpt-apps`]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- greasemonkey/duckduckgpt.user.js | 48 +++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/greasemonkey/duckduckgpt.user.js b/greasemonkey/duckduckgpt.user.js index ead1c8c9527..ba8255008aa 100644 --- a/greasemonkey/duckduckgpt.user.js +++ b/greasemonkey/duckduckgpt.user.js @@ -152,7 +152,7 @@ // @description:zu Faka amaphawu ase-ChatGPT kuvaliwe i-DuckDuckGo Search (okwesikhashana ngu-GPT-4o!) // @author KudoAI // @namespace https://kudoai.com -// @version 2024.5.30.5 +// @version 2024.5.31 // @license MIT // @icon https://media.ddgpt.com/images/icons/duckduckgpt/icon48.png?af89302 // @icon64 https://media.ddgpt.com/images/icons/duckduckgpt/icon64.png?af89302 @@ -175,6 +175,7 @@ // @connect gptforlove.com // @connect greasyfork.org // @connect jsdelivr.net +// @connect onrender.com // @connect openai.com // @connect sogou.com // @require https://cdn.jsdelivr.net/npm/@kudoai/chatgpt.js@2.9.3/dist/chatgpt.min.js#sha256-EDN+mCc+0Y4YVzJEoNikd4/rAIaJDLAdb+erWvupXTM= @@ -702,7 +703,7 @@ // Define ANSWER functions - let endpoint, accessKey, model + let endpoint, endpointMethod, accessKey, model async function pickAPI() { if (config.proxyAPIenabled) { // randomize proxy API const untriedEndpoints = proxyEndpoints.filter( @@ -710,23 +711,27 @@ const entry = untriedEndpoints[Math.floor(chatgpt.randomFloat() * untriedEndpoints.length)] if (!entry) // no more proxy endpoints left untried appAlert('suggestOpenAI') - else endpoint = entry[0] + else { endpoint = entry[0], endpointMethod = entry[1].method } } else { // use OpenAI API endpoint = openAIendpoints.chat const timeoutPromise = new Promise((resolve, reject) => setTimeout(() => reject(new Error('Timeout occurred')), 3000)) accessKey = await Promise.race([getOpenAItoken(), timeoutPromise]) if (!accessKey) { appAlert('login') ; return } - model = 'gpt-3.5-turbo' + endpointMethod = 'POST', model = 'gpt-3.5-turbo' } appInfo('Endpoint used: ' + endpoint) } function createHeaders(api) { let headers = { 'Content-Type': 'application/json', 'X-Forwarded-For': ipv4.generate({ verbose: false })} - if (api.includes('openai.com')) headers.Authorization = 'Bearer ' + accessKey - else if (api.includes('binjie.fun')) headers.Referer = headers.Origin = 'https://chat18.aichatos.xyz' - else if (api.includes('gptforlove.com')) headers.Referer = headers.Origin = 'https://ai27.gptforlove.com' + if (api.includes('openai.com')) headers.Authorization = 'Bearer ' + accessKey + headers.Referer = headers.Origin = ( // preserve expected traffic src + api.includes('openai.com') ? 'https://chatgpt.com' + : api.includes('binjie.fun') ? 'https://chat18.aichatos.xyz' + : api.includes('gptforlove.com') ? 'https://ai27.gptforlove.com' + : api.includes('onrender.com') ? 'https://e8.frechat.xyz' : '' + ) return headers } @@ -747,7 +752,8 @@ systemMessage: 'You are ChatGPT, the version is GPT-4o, a large language model trained by OpenAI. Follow the user\'s instructions carefully. Respond using markdown.' } if (ids.gptPlus.parentID) payload.options = { parentMessageId: ids.gptPlus.parentID } - } + } else if (api.includes('onrender.com')) + payload = { messages: msgs, model: 'gemma-7b-it' } return JSON.stringify(payload) } @@ -764,7 +770,7 @@ + ' But the key is variety. Do not be repetitive.' + ' You must entice user to want to ask one of your related queries.' GM.xmlHttpRequest({ - method: 'POST', url: endpoint, responseType: 'text', headers: createHeaders(endpoint), + method: endpointMethod, url: endpoint, responseType: 'text', headers: createHeaders(endpoint), data: createPayload(endpoint, [{ role: 'user', content: rqPrompt }]), onload: event => { let str_relatedQueries = '' @@ -785,6 +791,9 @@ let chunks = event.responseText.trim().split('\n') str_relatedQueries = JSON.parse(chunks[chunks.length - 1]).text } catch (err) { appError(err) ; reject(err) } + } else if (endpoint.includes('onrender.com')) { + try { str_relatedQueries = event.responseText } + catch (err) { appError(err) ; reject(err) } } const arr_relatedQueries = (str_relatedQueries.match(/\d+\.\s*(.*?)(?=\n|$)/g) || []) .slice(0, 5) // limit to 1st 5 @@ -826,7 +835,7 @@ // Get/show answer from ChatGPT await pickAPI() GM.xmlHttpRequest({ - method: 'POST', url: endpoint, headers: createHeaders(endpoint), + method: endpointMethod, url: endpoint, headers: createHeaders(endpoint), responseType: 'text', data: createPayload(endpoint, convo), onload: onLoad(), onerror: err => { appError(err) @@ -946,6 +955,16 @@ proxyRetryOrAlert() } } else { appInfo('Response: ' + event.responseText) ; proxyRetryOrAlert() } + } else if (endpoint.includes('onrender.com')) { + if (event.responseText) { + try { + appShow(event.responseText, footerContent) ; getShowReply.triedEndpoints = [] ; getShowReply.attemptCnt = 0 + } catch (err) { // use different endpoint or suggest OpenAI + appInfo('Response: ' + event.responseText) + appError(appAlerts.parseFailed + ': ' + err) + proxyRetryOrAlert() + } + } else { appInfo('Response: ' + event.responseText) ; proxyRetryOrAlert() } } }}} @@ -1288,10 +1307,13 @@ const openAIendpoints = { auth: 'https://auth0.openai.com', session: 'https://chatgpt.com/api/auth/session', - chat: 'https://api.openai.com/v1/chat/completions' } + chat: 'https://api.openai.com/v1/chat/completions' + } const proxyEndpoints = [ - [ 'https://api.binjie.fun/api/generateStream' ], - [ 'https://api11.gptforlove.com/chat-process' ]] + [ 'https://api.binjie.fun/api/generateStream', { method: 'POST', stream: true }], + [ 'https://api11.gptforlove.com/chat-process', { method: 'POST', stream: true }], + [ 'https://demo-yj7h.onrender.com/single/chat_messages', { method: 'PUT', stream: true }] + ] // Init ALERTS const appAlerts = {