fix typing
Showing
1 changed file
with
21 additions
and
11 deletions
| ... | @@ -20,7 +20,6 @@ function App() { | ... | @@ -20,7 +20,6 @@ function App() { |
| 20 | message: "Welcome to AI-PRO... How can I help you?" | 20 | message: "Welcome to AI-PRO... How can I help you?" |
| 21 | }]); | 21 | }]); |
| 22 | 22 | ||
| 23 | // clear chats | ||
| 24 | function clearChat(){ | 23 | function clearChat(){ |
| 25 | setChatLog([]); | 24 | setChatLog([]); |
| 26 | setChatInput(""); | 25 | setChatInput(""); |
| ... | @@ -31,8 +30,6 @@ function App() { | ... | @@ -31,8 +30,6 @@ function App() { |
| 31 | fetch(process.env.REACT_APP_SERVER_URL + "/models") | 30 | fetch(process.env.REACT_APP_SERVER_URL + "/models") |
| 32 | .then(res => res.json()) | 31 | .then(res => res.json()) |
| 33 | .then(data => { | 32 | .then(data => { |
| 34 | // console.log(data.models.data) | ||
| 35 | // set models in order alpahbetically | ||
| 36 | data.models.data.sort((a, b) => { | 33 | data.models.data.sort((a, b) => { |
| 37 | if(a.id < b.id) { return -1; } | 34 | if(a.id < b.id) { return -1; } |
| 38 | if(a.id > b.id) { return 1; } | 35 | if(a.id > b.id) { return 1; } |
| ... | @@ -44,7 +41,7 @@ function App() { | ... | @@ -44,7 +41,7 @@ function App() { |
| 44 | 41 | ||
| 45 | async function handleSubmit(e){ | 42 | async function handleSubmit(e){ |
| 46 | e.preventDefault(); | 43 | e.preventDefault(); |
| 47 | // console.log(chatInput) | 44 | |
| 48 | const userInput = ['what', 'why', 'when', 'where' , 'which', 'did', 'do', 'how', 'can', 'are', 'who']; | 45 | const userInput = ['what', 'why', 'when', 'where' , 'which', 'did', 'do', 'how', 'can', 'are', 'who']; |
| 49 | const userInputRegex = new RegExp(`\\b(${userInput.join('|')})\\b`, 'gi'); | 46 | const userInputRegex = new RegExp(`\\b(${userInput.join('|')})\\b`, 'gi'); |
| 50 | const inputMatches = chatInput.match(userInputRegex); | 47 | const inputMatches = chatInput.match(userInputRegex); |
| ... | @@ -53,16 +50,13 @@ function App() { | ... | @@ -53,16 +50,13 @@ function App() { |
| 53 | const userPunctuationRegex = new RegExp(`[${userPunctuation.join('')}]$`); | 50 | const userPunctuationRegex = new RegExp(`[${userPunctuation.join('')}]$`); |
| 54 | const punctuationMatches = chatInput.match(userPunctuationRegex); | 51 | const punctuationMatches = chatInput.match(userPunctuationRegex); |
| 55 | 52 | ||
| 56 | // console.log(punctuationMatches) | ||
| 57 | var userModifiedInput = chatInput | 53 | var userModifiedInput = chatInput |
| 58 | 54 | ||
| 59 | if (!punctuationMatches) { | 55 | if (!punctuationMatches) { |
| 60 | if (!inputMatches) { | 56 | if (!inputMatches) { |
| 61 | userModifiedInput = chatInput + "."; | 57 | userModifiedInput = chatInput + "."; |
| 62 | // console.log("not a question!") | ||
| 63 | } else { | 58 | } else { |
| 64 | userModifiedInput = chatInput + "?"; | 59 | userModifiedInput = chatInput + "?"; |
| 65 | // console.log("its a question!") | ||
| 66 | } | 60 | } |
| 67 | } | 61 | } |
| 68 | 62 | ||
| ... | @@ -70,7 +64,6 @@ function App() { | ... | @@ -70,7 +64,6 @@ function App() { |
| 70 | setChatInput(""); | 64 | setChatInput(""); |
| 71 | setChatLog(chatLogNew) | 65 | setChatLog(chatLogNew) |
| 72 | 66 | ||
| 73 | // fetch response to the api combining the chat log array of messages and seinding it as a message to localhost:3000 as a post | ||
| 74 | const messages = chatLogNew.map((message) => message.message).join("\n") | 67 | const messages = chatLogNew.map((message) => message.message).join("\n") |
| 75 | 68 | ||
| 76 | try { | 69 | try { |
| ... | @@ -95,10 +88,27 @@ function App() { | ... | @@ -95,10 +88,27 @@ function App() { |
| 95 | } else { | 88 | } else { |
| 96 | replaceTags = (parsedData.replace(':',':<code>').replace('<?','<?').replace('?>','?>').replace(/\n/g, '<br>')) | 89 | replaceTags = (parsedData.replace(':',':<code>').replace('<?','<?').replace('?>','?>').replace(/\n/g, '<br>')) |
| 97 | } | 90 | } |
| 98 | setChatLog([...chatLogNew, { user: "gpt", message: `${replaceTags}`} ]) | ||
| 99 | 91 | ||
| 100 | var scrollToTheBottomChatLog = document.getElementsByClassName("chat-log")[0]; | 92 | for (let i = 0; i < replaceTags.length; i++) { |
| 101 | scrollToTheBottomChatLog.scrollTop = scrollToTheBottomChatLog.scrollHeight; | 93 | setTimeout(() => { |
| 94 | const parsedMsg = replaceTags.slice(0, i + 1); | ||
| 95 | updateLastMessage(parsedMsg); | ||
| 96 | var scrollToTheBottomChatLog = document.getElementsByClassName("chat-log")[0]; | ||
| 97 | scrollToTheBottomChatLog.scrollTop = scrollToTheBottomChatLog.scrollHeight; | ||
| 98 | }, i * 5); | ||
| 99 | } | ||
| 100 | |||
| 101 | function updateLastMessage(parsedMsg) { | ||
| 102 | setChatLog(prevChatLog => { | ||
| 103 | const lastMsg = prevChatLog[prevChatLog.length - 1]; | ||
| 104 | if (lastMsg && lastMsg.user === "gpt") { | ||
| 105 | return [...prevChatLog.slice(0, prevChatLog.length - 1), { user: lastMsg.user, message: parsedMsg }]; | ||
| 106 | } else { | ||
| 107 | return [...prevChatLog, { user: "gpt", message: parsedMsg }]; | ||
| 108 | } | ||
| 109 | }); | ||
| 110 | } | ||
| 111 | |||
| 102 | } catch (error) { | 112 | } catch (error) { |
| 103 | const errorMsg = "We apologize for any inconvenience caused due to the delay in the response time. Please try again."; | 113 | const errorMsg = "We apologize for any inconvenience caused due to the delay in the response time. Please try again."; |
| 104 | setChatLog([...chatLogNew, { user: "gpt", message: `<div class="errormsg"><span>i</span><div class="msg">${errorMsg}</div></div>`} ]) | 114 | setChatLog([...chatLogNew, { user: "gpt", message: `<div class="errormsg"><span>i</span><div class="msg">${errorMsg}</div></div>`} ]) | ... | ... |
-
Please register or sign in to post a comment