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,6 +169,7 @@ class ChatPage(BasePage):
else: else:
self.state_follow_up = self.chat_control.followup_suggestions self.state_follow_up = self.chat_control.followup_suggestions
chat_event = (
gr.on( gr.on(
triggers=[ triggers=[
self.chat_panel.text_input.submit, self.chat_panel.text_input.submit,
@ -193,7 +194,8 @@ class ChatPage(BasePage):
], ],
concurrency_limit=20, concurrency_limit=20,
show_progress="hidden", show_progress="hidden",
).success( )
.success(
fn=self.chat_fn, fn=self.chat_fn,
inputs=[ inputs=[
self.chat_control.conversation_id, self.chat_control.conversation_id,
@ -214,19 +216,22 @@ class ChatPage(BasePage):
], ],
concurrency_limit=20, concurrency_limit=20,
show_progress="minimal", show_progress="minimal",
).then( )
.then(
fn=lambda: True, fn=lambda: True,
inputs=None, inputs=None,
outputs=[self._preview_links], outputs=[self._preview_links],
js=pdfview_js, js=pdfview_js,
).success( )
.success(
fn=self.check_and_suggest_name_conv, fn=self.check_and_suggest_name_conv,
inputs=self.chat_panel.chatbot, inputs=self.chat_panel.chatbot,
outputs=[ outputs=[
self.chat_control.conversation_rn, self.chat_control.conversation_rn,
self._conversation_renamed, self._conversation_renamed,
], ],
).success( )
.success(
self.chat_control.rename_conv, self.chat_control.rename_conv,
inputs=[ inputs=[
self.chat_control.conversation_id, self.chat_control.conversation_id,
@ -240,7 +245,12 @@ class ChatPage(BasePage):
self.chat_control.conversation_rn, self.chat_control.conversation_rn,
], ],
show_progress="hidden", show_progress="hidden",
).then( )
)
# chat suggestion toggle
if getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False):
chat_event = chat_event.success(
fn=self.suggest_chat_conv, fn=self.suggest_chat_conv,
inputs=[ inputs=[
self._app.settings_state, self._app.settings_state,
@ -252,7 +262,7 @@ class ChatPage(BasePage):
], ],
show_progress="hidden", show_progress="hidden",
).success( ).success(
self.chat_control.update_chat_suggestions, self.chat_control.persist_chat_suggestions,
inputs=[ inputs=[
self.chat_control.conversation_id, self.chat_control.conversation_id,
self.state_follow_up, self.state_follow_up,
@ -264,7 +274,10 @@ class ChatPage(BasePage):
self.chat_control.conversation, self.chat_control.conversation,
], ],
show_progress="hidden", show_progress="hidden",
).then( )
# 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,6 +297,7 @@ class ChatPage(BasePage):
concurrency_limit=20, concurrency_limit=20,
) )
regen_event = (
self.chat_panel.regen_btn.click( self.chat_panel.regen_btn.click(
fn=self.regen_fn, fn=self.regen_fn,
inputs=[ inputs=[
@ -305,19 +319,22 @@ class ChatPage(BasePage):
], ],
concurrency_limit=20, concurrency_limit=20,
show_progress="minimal", show_progress="minimal",
).then( )
.then(
fn=lambda: True, fn=lambda: True,
inputs=None, inputs=None,
outputs=[self._preview_links], outputs=[self._preview_links],
js=pdfview_js, js=pdfview_js,
).success( )
.success(
fn=self.check_and_suggest_name_conv, fn=self.check_and_suggest_name_conv,
inputs=self.chat_panel.chatbot, inputs=self.chat_panel.chatbot,
outputs=[ outputs=[
self.chat_control.conversation_rn, self.chat_control.conversation_rn,
self._conversation_renamed, self._conversation_renamed,
], ],
).success( )
.success(
self.chat_control.rename_conv, self.chat_control.rename_conv,
inputs=[ inputs=[
self.chat_control.conversation_id, self.chat_control.conversation_id,
@ -331,7 +348,12 @@ class ChatPage(BasePage):
self.chat_control.conversation_rn, self.chat_control.conversation_rn,
], ],
show_progress="hidden", show_progress="hidden",
).then( )
)
# chat suggestion toggle
if getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False):
regen_event = regen_event.success(
fn=self.suggest_chat_conv, fn=self.suggest_chat_conv,
inputs=[ inputs=[
self._app.settings_state, self._app.settings_state,
@ -343,7 +365,7 @@ class ChatPage(BasePage):
], ],
show_progress="hidden", show_progress="hidden",
).success( ).success(
self.chat_control.update_chat_suggestions, self.chat_control.persist_chat_suggestions,
inputs=[ inputs=[
self.chat_control.conversation_id, self.chat_control.conversation_id,
self.state_follow_up, self.state_follow_up,
@ -355,7 +377,10 @@ class ChatPage(BasePage):
self.chat_control.conversation, self.chat_control.conversation,
], ],
show_progress="hidden", show_progress="hidden",
).then( )
# 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"""