fix: optimize chat suggestion logic

This commit is contained in:
Tadashi 2024-10-10 14:44:50 +07:00
parent ad34395d0b
commit 3ff6af8acf
No known key found for this signature in database
GPG Key ID: 399380A00CC9028D
2 changed files with 196 additions and 171 deletions

View File

@ -1,6 +1,6 @@
import ast
import asyncio import asyncio
import csv import csv
import json
import re import re
from copy import deepcopy from copy import deepcopy
from datetime import datetime from datetime import datetime
@ -169,102 +169,115 @@ class ChatPage(BasePage):
else: else:
self.state_follow_up = self.chat_control.followup_suggestions self.state_follow_up = self.chat_control.followup_suggestions
gr.on( chat_event = (
triggers=[ gr.on(
self.chat_panel.text_input.submit, triggers=[
self.chat_panel.submit_btn.click, self.chat_panel.text_input.submit,
], self.chat_panel.submit_btn.click,
fn=self.submit_msg, ],
inputs=[ fn=self.submit_msg,
self.chat_panel.text_input, inputs=[
self.chat_panel.chatbot, self.chat_panel.text_input,
self._app.user_id, self.chat_panel.chatbot,
self.chat_control.conversation_id, self._app.user_id,
self.chat_control.conversation_rn, self.chat_control.conversation_id,
self.state_follow_up, self.chat_control.conversation_rn,
], self.state_follow_up,
outputs=[ ],
self.chat_panel.text_input, outputs=[
self.chat_panel.chatbot, self.chat_panel.text_input,
self.chat_control.conversation_id, self.chat_panel.chatbot,
self.chat_control.conversation, self.chat_control.conversation_id,
self.chat_control.conversation_rn, self.chat_control.conversation,
self.state_follow_up, self.chat_control.conversation_rn,
], self.state_follow_up,
concurrency_limit=20, ],
show_progress="hidden", concurrency_limit=20,
).success( show_progress="hidden",
fn=self.chat_fn, )
inputs=[ .success(
self.chat_control.conversation_id, fn=self.chat_fn,
self.chat_panel.chatbot, inputs=[
self._app.settings_state, self.chat_control.conversation_id,
self._reasoning_type, self.chat_panel.chatbot,
self._llm_type, self._app.settings_state,
self.state_chat, self._reasoning_type,
self._app.user_id, self._llm_type,
] self.state_chat,
+ self._indices_input, self._app.user_id,
outputs=[ ]
self.chat_panel.chatbot, + self._indices_input,
self.info_panel, outputs=[
self.plot_panel, self.chat_panel.chatbot,
self.state_plot_panel, self.info_panel,
self.state_chat, self.plot_panel,
], self.state_plot_panel,
concurrency_limit=20, self.state_chat,
show_progress="minimal", ],
).then( concurrency_limit=20,
fn=lambda: True, show_progress="minimal",
inputs=None, )
outputs=[self._preview_links], .then(
js=pdfview_js, fn=lambda: True,
).success( inputs=None,
fn=self.check_and_suggest_name_conv, outputs=[self._preview_links],
inputs=self.chat_panel.chatbot, js=pdfview_js,
outputs=[ )
self.chat_control.conversation_rn, .success(
self._conversation_renamed, fn=self.check_and_suggest_name_conv,
], inputs=self.chat_panel.chatbot,
).success( outputs=[
self.chat_control.rename_conv, self.chat_control.conversation_rn,
inputs=[ self._conversation_renamed,
self.chat_control.conversation_id, ],
self.chat_control.conversation_rn, )
self._conversation_renamed, .success(
self._app.user_id, self.chat_control.rename_conv,
], inputs=[
outputs=[ self.chat_control.conversation_id,
self.chat_control.conversation, self.chat_control.conversation_rn,
self.chat_control.conversation, self._conversation_renamed,
self.chat_control.conversation_rn, self._app.user_id,
], ],
show_progress="hidden", outputs=[
).then( self.chat_control.conversation,
fn=self.suggest_chat_conv, self.chat_control.conversation,
inputs=[ self.chat_control.conversation_rn,
self._app.settings_state, ],
self.chat_panel.chatbot, show_progress="hidden",
], )
outputs=[ )
self.state_follow_up,
self._suggestion_updated, # chat suggestion toggle
], if getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False):
show_progress="hidden", chat_event = chat_event.success(
).success( fn=self.suggest_chat_conv,
self.chat_control.update_chat_suggestions, inputs=[
inputs=[ self._app.settings_state,
self.chat_control.conversation_id, self.chat_panel.chatbot,
self.state_follow_up, ],
self._suggestion_updated, outputs=[
self._app.user_id, self.state_follow_up,
], self._suggestion_updated,
outputs=[ ],
self.chat_control.conversation, show_progress="hidden",
self.chat_control.conversation, ).success(
], self.chat_control.persist_chat_suggestions,
show_progress="hidden", inputs=[
).then( self.chat_control.conversation_id,
self.state_follow_up,
self._suggestion_updated,
self._app.user_id,
],
outputs=[
self.chat_control.conversation,
self.chat_control.conversation,
],
show_progress="hidden",
)
# final data persist
chat_event = chat_event.then(
fn=self.persist_data_source, fn=self.persist_data_source,
inputs=[ inputs=[
self.chat_control.conversation_id, self.chat_control.conversation_id,
@ -284,78 +297,90 @@ class ChatPage(BasePage):
concurrency_limit=20, concurrency_limit=20,
) )
self.chat_panel.regen_btn.click( regen_event = (
fn=self.regen_fn, self.chat_panel.regen_btn.click(
inputs=[ fn=self.regen_fn,
self.chat_control.conversation_id, inputs=[
self.chat_panel.chatbot, self.chat_control.conversation_id,
self._app.settings_state, self.chat_panel.chatbot,
self._reasoning_type, self._app.settings_state,
self._llm_type, self._reasoning_type,
self.state_chat, self._llm_type,
self._app.user_id, self.state_chat,
] self._app.user_id,
+ self._indices_input, ]
outputs=[ + self._indices_input,
self.chat_panel.chatbot, outputs=[
self.info_panel, self.chat_panel.chatbot,
self.plot_panel, self.info_panel,
self.state_plot_panel, self.plot_panel,
self.state_chat, self.state_plot_panel,
], self.state_chat,
concurrency_limit=20, ],
show_progress="minimal", concurrency_limit=20,
).then( show_progress="minimal",
fn=lambda: True, )
inputs=None, .then(
outputs=[self._preview_links], fn=lambda: True,
js=pdfview_js, inputs=None,
).success( outputs=[self._preview_links],
fn=self.check_and_suggest_name_conv, js=pdfview_js,
inputs=self.chat_panel.chatbot, )
outputs=[ .success(
self.chat_control.conversation_rn, fn=self.check_and_suggest_name_conv,
self._conversation_renamed, inputs=self.chat_panel.chatbot,
], outputs=[
).success( self.chat_control.conversation_rn,
self.chat_control.rename_conv, self._conversation_renamed,
inputs=[ ],
self.chat_control.conversation_id, )
self.chat_control.conversation_rn, .success(
self._conversation_renamed, self.chat_control.rename_conv,
self._app.user_id, inputs=[
], self.chat_control.conversation_id,
outputs=[ self.chat_control.conversation_rn,
self.chat_control.conversation, self._conversation_renamed,
self.chat_control.conversation, self._app.user_id,
self.chat_control.conversation_rn, ],
], outputs=[
show_progress="hidden", self.chat_control.conversation,
).then( self.chat_control.conversation,
fn=self.suggest_chat_conv, self.chat_control.conversation_rn,
inputs=[ ],
self._app.settings_state, show_progress="hidden",
self.chat_panel.chatbot, )
], )
outputs=[
self.state_follow_up, # chat suggestion toggle
self._suggestion_updated, if getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False):
], regen_event = regen_event.success(
show_progress="hidden", fn=self.suggest_chat_conv,
).success( inputs=[
self.chat_control.update_chat_suggestions, self._app.settings_state,
inputs=[ self.chat_panel.chatbot,
self.chat_control.conversation_id, ],
self.state_follow_up, outputs=[
self._suggestion_updated, self.state_follow_up,
self._app.user_id, self._suggestion_updated,
], ],
outputs=[ show_progress="hidden",
self.chat_control.conversation, ).success(
self.chat_control.conversation, self.chat_control.persist_chat_suggestions,
], inputs=[
show_progress="hidden", self.chat_control.conversation_id,
).then( self.state_follow_up,
self._suggestion_updated,
self._app.user_id,
],
outputs=[
self.chat_control.conversation,
self.chat_control.conversation,
],
show_progress="hidden",
)
# final data persist
regen_event = regen_event.then(
fn=self.persist_data_source, fn=self.persist_data_source,
inputs=[ inputs=[
self.chat_control.conversation_id, self.chat_control.conversation_id,
@ -963,7 +988,7 @@ class ChatPage(BasePage):
if ques_res := re.search(r"\[(.*?)\]", re.sub("\n", "", suggested_resp)): if ques_res := re.search(r"\[(.*?)\]", re.sub("\n", "", suggested_resp)):
ques_res_str = ques_res.group() ques_res_str = ques_res.group()
try: try:
suggested_ques = ast.literal_eval(ques_res_str) suggested_ques = json.loads(ques_res_str)
suggested_ques = [[x] for x in suggested_ques] suggested_ques = [[x] for x in suggested_ques]
updated = True updated = True
except Exception: except Exception:

View File

@ -321,7 +321,7 @@ class ConversationControl(BasePage):
gr.update(visible=False), gr.update(visible=False),
) )
def update_chat_suggestions( def persist_chat_suggestions(
self, conversation_id, new_suggestions, is_updated, user_id self, conversation_id, new_suggestions, is_updated, user_id
): ):
"""Update the conversation's chat suggestions""" """Update the conversation's chat suggestions"""