fix: optimize chat suggestion logic
This commit is contained in:
parent
ad34395d0b
commit
3ff6af8acf
|
@ -1,6 +1,6 @@
|
|||
import ast
|
||||
import asyncio
|
||||
import csv
|
||||
import json
|
||||
import re
|
||||
from copy import deepcopy
|
||||
from datetime import datetime
|
||||
|
@ -169,6 +169,7 @@ class ChatPage(BasePage):
|
|||
else:
|
||||
self.state_follow_up = self.chat_control.followup_suggestions
|
||||
|
||||
chat_event = (
|
||||
gr.on(
|
||||
triggers=[
|
||||
self.chat_panel.text_input.submit,
|
||||
|
@ -193,7 +194,8 @@ class ChatPage(BasePage):
|
|||
],
|
||||
concurrency_limit=20,
|
||||
show_progress="hidden",
|
||||
).success(
|
||||
)
|
||||
.success(
|
||||
fn=self.chat_fn,
|
||||
inputs=[
|
||||
self.chat_control.conversation_id,
|
||||
|
@ -214,19 +216,22 @@ class ChatPage(BasePage):
|
|||
],
|
||||
concurrency_limit=20,
|
||||
show_progress="minimal",
|
||||
).then(
|
||||
)
|
||||
.then(
|
||||
fn=lambda: True,
|
||||
inputs=None,
|
||||
outputs=[self._preview_links],
|
||||
js=pdfview_js,
|
||||
).success(
|
||||
)
|
||||
.success(
|
||||
fn=self.check_and_suggest_name_conv,
|
||||
inputs=self.chat_panel.chatbot,
|
||||
outputs=[
|
||||
self.chat_control.conversation_rn,
|
||||
self._conversation_renamed,
|
||||
],
|
||||
).success(
|
||||
)
|
||||
.success(
|
||||
self.chat_control.rename_conv,
|
||||
inputs=[
|
||||
self.chat_control.conversation_id,
|
||||
|
@ -240,7 +245,12 @@ class ChatPage(BasePage):
|
|||
self.chat_control.conversation_rn,
|
||||
],
|
||||
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,
|
||||
inputs=[
|
||||
self._app.settings_state,
|
||||
|
@ -252,7 +262,7 @@ class ChatPage(BasePage):
|
|||
],
|
||||
show_progress="hidden",
|
||||
).success(
|
||||
self.chat_control.update_chat_suggestions,
|
||||
self.chat_control.persist_chat_suggestions,
|
||||
inputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self.state_follow_up,
|
||||
|
@ -264,7 +274,10 @@ class ChatPage(BasePage):
|
|||
self.chat_control.conversation,
|
||||
],
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
)
|
||||
|
||||
# final data persist
|
||||
chat_event = chat_event.then(
|
||||
fn=self.persist_data_source,
|
||||
inputs=[
|
||||
self.chat_control.conversation_id,
|
||||
|
@ -284,6 +297,7 @@ class ChatPage(BasePage):
|
|||
concurrency_limit=20,
|
||||
)
|
||||
|
||||
regen_event = (
|
||||
self.chat_panel.regen_btn.click(
|
||||
fn=self.regen_fn,
|
||||
inputs=[
|
||||
|
@ -305,19 +319,22 @@ class ChatPage(BasePage):
|
|||
],
|
||||
concurrency_limit=20,
|
||||
show_progress="minimal",
|
||||
).then(
|
||||
)
|
||||
.then(
|
||||
fn=lambda: True,
|
||||
inputs=None,
|
||||
outputs=[self._preview_links],
|
||||
js=pdfview_js,
|
||||
).success(
|
||||
)
|
||||
.success(
|
||||
fn=self.check_and_suggest_name_conv,
|
||||
inputs=self.chat_panel.chatbot,
|
||||
outputs=[
|
||||
self.chat_control.conversation_rn,
|
||||
self._conversation_renamed,
|
||||
],
|
||||
).success(
|
||||
)
|
||||
.success(
|
||||
self.chat_control.rename_conv,
|
||||
inputs=[
|
||||
self.chat_control.conversation_id,
|
||||
|
@ -331,7 +348,12 @@ class ChatPage(BasePage):
|
|||
self.chat_control.conversation_rn,
|
||||
],
|
||||
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,
|
||||
inputs=[
|
||||
self._app.settings_state,
|
||||
|
@ -343,7 +365,7 @@ class ChatPage(BasePage):
|
|||
],
|
||||
show_progress="hidden",
|
||||
).success(
|
||||
self.chat_control.update_chat_suggestions,
|
||||
self.chat_control.persist_chat_suggestions,
|
||||
inputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self.state_follow_up,
|
||||
|
@ -355,7 +377,10 @@ class ChatPage(BasePage):
|
|||
self.chat_control.conversation,
|
||||
],
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
)
|
||||
|
||||
# final data persist
|
||||
regen_event = regen_event.then(
|
||||
fn=self.persist_data_source,
|
||||
inputs=[
|
||||
self.chat_control.conversation_id,
|
||||
|
@ -963,7 +988,7 @@ class ChatPage(BasePage):
|
|||
if ques_res := re.search(r"\[(.*?)\]", re.sub("\n", "", suggested_resp)):
|
||||
ques_res_str = ques_res.group()
|
||||
try:
|
||||
suggested_ques = ast.literal_eval(ques_res_str)
|
||||
suggested_ques = json.loads(ques_res_str)
|
||||
suggested_ques = [[x] for x in suggested_ques]
|
||||
updated = True
|
||||
except Exception:
|
||||
|
|
|
@ -321,7 +321,7 @@ class ConversationControl(BasePage):
|
|||
gr.update(visible=False),
|
||||
)
|
||||
|
||||
def update_chat_suggestions(
|
||||
def persist_chat_suggestions(
|
||||
self, conversation_id, new_suggestions, is_updated, user_id
|
||||
):
|
||||
"""Update the conversation's chat suggestions"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user