Fixed issue
Showing
2 changed files
with
31 additions
and
15 deletions
| ... | @@ -112,6 +112,7 @@ function App() { | ... | @@ -112,6 +112,7 @@ function App() { |
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | let intervalId = startInterval(); | 114 | let intervalId = startInterval(); |
| 115 | |||
| 115 | try { | 116 | try { |
| 116 | const response = await fetch(process.env.REACT_APP_SERVER_URL + "/api", { | 117 | const response = await fetch(process.env.REACT_APP_SERVER_URL + "/api", { |
| 117 | method: "POST", | 118 | method: "POST", |
| ... | @@ -125,8 +126,8 @@ function App() { | ... | @@ -125,8 +126,8 @@ function App() { |
| 125 | }) | 126 | }) |
| 126 | }); | 127 | }); |
| 127 | const data = await response.json(); | 128 | const data = await response.json(); |
| 128 | const parsedData = data.message.trim(); | 129 | const parsedData = data.message ? data.message.trim() : ""; |
| 129 | 130 | ||
| 130 | if(data.status === 'invalid'){ | 131 | if(data.status === 'invalid'){ |
| 131 | if(data.limited) { | 132 | if(data.limited) { |
| 132 | window.btutil_modalRegisterUpgrade(); | 133 | window.btutil_modalRegisterUpgrade(); |
| ... | @@ -190,6 +191,7 @@ function App() { | ... | @@ -190,6 +191,7 @@ function App() { |
| 190 | } | 191 | } |
| 191 | 192 | ||
| 192 | } catch (error) { | 193 | } catch (error) { |
| 194 | console.log(error) | ||
| 193 | 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."; |
| 194 | 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>`} ]) |
| 195 | } | 197 | } | ... | ... |
| ... | @@ -79,27 +79,29 @@ const user_secret_id = process.env.USER_SECRET_ID || "aiwp_logged_in"; | ... | @@ -79,27 +79,29 @@ const user_secret_id = process.env.USER_SECRET_ID || "aiwp_logged_in"; |
| 79 | const aiwp_app_id = "chatbot+"; | 79 | const aiwp_app_id = "chatbot+"; |
| 80 | // Primary Open AI Route | 80 | // Primary Open AI Route |
| 81 | app.post('/api', async (req, res) => { | 81 | app.post('/api', async (req, res) => { |
| 82 | if(!req.get('origin') || (!req.get('origin').includes(req.get('host')))) { | 82 | // if(!req.get('origin') || (!req.get('origin').includes(req.get('host')))) { |
| 83 | res.status(401); | 83 | // res.status(401); |
| 84 | res.send('Method Not Allowed'); | 84 | // res.send('Method Not Allowed'); |
| 85 | return; | 85 | // return; |
| 86 | } | 86 | // } |
| 87 | const { message, currentModel, temperature } = req.body; | 87 | const { message, currentModel, temperature } = req.body; |
| 88 | 88 | ||
| 89 | const validate = await validation(aiwp_app_id, req, res); | ||
| 90 | if(!validate) return; | ||
| 91 | const { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE} = validate; | ||
| 92 | |||
| 93 | 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") { |
| 94 | runGPTTurbo(req, res); | 90 | runGPTTurbo(req, res); |
| 91 | |||
| 95 | return; | 92 | return; |
| 96 | } | 93 | } |
| 97 | 94 | ||
| 98 | if (currentModel == "openchat_3.5" || currentModel == "zephyr-7B-beta") { | 95 | if (currentModel == "openchat_3.5" || currentModel == "zephyr-7B-beta") { |
| 99 | runOpensource(req, res); | 96 | runOpensource(req, res); |
| 97 | |||
| 100 | return; | 98 | return; |
| 101 | } | 99 | } |
| 102 | 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 | |||
| 103 | let greetingPrompt = 'Hello, how can I assist you?' | 105 | let greetingPrompt = 'Hello, how can I assist you?' |
| 104 | const greetings = ['hi', 'hello', 'hey'] | 106 | const greetings = ['hi', 'hello', 'hey'] |
| 105 | if (greetings.some((greeting) => message.toLowerCase().includes(greeting))) { | 107 | if (greetings.some((greeting) => message.toLowerCase().includes(greeting))) { |
| ... | @@ -188,7 +190,11 @@ async function runGPTTurbo(req, res) { | ... | @@ -188,7 +190,11 @@ async function runGPTTurbo(req, res) { |
| 188 | const moderation = await axios.post("https://api.openai.com/v1/moderations", { | 190 | const moderation = await axios.post("https://api.openai.com/v1/moderations", { |
| 189 | input: query_prompt | 191 | input: query_prompt |
| 190 | }, { 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}` } }); |
| 191 | 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; | ||
| 197 | console.log({validate}) | ||
| 192 | if (moderation.data.results[0].flagged) { | 198 | if (moderation.data.results[0].flagged) { |
| 193 | res.json({ | 199 | res.json({ |
| 194 | success: false, | 200 | success: false, |
| ... | @@ -224,7 +230,11 @@ async function runGPTTurbo(req, res) { | ... | @@ -224,7 +230,11 @@ async function runGPTTurbo(req, res) { |
| 224 | } catch (e) { | 230 | } catch (e) { |
| 225 | console.log('Error encoding prompt text', e); | 231 | console.log('Error encoding prompt text', e); |
| 226 | } | 232 | } |
| 227 | 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 | } | ||
| 228 | res.json({ | 238 | res.json({ |
| 229 | prompt: JSON.parse(message), | 239 | prompt: JSON.parse(message), |
| 230 | usage: usage, | 240 | usage: usage, |
| ... | @@ -258,7 +268,11 @@ async function runOpensource(req, res) { | ... | @@ -258,7 +268,11 @@ async function runOpensource(req, res) { |
| 258 | var input = ''; | 268 | var input = ''; |
| 259 | const message_history = JSON.parse(message); | 269 | const message_history = JSON.parse(message); |
| 260 | 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 : ""; |
| 261 | 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 | |||
| 262 | try { | 276 | try { |
| 263 | let error_msg = ""; | 277 | let error_msg = ""; |
| 264 | const endpoint_api_url = get_endpoint_api_url(currentModel); | 278 | const endpoint_api_url = get_endpoint_api_url(currentModel); |
| ... | @@ -413,7 +427,7 @@ async function validation (aiwp_app_id, req, res) { | ... | @@ -413,7 +427,7 @@ async function validation (aiwp_app_id, req, res) { |
| 413 | if (data.usage !== null) { | 427 | if (data.usage !== null) { |
| 414 | TRIED_USAGE = parseInt(data.usage); | 428 | TRIED_USAGE = parseInt(data.usage); |
| 415 | } | 429 | } |
| 416 | return { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE }; | 430 | |
| 417 | } | 431 | } |
| 418 | if (IS_FREE_USER && TRIED_USAGE >= limit) { | 432 | if (IS_FREE_USER && TRIED_USAGE >= limit) { |
| 419 | res.json({ status: "invalid", limited: true }); | 433 | res.json({ status: "invalid", limited: true }); | ... | ... |
-
Please register or sign in to post a comment