diff --git a/A2rchi/interfaces/chat_app/app.py b/A2rchi/interfaces/chat_app/app.py index 2d0e451..0de792b 100644 --- a/A2rchi/interfaces/chat_app/app.py +++ b/A2rchi/interfaces/chat_app/app.py @@ -110,7 +110,7 @@ def query_conversation_history(self, conversation_id): return history - def insert_conversation(self, conversation_id, user_message, a2rchi_message) -> List[int]: + def insert_conversation(self, conversation_id, user_message, a2rchi_message, is_refresh=False) -> List[int]: """ """ print(" INFO - entered insert_conversation.") @@ -120,11 +120,17 @@ def insert_conversation(self, conversation_id, user_message, a2rchi_message) -> a2rchi_sender, a2rchi_content, a2rchi_msg_ts = a2rchi_message # construct insert_tups - insert_tups = [ - # (conversation_id, sender, content, ts) - (conversation_id, user_sender, user_content, user_msg_ts), - (conversation_id, a2rchi_sender, a2rchi_content, a2rchi_msg_ts), - ] + insert_tups = ( + [ + # (conversation_id, sender, content, ts) + (conversation_id, user_sender, user_content, user_msg_ts), + (conversation_id, a2rchi_sender, a2rchi_content, a2rchi_msg_ts), + ] + if not is_refresh + else [ + (conversation_id, a2rchi_sender, a2rchi_content, a2rchi_msg_ts), + ] + ) # create connection to database self.conn = psycopg2.connect(**self.pg_config) @@ -218,7 +224,7 @@ def __call__(self, message: List[str], conversation_id: int, is_refresh: bool, m print(user_message) print(a2rchi_message) - message_ids = self.insert_conversation(conversation_id, user_message, a2rchi_message) + message_ids = self.insert_conversation(conversation_id, user_message, a2rchi_message, is_refresh) except Exception as e: raise e diff --git a/A2rchi/interfaces/chat_app/static/script.js-template b/A2rchi/interfaces/chat_app/static/script.js-template index 6ffddb7..4ba5597 100644 --- a/A2rchi/interfaces/chat_app/static/script.js-template +++ b/A2rchi/interfaces/chat_app/static/script.js-template @@ -17,6 +17,7 @@ let userText = null; let conversation_id = null; let conversation = [] let num_responses_since_last_rating = 0; +let last_response_is_feedback_request = false; const loadDataFromLocalstorage = () => { // Load saved chats and theme from local storage and apply/add on the page @@ -50,8 +51,16 @@ const refreshChat = async () => { return; } + // remove message to be regenerated from conversation conversation.pop(); chatContainer.removeChild(chatContainer.lastChild); + + // if the last response generated a feedback request, make sure to remove it here as well + if (last_response_is_feedback_request) { + chatContainer.removeChild(chatContainer.lastChild); + } + + // generate new response showTypingAnimation(isRefresh=true); } @@ -80,6 +89,7 @@ const getChatResponse = async (incomingChatDiv, isRefresh=false) => { pElement.classList.add(".default-text"); conversation.push(["A2rchi", response.response]); conversation_id = response.conversation_id; + last_response_is_feedback_request = false; } catch (error) { pElement.classList.add("error"); pElement.textContent = "Oops! Something went wrong while retrieving the response. Please try again."; @@ -210,7 +220,7 @@ const showFeedbackRequest = () => {
chatbot-img
-

I've noticed you haven't rated any of my responses in awhile. Rating responses is crucial because it not only helps me improve, but it also ensures that this project remains open source and freely accessible for everyone. Your input is highly valuable in supporting the A2rchi mission!

+

I've noticed you haven't rated any of my responses in a while. Rating responses is crucial because it not only helps me improve, but it also ensures that this project remains open source and freely accessible for everyone. Your input is highly valuable in supporting the A2rchi mission!

`; @@ -221,6 +231,7 @@ const showFeedbackRequest = () => { chatContainer.appendChild(incomingChatDiv); chatContainer.scrollTo(0, chatContainer.scrollHeight); num_responses_since_last_rating = 0; + last_response_is_feedback_request = true; } }