Merge branch '30852_chatbot_chatgpt' into 'master'
30852 chatbot chatgpt See merge request !100
Showing
4 changed files
with
160 additions
and
18 deletions
| ... | @@ -71,14 +71,6 @@ function App() { | ... | @@ -71,14 +71,6 @@ function App() { |
| 71 | TPLogicRun(); | 71 | TPLogicRun(); |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | // eslint-disable-next-line | ||
| 75 | btutil_getChatUsage(); | ||
| 76 | // eslint-disable-next-line | ||
| 77 | let maxTokens = btutilCommon_getCookie("mucnxwlyxt"); | ||
| 78 | if (maxTokens==='1'){ | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | const userInput = ['what', 'why', 'when', 'where' , 'which', 'did', 'do', 'how', 'can', 'are', 'who']; | 74 | const userInput = ['what', 'why', 'when', 'where' , 'which', 'did', 'do', 'how', 'can', 'are', 'who']; |
| 83 | const userInputRegex = new RegExp(`\\b(${userInput.join('|')})\\b`, 'gi'); | 75 | const userInputRegex = new RegExp(`\\b(${userInput.join('|')})\\b`, 'gi'); |
| 84 | const inputMatches = chatInput.match(userInputRegex); | 76 | const inputMatches = chatInput.match(userInputRegex); |
| ... | @@ -120,6 +112,7 @@ function App() { | ... | @@ -120,6 +112,7 @@ function App() { |
| 120 | } | 112 | } |
| 121 | 113 | ||
| 122 | let intervalId = startInterval(); | 114 | let intervalId = startInterval(); |
| 115 | |||
| 123 | try { | 116 | try { |
| 124 | const response = await fetch(process.env.REACT_APP_SERVER_URL + "/api", { | 117 | const response = await fetch(process.env.REACT_APP_SERVER_URL + "/api", { |
| 125 | method: "POST", | 118 | method: "POST", |
| ... | @@ -133,7 +126,20 @@ function App() { | ... | @@ -133,7 +126,20 @@ function App() { |
| 133 | }) | 126 | }) |
| 134 | }); | 127 | }); |
| 135 | const data = await response.json(); | 128 | const data = await response.json(); |
| 136 | const parsedData = data.message.trim(); | 129 | const parsedData = data.message ? data.message.trim() : ""; |
| 130 | |||
| 131 | if(data.status === 'invalid'){ | ||
| 132 | if(data.limited) { | ||
| 133 | window.btutil_modalRegisterUpgrade(); | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | if(data && data.status === 'max-tokens') { | ||
| 137 | window.btutil_maxUsage(); | ||
| 138 | return; | ||
| 139 | } | ||
| 140 | window.btutil_modalRegisterUpgrade(true); | ||
| 141 | return; | ||
| 142 | } | ||
| 137 | // "gpt-3.5-turbo" | 143 | // "gpt-3.5-turbo" |
| 138 | let chatLogTurboNew = chatLogTurbo; | 144 | let chatLogTurboNew = chatLogTurbo; |
| 139 | let chatLogOpenSourceNew = chatLogOpenSource; | 145 | let chatLogOpenSourceNew = chatLogOpenSource; |
| ... | @@ -144,9 +150,7 @@ function App() { | ... | @@ -144,9 +150,7 @@ function App() { |
| 144 | }); | 150 | }); |
| 145 | userModifiedInput = ""; | 151 | userModifiedInput = ""; |
| 146 | } | 152 | } |
| 147 | if(data.usage) { | 153 | |
| 148 | window.btutil_setChatUsage('chatbot+', data.usage.prompt_tokens, data.usage.total_tokens); | ||
| 149 | } | ||
| 150 | chatLogTurboNew.push({ role: "user", content: userModifiedInput }); | 154 | chatLogTurboNew.push({ role: "user", content: userModifiedInput }); |
| 151 | chatLogTurboNew.push({ role: "assistant", content: parsedData }); | 155 | chatLogTurboNew.push({ role: "assistant", content: parsedData }); |
| 152 | 156 | ||
| ... | @@ -187,6 +191,7 @@ function App() { | ... | @@ -187,6 +191,7 @@ function App() { |
| 187 | } | 191 | } |
| 188 | 192 | ||
| 189 | } catch (error) { | 193 | } catch (error) { |
| 194 | console.log(error) | ||
| 190 | const errorMsg = "We apologize for any inconvenience caused due to the delay in the response time. Please try again."; | 195 | const errorMsg = "We apologize for any inconvenience caused due to the delay in the response time. Please try again."; |
| 191 | setChatLog([...chatLogNew, { user: "gpt", message: `<div class="errormsg"><span>i</span><div class="msg">${errorMsg}</div></div>`} ]) | 196 | setChatLog([...chatLogNew, { user: "gpt", message: `<div class="errormsg"><span>i</span><div class="msg">${errorMsg}</div></div>`} ]) |
| 192 | } | 197 | } | ... | ... |
| 1 | const { Configuration, OpenAIApi } = require("openai"); | 1 | const { Configuration, OpenAIApi } = require("openai"); |
| 2 | const express = require('express') | 2 | const express = require('express') |
| 3 | const bodyParser = require('body-parser') | 3 | const bodyParser = require('body-parser') |
| 4 | const cookieParser = require("cookie-parser") | ||
| 4 | const cors = require('cors') | 5 | const cors = require('cors') |
| 5 | require('dotenv').config() | 6 | require('dotenv').config() |
| 6 | const rateLimit = require('express-rate-limit') | 7 | const rateLimit = require('express-rate-limit') |
| 8 | const fetch = require('node-fetch'); | ||
| 7 | const anchorme = require("anchorme").default; | 9 | const anchorme = require("anchorme").default; |
| 8 | const axios = require('axios'); | 10 | const axios = require('axios'); |
| 9 | const { encodingForModel } = require('js-tiktoken'); | 11 | const { encodingForModel } = require('js-tiktoken'); |
| ... | @@ -68,10 +70,13 @@ app.use(bodyParser.json()) | ... | @@ -68,10 +70,13 @@ app.use(bodyParser.json()) |
| 68 | app.use(cors()) | 70 | app.use(cors()) |
| 69 | app.use(require('morgan')('dev')) | 71 | app.use(require('morgan')('dev')) |
| 70 | app.use(rateLimiter) | 72 | app.use(rateLimiter) |
| 73 | app.use(cookieParser()); | ||
| 71 | 74 | ||
| 72 | const max_tokens = process.env.MAX_TOKENS_chatbot_plus ? parseInt(process.env.MAX_TOKENS_chatbot_plus) : 512; | 75 | const max_tokens = process.env.MAX_TOKENS_chatbot_plus ? parseInt(process.env.MAX_TOKENS_chatbot_plus) : 512; |
| 73 | // Routing | 76 | // Routing |
| 74 | 77 | const hostapi = process.env.REACT_APP_HOST_API || "https://api.ai-pro.org"; | |
| 78 | const user_secret_id = process.env.USER_SECRET_ID || "aiwp_logged_in"; | ||
| 79 | const aiwp_app_id = "chatbot+"; | ||
| 75 | // Primary Open AI Route | 80 | // Primary Open AI Route |
| 76 | app.post('/api', async (req, res) => { | 81 | app.post('/api', async (req, res) => { |
| 77 | if(!req.get('origin') || (!req.get('origin').includes(req.get('host')))) { | 82 | if(!req.get('origin') || (!req.get('origin').includes(req.get('host')))) { |
| ... | @@ -83,14 +88,20 @@ app.post('/api', async (req, res) => { | ... | @@ -83,14 +88,20 @@ app.post('/api', async (req, res) => { |
| 83 | 88 | ||
| 84 | if (currentModel == "gpt-3.5-turbo" || currentModel == "gpt-3.5-turbo-0301") { | 89 | if (currentModel == "gpt-3.5-turbo" || currentModel == "gpt-3.5-turbo-0301") { |
| 85 | runGPTTurbo(req, res); | 90 | runGPTTurbo(req, res); |
| 91 | |||
| 86 | return; | 92 | return; |
| 87 | } | 93 | } |
| 88 | 94 | ||
| 89 | if (currentModel == "openchat_3.5" || currentModel == "zephyr-7B-beta") { | 95 | if (currentModel == "openchat_3.5" || currentModel == "zephyr-7B-beta") { |
| 90 | runOpensource(req, res); | 96 | runOpensource(req, res); |
| 97 | |||
| 91 | return; | 98 | return; |
| 92 | } | 99 | } |
| 93 | 100 | ||
| 101 | const validate = await validation(aiwp_app_id, req, res); | ||
| 102 | if(!validate) return; | ||
| 103 | const { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE} = validate; | ||
| 104 | |||
| 94 | let greetingPrompt = 'Hello, how can I assist you?' | 105 | let greetingPrompt = 'Hello, how can I assist you?' |
| 95 | const greetings = ['hi', 'hello', 'hey'] | 106 | const greetings = ['hi', 'hello', 'hey'] |
| 96 | if (greetings.some((greeting) => message.toLowerCase().includes(greeting))) { | 107 | if (greetings.some((greeting) => message.toLowerCase().includes(greeting))) { |
| ... | @@ -136,10 +147,15 @@ app.post('/api', async (req, res) => { | ... | @@ -136,10 +147,15 @@ app.post('/api', async (req, res) => { |
| 136 | usage.prompt_tokens = (enc.encode(query_prompt)).length; | 147 | usage.prompt_tokens = (enc.encode(query_prompt)).length; |
| 137 | usage.completion_tokens = (enc.encode(input)).length; | 148 | usage.completion_tokens = (enc.encode(input)).length; |
| 138 | usage.total_tokens = usage.prompt_tokens + usage.completion_tokens; | 149 | usage.total_tokens = usage.prompt_tokens + usage.completion_tokens; |
| 150 | |||
| 139 | } catch (e) { | 151 | } catch (e) { |
| 140 | console.log('Error encoding prompt text', e); | 152 | console.log('Error encoding prompt text', e); |
| 141 | } | 153 | } |
| 142 | 154 | if(IS_FREE_USER) { | |
| 155 | await setUsage({ | ||
| 156 | aiwp_logged_in, app: 'chatbot+', prompt_token: usage.prompt_tokens, total_token: usage.total_tokens, aiwp_app_id, usage_tries: TRIED_USAGE | ||
| 157 | }); | ||
| 158 | } | ||
| 143 | res.json({ | 159 | res.json({ |
| 144 | usage: usage, | 160 | usage: usage, |
| 145 | message: anchorme({ | 161 | message: anchorme({ |
| ... | @@ -174,6 +190,10 @@ async function runGPTTurbo(req, res) { | ... | @@ -174,6 +190,10 @@ async function runGPTTurbo(req, res) { |
| 174 | const moderation = await axios.post("https://api.openai.com/v1/moderations", { | 190 | const moderation = await axios.post("https://api.openai.com/v1/moderations", { |
| 175 | input: query_prompt | 191 | input: query_prompt |
| 176 | }, { headers: { 'content-type': 'application/json', 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` } }); | 192 | }, { headers: { 'content-type': 'application/json', 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` } }); |
| 193 | |||
| 194 | const validate = await validation(aiwp_app_id, req, res); | ||
| 195 | if(!validate) return; | ||
| 196 | const { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE} = validate; | ||
| 177 | 197 | ||
| 178 | if (moderation.data.results[0].flagged) { | 198 | if (moderation.data.results[0].flagged) { |
| 179 | res.json({ | 199 | res.json({ |
| ... | @@ -210,7 +230,11 @@ async function runGPTTurbo(req, res) { | ... | @@ -210,7 +230,11 @@ async function runGPTTurbo(req, res) { |
| 210 | } catch (e) { | 230 | } catch (e) { |
| 211 | console.log('Error encoding prompt text', e); | 231 | console.log('Error encoding prompt text', e); |
| 212 | } | 232 | } |
| 213 | 233 | if(IS_FREE_USER) { | |
| 234 | await setUsage({ | ||
| 235 | aiwp_logged_in, app: 'chatbot+', prompt_token: usage.prompt_tokens, total_token: usage.total_tokens, aiwp_app_id, usage_tries: TRIED_USAGE | ||
| 236 | }); | ||
| 237 | } | ||
| 214 | res.json({ | 238 | res.json({ |
| 215 | prompt: JSON.parse(message), | 239 | prompt: JSON.parse(message), |
| 216 | usage: usage, | 240 | usage: usage, |
| ... | @@ -244,7 +268,11 @@ async function runOpensource(req, res) { | ... | @@ -244,7 +268,11 @@ async function runOpensource(req, res) { |
| 244 | var input = ''; | 268 | var input = ''; |
| 245 | const message_history = JSON.parse(message); | 269 | const message_history = JSON.parse(message); |
| 246 | const query_prompt = message_history.length ? message_history[message_history.length - 1].content : ""; | 270 | const query_prompt = message_history.length ? message_history[message_history.length - 1].content : ""; |
| 247 | 271 | ||
| 272 | const validate = await validation(aiwp_app_id, req, res); | ||
| 273 | if(!validate) return; | ||
| 274 | const { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE} = validate; | ||
| 275 | |||
| 248 | try { | 276 | try { |
| 249 | let error_msg = ""; | 277 | let error_msg = ""; |
| 250 | const endpoint_api_url = get_endpoint_api_url(currentModel); | 278 | const endpoint_api_url = get_endpoint_api_url(currentModel); |
| ... | @@ -303,6 +331,114 @@ async function runOpensource(req, res) { | ... | @@ -303,6 +331,114 @@ async function runOpensource(req, res) { |
| 303 | } | 331 | } |
| 304 | } | 332 | } |
| 305 | 333 | ||
| 334 | async function authenticate(params) { | ||
| 335 | let data = await fetch(`${hostapi}/e/authenticate/v2`, { | ||
| 336 | method: "POST", | ||
| 337 | headers: { | ||
| 338 | "Content-Type": "application/json" | ||
| 339 | }, | ||
| 340 | body: JSON.stringify(params), | ||
| 341 | referrer: "https://chatgpt.ai-pro.org" | ||
| 342 | }); | ||
| 343 | return await data.json(); | ||
| 344 | } | ||
| 345 | |||
| 346 | async function getLimitedUsage(params) { | ||
| 347 | let data = await fetch(`${hostapi}/e/get-usage`, { | ||
| 348 | method: "POST", | ||
| 349 | headers: { | ||
| 350 | "Content-Type": "application/json" | ||
| 351 | }, | ||
| 352 | body: JSON.stringify(params), | ||
| 353 | referrer: "https://chatgpt.ai-pro.org" | ||
| 354 | }); | ||
| 355 | return await data.json(); | ||
| 356 | } | ||
| 357 | async function getUsage(params) { | ||
| 358 | let data = await fetch(`${hostapi}/e/get-chat-usage`, { | ||
| 359 | method: "POST", | ||
| 360 | headers: { | ||
| 361 | "Content-Type": "application/json" | ||
| 362 | }, | ||
| 363 | body: JSON.stringify(params), | ||
| 364 | referrer: "https://chatgpt.ai-pro.org" | ||
| 365 | }); | ||
| 366 | return await data.json(); | ||
| 367 | } | ||
| 368 | async function setUsage(params) { | ||
| 369 | fetch(`${hostapi}/e/set-usage`, { | ||
| 370 | method: "POST", | ||
| 371 | headers: { | ||
| 372 | "Content-Type": "application/json" | ||
| 373 | }, | ||
| 374 | body: JSON.stringify(params), | ||
| 375 | referrer: "https://chatgpt.ai-pro.org" | ||
| 376 | }); | ||
| 377 | fetch(`${hostapi}/e/set-chat-usage`, { | ||
| 378 | method: "POST", | ||
| 379 | headers: { | ||
| 380 | "Content-Type": "application/json" | ||
| 381 | }, | ||
| 382 | body: JSON.stringify(params), | ||
| 383 | referrer: "https://chatgpt.ai-pro.org" | ||
| 384 | }); | ||
| 385 | } | ||
| 386 | |||
| 387 | async function validation (aiwp_app_id, req, res) { | ||
| 388 | const aiwp_logged_in = req.cookies[user_secret_id] ? decodeURIComponent(req.cookies[user_secret_id]) : ""; | ||
| 389 | const limit = req.cookies["WcvYPABR"] ? parseInt(req.cookies["WcvYPABR"].replace(/\D/g, '')) : 3; | ||
| 390 | let IS_FREE_USER = false; | ||
| 391 | let TRIED_USAGE = 0; | ||
| 392 | |||
| 393 | if (aiwp_logged_in) { | ||
| 394 | let auth = await authenticate({ aiwp_logged_in, user_event_data: {}, user_event: 'endpoint' }); | ||
| 395 | if (!auth.success) { | ||
| 396 | IS_FREE_USER = true; | ||
| 397 | if (auth.is_restrict) { | ||
| 398 | res.json({ status: "invalid", restrict: true, redirect: auth.redirect }); | ||
| 399 | res.end(); | ||
| 400 | return false; | ||
| 401 | } else if (typeof auth.has_pro_access === "undefined" && !auth.has_pro_access) { | ||
| 402 | res.json({ status: "invalid", restrict: true }); | ||
| 403 | res.end(); | ||
| 404 | return false; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | if (!auth.subscription_type || (auth.auth_version === 'v2' && auth.subscription_type.toLowerCase() === 'basic')) { | ||
| 408 | res.json({ status: "invalid" }); | ||
| 409 | res.status(200); | ||
| 410 | return false; | ||
| 411 | } | ||
| 412 | let data = await getUsage({ | ||
| 413 | aiwp_logged_in, app: 'chatbot+' | ||
| 414 | }); | ||
| 415 | |||
| 416 | if (!(data.success === 1 && data.status === 'valid')) { | ||
| 417 | res.json({ status: "invalid", data }); | ||
| 418 | res.status(200); | ||
| 419 | return false; | ||
| 420 | } | ||
| 421 | } else { | ||
| 422 | IS_FREE_USER = true; | ||
| 423 | let data = await getLimitedUsage({ | ||
| 424 | aiwp_app_id | ||
| 425 | }); | ||
| 426 | |||
| 427 | if (data.usage !== null) { | ||
| 428 | TRIED_USAGE = parseInt(data.usage); | ||
| 429 | } | ||
| 430 | |||
| 431 | } | ||
| 432 | if (IS_FREE_USER && TRIED_USAGE >= limit) { | ||
| 433 | res.json({ status: "invalid", limited: true }); | ||
| 434 | res.end(); | ||
| 435 | return false; | ||
| 436 | } | ||
| 437 | if (IS_FREE_USER) TRIED_USAGE++; | ||
| 438 | |||
| 439 | return { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE }; | ||
| 440 | }; | ||
| 441 | |||
| 306 | 442 | ||
| 307 | 443 | ||
| 308 | // Get Models Route | 444 | // Get Models Route | ... | ... |
This diff is collapsed.
Click to expand it.
| ... | @@ -10,17 +10,18 @@ | ... | @@ -10,17 +10,18 @@ |
| 10 | "author": "", | 10 | "author": "", |
| 11 | "license": "ISC", | 11 | "license": "ISC", |
| 12 | "dependencies": { | 12 | "dependencies": { |
| 13 | "js-tiktoken": "1.0.7", | ||
| 14 | "anchorme": "^2.1.2", | 13 | "anchorme": "^2.1.2", |
| 15 | "axios": "^1.5.1", | 14 | "axios": "^1.5.1", |
| 16 | "body-parser": "^1.20.1", | 15 | "body-parser": "^1.20.1", |
| 17 | "cookie": "0.5.0", | 16 | "cookie": "0.5.0", |
| 18 | "cookie-parser": "1.4.6", | 17 | "cookie-parser": "^1.4.6", |
| 19 | "cors": "^2.8.5", | 18 | "cors": "^2.8.5", |
| 20 | "dotenv": "^16.0.3", | 19 | "dotenv": "^16.0.3", |
| 21 | "express": "^4.18.2", | 20 | "express": "^4.18.2", |
| 22 | "express-rate-limit": "^6.7.0", | 21 | "express-rate-limit": "^6.7.0", |
| 22 | "js-tiktoken": "1.0.7", | ||
| 23 | "morgan": "^1.10.0", | 23 | "morgan": "^1.10.0", |
| 24 | "node-fetch": "^2.7.0", | ||
| 24 | "openai": "^3.2.0" | 25 | "openai": "^3.2.0" |
| 25 | } | 26 | } |
| 26 | } | 27 | } | ... | ... |
-
Please register or sign in to post a comment