From e9a2b8f03a3b8e7f732abf50107934edb248641c Mon Sep 17 00:00:00 2001 From: Niharika Goulikar Date: Tue, 26 Nov 2024 12:16:21 +0000 Subject: [PATCH 1/4] Fixed the feedback issue --- application/api/user/routes.py | 24 +++++++++++-------- frontend/src/conversation/Conversation.tsx | 2 +- .../src/conversation/ConversationBubble.tsx | 20 +++++++++++++--- .../src/conversation/conversationHandlers.ts | 4 ++++ 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/application/api/user/routes.py b/application/api/user/routes.py index e305845d4..0e4a9f4db 100644 --- a/application/api/user/routes.py +++ b/application/api/user/routes.py @@ -176,10 +176,12 @@ class SubmitFeedback(Resource): "FeedbackModel", { "question": fields.String( - required=True, description="The user question" + required=False, description="The user question" ), - "answer": fields.String(required=True, description="The AI answer"), + "answer": fields.String(required=False, description="The AI answer"), "feedback": fields.String(required=True, description="User feedback"), + "question_index":fields.Integer(required=True, description="The question number in that particular conversation"), + "conversation_id":fields.String(required=True, description="id of the particular conversation"), "api_key": fields.String(description="Optional API key"), }, ) @@ -189,23 +191,25 @@ class SubmitFeedback(Resource): ) def post(self): data = request.get_json() - required_fields = ["question", "answer", "feedback"] + required_fields = [ "feedback","conversation_id","question_index"] missing_fields = check_required_fields(data, required_fields) if missing_fields: return missing_fields - new_doc = { - "question": data["question"], - "answer": data["answer"], - "feedback": data["feedback"], - "timestamp": datetime.datetime.now(datetime.timezone.utc), - } if "api_key" in data: new_doc["api_key"] = data["api_key"] try: - feedback_collection.insert_one(new_doc) + conversations_collection.update_one( + {"_id": ObjectId(data["conversation_id"]), f"queries.{data["question_index"]}": {"$exists": True}}, + { + "$set": { + f"queries.{data["question_index"]}.feedback": data["feedback"] + } + } + ) + except Exception as err: return make_response(jsonify({"success": False, "error": str(err)}), 400) diff --git a/frontend/src/conversation/Conversation.tsx b/frontend/src/conversation/Conversation.tsx index ed69064ac..41bc8d2f4 100644 --- a/frontend/src/conversation/Conversation.tsx +++ b/frontend/src/conversation/Conversation.tsx @@ -111,7 +111,7 @@ export default function Conversation() { const handleFeedback = (query: Query, feedback: FEEDBACK, index: number) => { const prevFeedback = query.feedback; dispatch(updateQuery({ index, query: { feedback } })); - handleSendFeedback(query.prompt, query.response!, feedback).catch(() => + handleSendFeedback(query.prompt, query.response!, feedback,conversationId as string,index).catch(() => dispatch(updateQuery({ index, query: { feedback: prevFeedback } })), ); }; diff --git a/frontend/src/conversation/ConversationBubble.tsx b/frontend/src/conversation/ConversationBubble.tsx index 567b09e91..7fe1b0036 100644 --- a/frontend/src/conversation/ConversationBubble.tsx +++ b/frontend/src/conversation/ConversationBubble.tsx @@ -429,6 +429,11 @@ const ConversationBubble = forwardRef< feedback === 'LIKE' || type !== 'ERROR' ? 'group-hover:lg:visible' : '' + } + ${ + feedback === 'DISLIKE' && type !== 'ERROR' + ? ' hidden' + : '' }`} >
@@ -445,11 +450,14 @@ const ConversationBubble = forwardRef< isLikeClicked || feedback === 'LIKE' ? 'fill-white-3000 stroke-purple-30 dark:fill-transparent' : 'fill-none stroke-gray-4000' - }`} + } `} onClick={() => { + if(feedback===undefined){ + console.log("liked") handleFeedback?.('LIKE'); setIsLikeClicked(true); setIsDislikeClicked(false); + } }} onMouseEnter={() => setIsLikeHovered(true)} onMouseLeave={() => setIsLikeHovered(false)} @@ -462,9 +470,13 @@ const ConversationBubble = forwardRef< !isDislikeClicked ? 'lg:invisible' : '' } ${ feedback === 'DISLIKE' || type !== 'ERROR' - ? 'group-hover:lg:visible' + ? ' group-hover:lg:visible' : '' - }`} + } ${ + feedback === 'LIKE' && type !== 'ERROR' + ? ' hidden' + : '' + } `} >
{ + if(feedback===undefined){ handleFeedback?.('DISLIKE'); setIsDislikeClicked(true); setIsLikeClicked(false); + } }} onMouseEnter={() => setIsDislikeHovered(true)} onMouseLeave={() => setIsDislikeHovered(false)} diff --git a/frontend/src/conversation/conversationHandlers.ts b/frontend/src/conversation/conversationHandlers.ts index be046bca4..ea8ad6ea7 100644 --- a/frontend/src/conversation/conversationHandlers.ts +++ b/frontend/src/conversation/conversationHandlers.ts @@ -202,12 +202,16 @@ export function handleSendFeedback( prompt: string, response: string, feedback: FEEDBACK, + conversation_id:string, + prompt_index:number ) { return conversationService .feedback({ question: prompt, answer: response, feedback: feedback, + conversation_id:conversation_id, + question_index:prompt_index }) .then((response) => { if (response.ok) { From faf031ce8037aac6abbbb35ad66447df98c5ff3b Mon Sep 17 00:00:00 2001 From: Niharika Goulikar Date: Tue, 26 Nov 2024 12:28:16 +0000 Subject: [PATCH 2/4] fixed linting issues --- application/api/user/routes.py | 4 --- .../src/conversation/ConversationBubble.tsx | 33 +++++++------------ 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/application/api/user/routes.py b/application/api/user/routes.py index 0e4a9f4db..96fc0e0f3 100644 --- a/application/api/user/routes.py +++ b/application/api/user/routes.py @@ -196,10 +196,6 @@ def post(self): if missing_fields: return missing_fields - - if "api_key" in data: - new_doc["api_key"] = data["api_key"] - try: conversations_collection.update_one( {"_id": ObjectId(data["conversation_id"]), f"queries.{data["question_index"]}": {"$exists": True}}, diff --git a/frontend/src/conversation/ConversationBubble.tsx b/frontend/src/conversation/ConversationBubble.tsx index 7fe1b0036..03a3ab082 100644 --- a/frontend/src/conversation/ConversationBubble.tsx +++ b/frontend/src/conversation/ConversationBubble.tsx @@ -430,11 +430,7 @@ const ConversationBubble = forwardRef< ? 'group-hover:lg:visible' : '' } - ${ - feedback === 'DISLIKE' && type !== 'ERROR' - ? ' hidden' - : '' - }`} + ${feedback === 'DISLIKE' && type !== 'ERROR' ? 'hidden' : ''}`} >
{ - if(feedback===undefined){ - console.log("liked") - handleFeedback?.('LIKE'); - setIsLikeClicked(true); - setIsDislikeClicked(false); + if (feedback === undefined) { + handleFeedback?.('LIKE'); + setIsLikeClicked(true); + setIsDislikeClicked(false); } }} onMouseEnter={() => setIsLikeHovered(true)} @@ -470,13 +465,9 @@ const ConversationBubble = forwardRef< !isDislikeClicked ? 'lg:invisible' : '' } ${ feedback === 'DISLIKE' || type !== 'ERROR' - ? ' group-hover:lg:visible' - : '' - } ${ - feedback === 'LIKE' && type !== 'ERROR' - ? ' hidden' + ? 'group-hover:lg:visible' : '' - } `} + } ${feedback === 'LIKE' && type !== 'ERROR' ? ' hidden' : ''} `} >
{ - if(feedback===undefined){ - handleFeedback?.('DISLIKE'); - setIsDislikeClicked(true); - setIsLikeClicked(false); + if (feedback === undefined) { + handleFeedback?.('DISLIKE'); + setIsDislikeClicked(true); + setIsLikeClicked(false); } }} onMouseEnter={() => setIsDislikeHovered(true)} From 9d4aee5de2a9bbefc33b61f55da5d2a5cc6e11a0 Mon Sep 17 00:00:00 2001 From: Niharika Goulikar Date: Tue, 26 Nov 2024 13:05:50 +0000 Subject: [PATCH 3/4] fixed the python error --- application/api/user/routes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/api/user/routes.py b/application/api/user/routes.py index 96fc0e0f3..6eb57cc00 100644 --- a/application/api/user/routes.py +++ b/application/api/user/routes.py @@ -198,10 +198,10 @@ def post(self): try: conversations_collection.update_one( - {"_id": ObjectId(data["conversation_id"]), f"queries.{data["question_index"]}": {"$exists": True}}, + {"_id": ObjectId(data["conversation_id"]), f"queries.{data['question_index']}": {"$exists": True}}, { "$set": { - f"queries.{data["question_index"]}.feedback": data["feedback"] + f"queries.{data['question_index']}.feedback": data["feedback"] } } ) From 47ecf98e2a868ff24303dc226c80663238735b57 Mon Sep 17 00:00:00 2001 From: Niharika Goulikar Date: Wed, 18 Dec 2024 10:47:34 +0000 Subject: [PATCH 4/4] Resolved merge conflicts --- application/api/user/routes.py | 2 +- frontend/src/conversation/Conversation.tsx | 19 ++++++++++++++++--- .../src/conversation/ConversationBubble.tsx | 12 ++++++++++-- .../src/conversation/conversationHandlers.ts | 8 ++++---- .../src/conversation/conversationModels.ts | 2 +- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/application/api/user/routes.py b/application/api/user/routes.py index 6eb57cc00..961e375b5 100644 --- a/application/api/user/routes.py +++ b/application/api/user/routes.py @@ -1791,4 +1791,4 @@ def post(self): 200, ) except Exception as err: - return make_response(jsonify({"success": False, "error": str(err)}), 400) + return make_response(jsonify({"success": False, "error": str(err)}), 400) \ No newline at end of file diff --git a/frontend/src/conversation/Conversation.tsx b/frontend/src/conversation/Conversation.tsx index 41bc8d2f4..1c3a296f5 100644 --- a/frontend/src/conversation/Conversation.tsx +++ b/frontend/src/conversation/Conversation.tsx @@ -15,7 +15,6 @@ import { useDarkTheme, useMediaQuery } from '../hooks'; import { ShareConversationModal } from '../modals/ShareConversationModal'; import { selectConversationId } from '../preferences/preferenceSlice'; import { AppDispatch } from '../store'; -import conversationService from '../api/services/conversationService'; import ConversationBubble from './ConversationBubble'; import { handleSendFeedback } from './conversationHandlers'; import { FEEDBACK, Query } from './conversationModels'; @@ -111,8 +110,22 @@ export default function Conversation() { const handleFeedback = (query: Query, feedback: FEEDBACK, index: number) => { const prevFeedback = query.feedback; dispatch(updateQuery({ index, query: { feedback } })); - handleSendFeedback(query.prompt, query.response!, feedback,conversationId as string,index).catch(() => - dispatch(updateQuery({ index, query: { feedback: prevFeedback } })), + handleSendFeedback( + query.prompt, + query.response!, + feedback, + conversationId as string, + index, + ).catch(() => + handleSendFeedback( + query.prompt, + query.response!, + feedback, + conversationId as string, + index, + ).catch(() => + dispatch(updateQuery({ index, query: { feedback: prevFeedback } })), + ), ); }; diff --git a/frontend/src/conversation/ConversationBubble.tsx b/frontend/src/conversation/ConversationBubble.tsx index 03a3ab082..2d6fca1a2 100644 --- a/frontend/src/conversation/ConversationBubble.tsx +++ b/frontend/src/conversation/ConversationBubble.tsx @@ -448,10 +448,14 @@ const ConversationBubble = forwardRef< : 'fill-none stroke-gray-4000' }`} onClick={() => { - if (feedback === undefined) { + if (feedback === undefined || feedback === null) { handleFeedback?.('LIKE'); setIsLikeClicked(true); setIsDislikeClicked(false); + } else if (feedback === 'LIKE') { + handleFeedback?.(null); + setIsLikeClicked(false); + setIsDislikeClicked(false); } }} onMouseEnter={() => setIsLikeHovered(true)} @@ -484,10 +488,14 @@ const ConversationBubble = forwardRef< : 'fill-none stroke-gray-4000' }`} onClick={() => { - if (feedback === undefined) { + if (feedback === undefined || feedback === null) { handleFeedback?.('DISLIKE'); setIsDislikeClicked(true); setIsLikeClicked(false); + } else if (feedback === 'DISLIKE') { + handleFeedback?.(null); + setIsLikeClicked(false); + setIsDislikeClicked(false); } }} onMouseEnter={() => setIsDislikeHovered(true)} diff --git a/frontend/src/conversation/conversationHandlers.ts b/frontend/src/conversation/conversationHandlers.ts index ea8ad6ea7..1abdb74d2 100644 --- a/frontend/src/conversation/conversationHandlers.ts +++ b/frontend/src/conversation/conversationHandlers.ts @@ -202,16 +202,16 @@ export function handleSendFeedback( prompt: string, response: string, feedback: FEEDBACK, - conversation_id:string, - prompt_index:number + conversation_id: string, + prompt_index: number, ) { return conversationService .feedback({ question: prompt, answer: response, feedback: feedback, - conversation_id:conversation_id, - question_index:prompt_index + conversation_id: conversation_id, + question_index: prompt_index, }) .then((response) => { if (response.ok) { diff --git a/frontend/src/conversation/conversationModels.ts b/frontend/src/conversation/conversationModels.ts index cd2868526..a499deecf 100644 --- a/frontend/src/conversation/conversationModels.ts +++ b/frontend/src/conversation/conversationModels.ts @@ -1,6 +1,6 @@ export type MESSAGE_TYPE = 'QUESTION' | 'ANSWER' | 'ERROR'; export type Status = 'idle' | 'loading' | 'failed'; -export type FEEDBACK = 'LIKE' | 'DISLIKE'; +export type FEEDBACK = 'LIKE' | 'DISLIKE' | null; export interface Message { text: string;