Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the UI for like/dislike #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ bun.lockb
dist/*
frontend.zip
poetry.lock
venv/
venv/
.env
37 changes: 29 additions & 8 deletions chat/components/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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%",
)

Expand Down
29 changes: 6 additions & 23 deletions chat/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -13,6 +12,8 @@ class QA(rx.Base):

question: str
answer: str
likes: int = 0
dislikes: int = 0


DEFAULT_CHATS = {
Expand Down Expand Up @@ -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]):
Expand All @@ -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",
Expand All @@ -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
Expand Down