fix
Showing
5 changed files
with
98 additions
and
28 deletions
| ... | @@ -266,3 +266,30 @@ ul.suggested-options { | ... | @@ -266,3 +266,30 @@ ul.suggested-options { |
| 266 | padding: 10px 10px 0 10px; | 266 | padding: 10px 10px 0 10px; |
| 267 | } | 267 | } |
| 268 | } | 268 | } |
| 269 | |||
| 270 | .message .error_msg { | ||
| 271 | background: rgb(18 0 255 / 20%); | ||
| 272 | padding: 10px; | ||
| 273 | border-radius: 5px; | ||
| 274 | display: flex; | ||
| 275 | } | ||
| 276 | |||
| 277 | .message .error_msg span { | ||
| 278 | background: #0023ff; | ||
| 279 | width: 25px; | ||
| 280 | height: 25px; | ||
| 281 | display: inline-block; | ||
| 282 | text-align: center; | ||
| 283 | padding-top: 1px; | ||
| 284 | border-radius: 50px; | ||
| 285 | font-family: auto; | ||
| 286 | font-weight: 600; | ||
| 287 | vertical-align: top; | ||
| 288 | } | ||
| 289 | |||
| 290 | .message .error_msg .msg { | ||
| 291 | display: inline-block; | ||
| 292 | margin: 0 10px; | ||
| 293 | font-style: italic; | ||
| 294 | width: 80%; | ||
| 295 | } | ... | ... |
| ... | @@ -47,7 +47,7 @@ function App() { | ... | @@ -47,7 +47,7 @@ function App() { |
| 47 | const userInputRegex = new RegExp(`\\b(${userInput.join('|')})\\b`, 'gi'); | 47 | const userInputRegex = new RegExp(`\\b(${userInput.join('|')})\\b`, 'gi'); |
| 48 | const inputMatches = chatInput.match(userInputRegex); | 48 | const inputMatches = chatInput.match(userInputRegex); |
| 49 | 49 | ||
| 50 | const userPunctuation = ['\.', '?', '!', ':', ';', ',']; | 50 | const userPunctuation = ['.', '?', '!', ':', ';', ',']; |
| 51 | const userPunctuationRegex = new RegExp(`[${userPunctuation.join('')}]$`); | 51 | const userPunctuationRegex = new RegExp(`[${userPunctuation.join('')}]$`); |
| 52 | const punctuationMatches = chatInput.match(userPunctuationRegex); | 52 | const punctuationMatches = chatInput.match(userPunctuationRegex); |
| 53 | 53 | ||
| ... | @@ -71,34 +71,36 @@ function App() { | ... | @@ -71,34 +71,36 @@ function App() { |
| 71 | // fetch response to the api combining the chat log array of messages and seinding it as a message to localhost:3000 as a post | 71 | // fetch response to the api combining the chat log array of messages and seinding it as a message to localhost:3000 as a post |
| 72 | const messages = chatLogNew.map((message) => message.message).join("\n") | 72 | const messages = chatLogNew.map((message) => message.message).join("\n") |
| 73 | 73 | ||
| 74 | const response = await fetch(process.env.REACT_APP_SERVER_URL + "/api", { | 74 | try { |
| 75 | method: "POST", | 75 | const response = await fetch(process.env.REACT_APP_SERVER_URL + "/api", { |
| 76 | headers: { | 76 | method: "POST", |
| 77 | "Content-Type": "application/json" | 77 | headers: { |
| 78 | }, | 78 | "Content-Type": "application/json" |
| 79 | body: JSON.stringify({ | 79 | }, |
| 80 | message: messages, | 80 | body: JSON.stringify({ |
| 81 | currentModel, | 81 | message: messages, |
| 82 | }) | 82 | currentModel, |
| 83 | }) | ||
| 83 | }); | 84 | }); |
| 84 | const data = await response.json(); | 85 | const data = await response.json(); |
| 85 | const parsedData = data.message.trim(); | 86 | const parsedData = data.message.trim(); |
| 86 | const programmingKeywords = ['code', 'application', 'controller', 'rails' , 'PHP', 'java', 'javascript', 'script', 'console', 'python', 'programming', 'table']; | 87 | const programmingKeywords = ['code', 'application', 'controller', 'rails' , 'PHP', 'java', 'javascript', 'script', 'console', 'python', 'programming', 'table']; |
| 87 | 88 | ||
| 88 | const regex = new RegExp(`\\b(${programmingKeywords.join('|')})\\b`, 'gi'); | 89 | const regex = new RegExp(`\\b(${programmingKeywords.join('|')})\\b`, 'gi'); |
| 89 | // console.log(regex) | 90 | const matches = parsedData.match(regex); |
| 90 | const matches = parsedData.match(regex); | 91 | if (!matches) { |
| 91 | // console.log(matches); | 92 | var replaceTags = (parsedData.replace(/(?:\r\n|\r|\n)/g, '<br>').replace(/\./g, '. ')) |
| 92 | if (!matches) { | 93 | } else { |
| 93 | var replaceTags = (parsedData.replace(/(?:\r\n|\r|\n)/g, '<br>').replace(/\./g, '. ')) | 94 | replaceTags = (parsedData.replace(':',':<code>').replace('<?','<?').replace('?>','?>').replace(/\n/g, '<br>')) |
| 94 | // console.log("not programming!") | 95 | } |
| 95 | } else { | 96 | setChatLog([...chatLogNew, { user: "gpt", message: `${replaceTags}`} ]) |
| 96 | replaceTags = (parsedData.replace(':',':<code>').replace('<?','<?').replace('?>','?>').replace(/\n/g, '<br>')) | 97 | |
| 97 | // console.log("programming!") | 98 | var scrollToTheBottomChatLog = document.getElementsByClassName("chat-log")[0]; |
| 99 | scrollToTheBottomChatLog.scrollTop = scrollToTheBottomChatLog.scrollHeight; | ||
| 100 | } catch (error) { | ||
| 101 | const errorMsg = "We apologize for any inconvenience caused due to the delay in the response time. Please try again."; | ||
| 102 | setChatLog([...chatLogNew, { user: "gpt", message: `<div class="errormsg"><span>i</span><div class="msg">${errorMsg}</div></div>`} ]) | ||
| 98 | } | 103 | } |
| 99 | setChatLog([...chatLogNew, { user: "gpt", message: `${replaceTags}`} ]) | ||
| 100 | var scrollToTheBottomChatLog = document.getElementsByClassName("chat-log")[0]; | ||
| 101 | scrollToTheBottomChatLog.scrollTop = scrollToTheBottomChatLog.scrollHeight; | ||
| 102 | } | 104 | } |
| 103 | 105 | ||
| 104 | function handleTemp(temp) { | 106 | function handleTemp(temp) { | ... | ... |
| ... | @@ -2,6 +2,32 @@ | ... | @@ -2,6 +2,32 @@ |
| 2 | background-color: #101827 !important; | 2 | background-color: #101827 !important; |
| 3 | } | 3 | } |
| 4 | 4 | ||
| 5 | .errormsg { | ||
| 6 | border: 1px solid #7ac5ff; | ||
| 7 | padding: 15px 25px; | ||
| 8 | border-radius: 10px; | ||
| 9 | background: rgb(0 139 245 / 6%); | ||
| 10 | } | ||
| 11 | |||
| 12 | .errormsg .msg { | ||
| 13 | display: inline-block; | ||
| 14 | width: 90%; | ||
| 15 | } | ||
| 16 | .errormsg span { | ||
| 17 | background: #008BF5; | ||
| 18 | padding: 1px 11px; | ||
| 19 | border-radius: 50px; | ||
| 20 | width: 25px; | ||
| 21 | height: 25px; | ||
| 22 | margin-right: 10px; | ||
| 23 | color: #fff; | ||
| 24 | font-weight: 900; | ||
| 25 | display: inline-block; | ||
| 26 | vertical-align: top; | ||
| 27 | font-family: auto; | ||
| 28 | font-size: 15px; | ||
| 29 | } | ||
| 30 | |||
| 5 | .side-menu-button { | 31 | .side-menu-button { |
| 6 | border:0 solid white; | 32 | border:0 solid white; |
| 7 | /* Permalink - use to edit and share this gradient: https://colorzilla.com/gradient-editor/#cedbe9+0,aac5de+17,6199c7+50,3a84c3+51,419ad6+59,4bb8f0+71,3a8bc2+84,26558b+100;Blue+Gloss */ | 33 | /* Permalink - use to edit and share this gradient: https://colorzilla.com/gradient-editor/#cedbe9+0,aac5de+17,6199c7+50,3a84c3+51,419ad6+59,4bb8f0+71,3a8bc2+84,26558b+100;Blue+Gloss */ |
| ... | @@ -172,9 +198,16 @@ code br:nth-child(-n+2) { | ... | @@ -172,9 +198,16 @@ code br:nth-child(-n+2) { |
| 172 | } | 198 | } |
| 173 | 199 | ||
| 174 | @media (max-width: 414px) { | 200 | @media (max-width: 414px) { |
| 201 | .errormsg .msg { | ||
| 202 | width: 80%; | ||
| 203 | } | ||
| 175 | .message { | 204 | .message { |
| 176 | font-size: 14px; | 205 | font-size: 14px; |
| 177 | } | 206 | } |
| 207 | .errormsg { | ||
| 208 | padding: 10px; | ||
| 209 | width: 100%; | ||
| 210 | } | ||
| 178 | .chat-message-center { | 211 | .chat-message-center { |
| 179 | padding: 20px 5vw !important; | 212 | padding: 20px 5vw !important; |
| 180 | } | 213 | } | ... | ... |
| ... | @@ -3,6 +3,7 @@ const express = require('express') | ... | @@ -3,6 +3,7 @@ const express = require('express') |
| 3 | const bodyParser = require('body-parser') | 3 | const bodyParser = require('body-parser') |
| 4 | const cors = require('cors') | 4 | const cors = require('cors') |
| 5 | require('dotenv').config() | 5 | require('dotenv').config() |
| 6 | const rateLimit = require('express-rate-limit') | ||
| 6 | 7 | ||
| 7 | // Open AI Configuration | 8 | // Open AI Configuration |
| 8 | // console.log(process.env.OPENAI_API_ORG) | 9 | // console.log(process.env.OPENAI_API_ORG) |
| ... | @@ -12,6 +13,12 @@ const configuration = new Configuration({ | ... | @@ -12,6 +13,12 @@ const configuration = new Configuration({ |
| 12 | }); | 13 | }); |
| 13 | const openai = new OpenAIApi(configuration); | 14 | const openai = new OpenAIApi(configuration); |
| 14 | 15 | ||
| 16 | const rateLimiter = rateLimit({ | ||
| 17 | windowMs: 1000 * 60 * 1, // 1 minute (refreshTime) | ||
| 18 | max: 3000, // limit each IP to x requests per windowMs (refreshTime) | ||
| 19 | message: 'Sorry, too many requests. Please try again in a bit!', | ||
| 20 | }); | ||
| 21 | |||
| 15 | // Express Configuration | 22 | // Express Configuration |
| 16 | const app = express() | 23 | const app = express() |
| 17 | const port = 3080 | 24 | const port = 3080 |
| ... | @@ -19,7 +26,7 @@ const port = 3080 | ... | @@ -19,7 +26,7 @@ const port = 3080 |
| 19 | app.use(bodyParser.json()) | 26 | app.use(bodyParser.json()) |
| 20 | app.use(cors()) | 27 | app.use(cors()) |
| 21 | app.use(require('morgan')('dev')) | 28 | app.use(require('morgan')('dev')) |
| 22 | 29 | app.use(rateLimiter) | |
| 23 | 30 | ||
| 24 | // Routing | 31 | // Routing |
| 25 | 32 | ... | ... |
| ... | @@ -13,6 +13,7 @@ | ... | @@ -13,6 +13,7 @@ |
| 13 | "cors": "^2.8.5", | 13 | "cors": "^2.8.5", |
| 14 | "dotenv": "^16.0.3", | 14 | "dotenv": "^16.0.3", |
| 15 | "express": "^4.18.2", | 15 | "express": "^4.18.2", |
| 16 | "express-rate-limit": "^6.7.0", | ||
| 16 | "morgan": "^1.10.0", | 17 | "morgan": "^1.10.0", |
| 17 | "openai": "^3.1.0" | 18 | "openai": "^3.1.0" |
| 18 | } | 19 | } | ... | ... |
-
Please register or sign in to post a comment