|
| 1 | +"""Utility classes for the chat app tutorial.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | + |
| 7 | +import openai # pyright: ignore[reportMissingImports] |
| 8 | + |
| 9 | +import reflex as rx |
| 10 | + |
| 11 | +OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") |
| 12 | + |
| 13 | + |
| 14 | +class ChatappState(rx.State): |
| 15 | + """State for the chat app tutorial.""" |
| 16 | + |
| 17 | + # The current question being asked. |
| 18 | + question: str |
| 19 | + |
| 20 | + # Keep track of the chat history as a list of (question, answer) tuples. |
| 21 | + chat_history: list[tuple[str, str]] |
| 22 | + |
| 23 | + def set_question(self, q: str): |
| 24 | + """Set the current question.""" |
| 25 | + self.question = q |
| 26 | + |
| 27 | + def set_question1(self, q: str): |
| 28 | + """Set the current question (variant 1).""" |
| 29 | + self.question = q |
| 30 | + |
| 31 | + def set_question2(self, q: str): |
| 32 | + """Set the current question (variant 2).""" |
| 33 | + self.question = q |
| 34 | + |
| 35 | + def set_question3(self, q: str): |
| 36 | + """Set the current question (variant 3).""" |
| 37 | + self.question = q |
| 38 | + |
| 39 | + def answer(self) -> None: |
| 40 | + """Answer the question with a static response.""" |
| 41 | + # Our chatbot is not very smart right now... |
| 42 | + answer = "I don't know!" |
| 43 | + self.chat_history.append((self.question, answer)) |
| 44 | + |
| 45 | + def answer2(self) -> None: |
| 46 | + """Answer the question and clear the input.""" |
| 47 | + # Our chatbot is not very smart right now... |
| 48 | + answer = "I don't know!" |
| 49 | + self.chat_history.append((self.question, answer)) |
| 50 | + # Clear the question input. |
| 51 | + self.question = "" |
| 52 | + |
| 53 | + async def answer3(self): |
| 54 | + """Answer with a streaming static response.""" |
| 55 | + import asyncio |
| 56 | + |
| 57 | + # Our chatbot is not very smart right now... |
| 58 | + answer = "I don't know!" |
| 59 | + self.chat_history.append((self.question, "")) |
| 60 | + |
| 61 | + # Clear the question input. |
| 62 | + self.question = "" |
| 63 | + # Yield here to clear the frontend input before continuing. |
| 64 | + yield |
| 65 | + |
| 66 | + for i in range(len(answer)): |
| 67 | + await asyncio.sleep(0.1) |
| 68 | + self.chat_history[-1] = (self.chat_history[-1][0], answer[: i + 1]) |
| 69 | + yield |
| 70 | + |
| 71 | + async def answer4(self): |
| 72 | + """Answer using the OpenAI API with streaming.""" |
| 73 | + # Our chatbot has some brains now! |
| 74 | + client = openai.AsyncOpenAI(api_key=OPENAI_API_KEY) |
| 75 | + session = await client.chat.completions.create( |
| 76 | + model="gpt-4o-mini", |
| 77 | + messages=[{"role": "user", "content": self.question}], |
| 78 | + stop=None, |
| 79 | + temperature=0.7, |
| 80 | + stream=True, |
| 81 | + ) |
| 82 | + |
| 83 | + # Add to the answer as the chatbot responds. |
| 84 | + answer = "" |
| 85 | + self.chat_history.append((self.question, answer)) |
| 86 | + |
| 87 | + # Clear the question input. |
| 88 | + self.question = "" |
| 89 | + # Yield here to clear the frontend input before continuing. |
| 90 | + yield |
| 91 | + |
| 92 | + async for item in session: |
| 93 | + if hasattr(item.choices[0].delta, "content"): |
| 94 | + if item.choices[0].delta.content is None: |
| 95 | + # presence of 'None' indicates the end of the response |
| 96 | + break |
| 97 | + answer += item.choices[0].delta.content |
| 98 | + self.chat_history[-1] = (self.chat_history[-1][0], answer) |
| 99 | + yield |
0 commit comments