66import reflex as rx
77
88
9- def scroll_to_bottom ():
10- return """
11- function scrollToBottom() {
12- var scrollContainer = document.getElementById('scroll-container');
13- if (scrollContainer) {
14- scrollContainer.scrollTop = scrollContainer.scrollHeight;
15- }
16- console.log("scrolling to bottom");
17- }
18- scrollToBottom();
19- """
20-
21-
229class TutorialState (rx .State ):
2310 # Keep track of the chat history as a list of (question, answer) tuples.
2411 chat_history : list [tuple [str , str ]] = [
@@ -30,12 +17,13 @@ class TutorialState(rx.State):
3017
3118 @rx .event
3219 async def submit (self , form_data : dict ):
33- self .question = form_data ["question" ]
20+
21+ question = form_data ["question" ]
3422 # Our chatbot has some brains now!
3523 client = AsyncOpenAI (api_key = os .environ ["OPENAI_API_KEY" ])
3624 session = await client .chat .completions .create (
3725 model = "gpt-4o-mini" ,
38- messages = [{"role" : "user" , "content" : self . question }],
26+ messages = [{"role" : "user" , "content" : question }],
3927 stop = None ,
4028 temperature = 0.7 ,
4129 stream = True ,
@@ -44,11 +32,9 @@ async def submit(self, form_data: dict):
4432 # Add to the answer as the chatbot responds.
4533 answer = ""
4634 # self.chat_history = []
47- self .chat_history .append ((self . question , answer ))
35+ self .chat_history .append ((question , answer ))
4836 yield
4937
50- yield rx .call_script (scroll_to_bottom ())
51-
5238 async for item in session :
5339 if hasattr (item .choices [0 ].delta , "content" ):
5440 if item .choices [0 ].delta .content is None :
@@ -57,8 +43,6 @@ async def submit(self, form_data: dict):
5743 answer += item .choices [0 ].delta .content
5844 self .chat_history [- 1 ] = (self .chat_history [- 1 ][0 ], answer )
5945 yield
60- yield rx .call_script (scroll_to_bottom ())
61- yield rx .call_script (scroll_to_bottom ())
6246
6347 @rx .event
6448 def clear_chat (self ):
@@ -70,12 +54,10 @@ def qa(question: str, answer: str) -> rx.Component:
7054 rx .box (
7155 rx .text (question ),
7256 class_name = "font-base text-slate-10 rounded-lg p-2 bg-slate-3 ml-[20%] text-end self-end" ,
73- on_mount = rx .call_script (scroll_to_bottom ()),
7457 ),
7558 rx .box (
7659 rx .text (answer ),
7760 class_name = "font-base text-violet-10 rounded-lg p-2 bg-violet-3 max-w-[60%] mr-[20%] text-start self-start" ,
78- on_mount = rx .call_script (scroll_to_bottom ()),
7961 ),
8062 class_name = "flex flex-col gap-4 w-full" ,
8163 )
@@ -93,16 +75,14 @@ def chatbot() -> rx.Component:
9375 on_click = TutorialState .clear_chat ,
9476 ),
9577 rx .box (
96- rx .box (
78+ rx .auto_scroll (
9779 rx .foreach (
9880 TutorialState .chat_history ,
9981 lambda messages : qa (messages [0 ], messages [1 ]),
10082 ),
10183 rx .box (overflow_anchor = "auto" , height = "0.5rem" ),
102- id = "scroll-container" ,
10384 overflow_anchor = "none" ,
10485 class_name = "flex flex-col gap-4 w-full overflow-y-auto" ,
105- on_mount = rx .call_script (scroll_to_bottom ()),
10686 ),
10787 rx .form (
10888 rx .el .input (
0 commit comments