@@ -254,19 +254,18 @@ async def chat(q: Q):
254254 ],
255255 ),
256256 ),
257- if not q .args .chatbot :
258- add_card (
259- q ,
260- "chat_card" ,
261- ui .chatbot_card (
262- box = ui .box ("vertical" , height = "500px" ),
263- name = "chatbot" ,
264- placeholder = "Type your question here, happy to help!" ,
265- data = data (fields = "content from_user" , t = "list" , size = - 50 , rows = q .client .chat_buffer ),
266- commands = chat_card_command_items ,
267- events = ["scroll_up" ],
268- ),
257+ add_card (
258+ q ,
259+ "chat_card" ,
260+ ui .chatbot_card (
261+ box = ui .box ("vertical" , height = "500px" ),
262+ name = "chatbot" ,
263+ placeholder = "Type your question here, happy to help!" ,
264+ data = data (fields = "content from_user" , t = "list" , size = - 50 , rows = q .client .chat_buffer ),
265+ commands = chat_card_command_items ,
266+ events = ["scroll_up" ],
269267 ),
268+ ),
270269
271270 # additional actions
272271 await draw_additional_actions (q )
@@ -298,6 +297,7 @@ def _execute_suggestions(q: Q, loop: asyncio.AbstractEventLoop, time_out=50):
298297@on ("chatbot" )
299298async def chatbot (q : Q ):
300299 q .page ["sidebar" ].value = "#chat"
300+ q .args .re_drawn = False
301301
302302 # Append user message.
303303 q .client .chat_buffer .append ([q .args .chatbot , True ])
@@ -337,8 +337,6 @@ async def chatbot(q: Q):
337337 llm_response = f"The selected dataset has total number of { n_cols } columns.\n Below is quick preview:\n { df_markdown } "
338338 elif q .args .chatbot and (q .args .chatbot .lower () == "recommend questions" or q .args .chatbot .lower () == "recommend qs" ):
339339 await q .page .save ()
340- #_rows = q.page["chat_card"].data.rows
341- #import pdb; pdb.set_trace()
342340 del q .page ["chat_card" ]
343341 del q .page ["additional_actions" ]
344342 add_card (
@@ -373,7 +371,7 @@ async def chatbot(q: Q):
373371 events = ["scroll_up" ],
374372 )),
375373 await draw_additional_actions (q )
376- q .args .chatbot = None
374+ q .args .re_drawn = True
377375 elif q .args .chatbot and q .args .chatbot .lower () == "db setup" :
378376 llm_response , err = db_setup (
379377 db_name = q .client .db_name ,
@@ -447,6 +445,8 @@ async def chatbot(q: Q):
447445 llm_response = "Something went wrong, try executing the query again!"
448446 q .client .llm_response = llm_response
449447 q .client .chat_buffer .append ([llm_response , False ])
448+ if not q .args .re_drawn :
449+ q .page ["chat_card" ].data += [llm_response , False ]
450450
451451
452452@on ("submit_url_keys" )
@@ -851,12 +851,15 @@ async def on_event(q: Q):
851851
852852 if q .args .regenerate_with_options :
853853 q .args .chatbot = "try harder"
854+ q .client .chat_buffer .append ([q .args .chatbot , True ])
854855 elif q .args .regenerate :
855856 q .args .chatbot = "regenerate"
857+ q .client .chat_buffer .append ([q .args .chatbot , True ])
856858 q .client .eval_mode = False
857859
858860 if q .args .suggest :
859861 q .args .chatbot = "Recommend questions"
862+ q .client .chat_buffer .append ([q .args .chatbot , True ])
860863 await chatbot (q )
861864 event_handled = True
862865 if q .args .eval_mode :
@@ -868,6 +871,7 @@ async def on_event(q: Q):
868871 logging .info (f"User selected table: { q .args .table_dropdown } " )
869872 await submit_table (q )
870873 q .args .chatbot = f"Table { q .args .table_dropdown } selected"
874+ q .client .chat_buffer .append ([q .args .chatbot , True ])
871875 # Refresh response is triggered when user selects a table via dropdown
872876 event_handled = True
873877 if (
@@ -878,6 +882,7 @@ async def on_event(q: Q):
878882 q .client .model_choice_dropdown = q .args .model_choice_dropdown
879883 q .page ["select_tables" ].model_choice_dropdown .value = q .client .model_choice_dropdown
880884 q .args .chatbot = f"Model { q .client .model_choice_dropdown } selected"
885+ q .client .chat_buffer .append ([q .args .chatbot , True ])
881886 # Refresh response is triggered when user selects a table via dropdown
882887 q .args .model_choice_dropdown = None
883888 event_handled = True
@@ -886,6 +891,7 @@ async def on_event(q: Q):
886891 q .client .task_dropdown = q .args .task_dropdown
887892 q .page ["task_choice" ].task_dropdown .value = q .client .task_dropdown
888893 q .args .chatbot = f"'{ TASK_CHOICE [q .client .task_dropdown ]} ' mode selected"
894+ q .client .chat_buffer .append ([q .args .chatbot , True ])
889895 q .args .task_dropdown = None
890896 # Refresh response is triggered when user selects a table via dropdown
891897 event_handled = True
@@ -975,6 +981,7 @@ async def on_event(q: Q):
975981 q .args .chatbot = (
976982 f"Demo mode is enabled.\n Try below example questions for the selected data to get started,\n { sample_qs } "
977983 )
984+ q .client .chat_buffer .append ([q .args .chatbot , True ])
978985 q .page ["chat_card" ].data += [q .args .chatbot , False ]
979986 q .args .table_dropdown = None
980987 q .args .model_choice_dropdown = None
0 commit comments