From da99c571c2a53b6bbe9efff79ef2db09cd38f2fd Mon Sep 17 00:00:00 2001 From: Sagar Hedaoo Date: Sat, 18 May 2024 01:28:47 -0400 Subject: [PATCH] Added the UI for like/dislike --- .gitignore | 3 ++- chat/components/chat.py | 37 +++++++++++++++++++++++++++++-------- chat/state.py | 29 ++++++----------------------- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 83339d8..025c376 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ bun.lockb dist/* frontend.zip poetry.lock -venv/ \ No newline at end of file +venv/ +.env \ No newline at end of file diff --git a/chat/components/chat.py b/chat/components/chat.py index 941b93c..43d1532 100644 --- a/chat/components/chat.py +++ b/chat/components/chat.py @@ -3,19 +3,26 @@ from chat.components import loading_icon from chat.state import QA, State +message_style = dict( + display="inline-block", + padding="1em", + border_radius="8px", + max_width=["30em", "30em", "50em", "50em", "50em", "50em"], +) -message_style = dict(display="inline-block", padding="1em", border_radius="8px", max_width=["30em", "30em", "50em", "50em", "50em", "50em"]) +def increment_likes(chat_name: str, index: int): + """Increment the likes count for a given message.""" + State.chats[chat_name][index].likes += 1 -def message(qa: QA) -> rx.Component: - """A single question/answer message. - Args: - qa: The question/answer pair. +def increment_dislikes(chat_name: str, index: int): + """Increment the dislikes count for a given message.""" + State.chats[chat_name][index].dislikes += 1 - Returns: - A component displaying the question/answer pair. - """ + +def message(qa: QA, index: int) -> rx.Component: + """A single question/answer message with like/dislike buttons.""" return rx.box( rx.box( rx.markdown( @@ -37,6 +44,20 @@ def message(qa: QA) -> rx.Component: text_align="left", padding_top="1em", ), + rx.hstack( + rx.button( + rx.icon("thumbs-up", size=18), + rx.text(str(qa.likes)), + # on_click=lambda: increment_likes(State.current_chat, index), + ), + rx.button( + rx.icon("thumbs-down", size=18), + rx.text(str(qa.dislikes)), + # on_click=lambda: increment_dislikes(State.current_chat, index), + ), + align_items="center", + justify_content="space-between", + ), width="100%", ) diff --git a/chat/state.py b/chat/state.py index f8047b0..5efcd11 100644 --- a/chat/state.py +++ b/chat/state.py @@ -2,7 +2,6 @@ import reflex as rx from openai import OpenAI - # Checking if the API key is set properly if not os.getenv("OPENAI_API_KEY"): raise Exception("Please set OPENAI_API_KEY environment variable.") @@ -13,6 +12,8 @@ class QA(rx.Base): question: str answer: str + likes: int = 0 + dislikes: int = 0 DEFAULT_CHATS = { @@ -52,20 +53,12 @@ def delete_chat(self): self.current_chat = list(self.chats.keys())[0] def set_chat(self, chat_name: str): - """Set the name of the current chat. - - Args: - chat_name: The name of the chat. - """ + """Set the name of the current chat.""" self.current_chat = chat_name @rx.var def chat_titles(self) -> list[str]: - """Get the list of chat titles. - - Returns: - The list of chat names. - """ + """Get the list of chat titles.""" return list(self.chats.keys()) async def process_question(self, form_data: dict[str, str]): @@ -82,21 +75,14 @@ async def process_question(self, form_data: dict[str, str]): yield value async def openai_process_question(self, question: str): - """Get the response from the API. - - Args: - form_data: A dict with the current question. - """ - - # Add the question to the list of questions. - qa = QA(question=question, answer="") + """Get the response from the API.""" + qa = QA(question=question, answer="", likes=0, dislikes=0) self.chats[self.current_chat].append(qa) # Clear the input and start the processing. self.processing = True yield - # Build the messages. messages = [ { "role": "system", @@ -121,12 +107,9 @@ async def openai_process_question(self, question: str): for item in session: if hasattr(item.choices[0].delta, "content"): answer_text = item.choices[0].delta.content - # Ensure answer_text is not None before concatenation if answer_text is not None: self.chats[self.current_chat][-1].answer += answer_text else: - # Handle the case where answer_text is None, perhaps log it or assign a default value - # For example, assigning an empty string if answer_text is None answer_text = "" self.chats[self.current_chat][-1].answer += answer_text self.chats = self.chats