feat: add quick setting for follow up chat suggestions & add more common languages (#556) bump:patch
* feat: add chat suggestion quick setting * feat: add more common languages * fix: minor fixes
This commit is contained in:
parent
6650b15c64
commit
a0c9a6e8de
|
@ -4,6 +4,7 @@ from inspect import currentframe, getframeinfo
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from decouple import config
|
from decouple import config
|
||||||
|
from ktem.utils.lang import SUPPORTED_LANGUAGE_MAP
|
||||||
from theflow.settings.default import * # noqa
|
from theflow.settings.default import * # noqa
|
||||||
|
|
||||||
cur_frame = currentframe()
|
cur_frame = currentframe()
|
||||||
|
@ -284,7 +285,7 @@ SETTINGS_REASONING = {
|
||||||
"lang": {
|
"lang": {
|
||||||
"name": "Language",
|
"name": "Language",
|
||||||
"value": "en",
|
"value": "en",
|
||||||
"choices": [("English", "en"), ("Japanese", "ja"), ("Vietnamese", "vi")],
|
"choices": [(lang, code) for code, lang in SUPPORTED_LANGUAGE_MAP.items()],
|
||||||
"component": "dropdown",
|
"component": "dropdown",
|
||||||
},
|
},
|
||||||
"max_context_length": {
|
"max_context_length": {
|
||||||
|
|
|
@ -109,7 +109,9 @@ class ChatPage(BasePage):
|
||||||
self._preview_links = gr.State(value=None)
|
self._preview_links = gr.State(value=None)
|
||||||
self._reasoning_type = gr.State(value=None)
|
self._reasoning_type = gr.State(value=None)
|
||||||
self._conversation_renamed = gr.State(value=False)
|
self._conversation_renamed = gr.State(value=False)
|
||||||
self._suggestion_updated = gr.State(value=False)
|
self._use_suggestion = gr.State(
|
||||||
|
value=getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False)
|
||||||
|
)
|
||||||
self._info_panel_expanded = gr.State(value=True)
|
self._info_panel_expanded = gr.State(value=True)
|
||||||
|
|
||||||
def on_building_ui(self):
|
def on_building_ui(self):
|
||||||
|
@ -118,7 +120,6 @@ class ChatPage(BasePage):
|
||||||
self.state_retrieval_history = gr.State([])
|
self.state_retrieval_history = gr.State([])
|
||||||
self.state_plot_history = gr.State([])
|
self.state_plot_history = gr.State([])
|
||||||
self.state_plot_panel = gr.State(None)
|
self.state_plot_panel = gr.State(None)
|
||||||
self.state_follow_up = gr.State(None)
|
|
||||||
self.first_selector_choices = gr.State(None)
|
self.first_selector_choices = gr.State(None)
|
||||||
|
|
||||||
with gr.Column(scale=1, elem_id="conv-settings-panel") as self.conv_column:
|
with gr.Column(scale=1, elem_id="conv-settings-panel") as self.conv_column:
|
||||||
|
@ -189,6 +190,7 @@ class ChatPage(BasePage):
|
||||||
gr.HTML("Reasoning method")
|
gr.HTML("Reasoning method")
|
||||||
gr.HTML("Model")
|
gr.HTML("Model")
|
||||||
gr.HTML("Language")
|
gr.HTML("Language")
|
||||||
|
gr.HTML("Suggestion")
|
||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
reasoning_type_values = [
|
reasoning_type_values = [
|
||||||
|
@ -223,6 +225,12 @@ class ChatPage(BasePage):
|
||||||
container=False,
|
container=False,
|
||||||
show_label=False,
|
show_label=False,
|
||||||
)
|
)
|
||||||
|
self.use_chat_suggestion = gr.Checkbox(
|
||||||
|
label="Chat suggestion",
|
||||||
|
container=False,
|
||||||
|
elem_id="use-suggestion-checkbox",
|
||||||
|
)
|
||||||
|
|
||||||
self.citation = gr.Dropdown(
|
self.citation = gr.Dropdown(
|
||||||
choices=[
|
choices=[
|
||||||
(DEFAULT_SETTING, DEFAULT_SETTING),
|
(DEFAULT_SETTING, DEFAULT_SETTING),
|
||||||
|
@ -263,10 +271,8 @@ class ChatPage(BasePage):
|
||||||
return plot
|
return plot
|
||||||
|
|
||||||
def on_register_events(self):
|
def on_register_events(self):
|
||||||
if getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False):
|
self.followup_questions = self.chat_control.chat_suggestion.examples
|
||||||
self.state_follow_up = self.chat_control.chat_suggestion.example
|
self.followup_questions_ui = self.chat_control.chat_suggestion.accordion
|
||||||
else:
|
|
||||||
self.state_follow_up = self.chat_control.followup_suggestions
|
|
||||||
|
|
||||||
chat_event = (
|
chat_event = (
|
||||||
gr.on(
|
gr.on(
|
||||||
|
@ -280,7 +286,6 @@ class ChatPage(BasePage):
|
||||||
self._app.user_id,
|
self._app.user_id,
|
||||||
self.chat_control.conversation_id,
|
self.chat_control.conversation_id,
|
||||||
self.chat_control.conversation_rn,
|
self.chat_control.conversation_rn,
|
||||||
self.state_follow_up,
|
|
||||||
self.first_selector_choices,
|
self.first_selector_choices,
|
||||||
],
|
],
|
||||||
outputs=[
|
outputs=[
|
||||||
|
@ -289,7 +294,6 @@ class ChatPage(BasePage):
|
||||||
self.chat_control.conversation_id,
|
self.chat_control.conversation_id,
|
||||||
self.chat_control.conversation,
|
self.chat_control.conversation,
|
||||||
self.chat_control.conversation_rn,
|
self.chat_control.conversation_rn,
|
||||||
self.state_follow_up,
|
|
||||||
# file selector from the first index
|
# file selector from the first index
|
||||||
self._indices_input[0],
|
self._indices_input[0],
|
||||||
self._indices_input[1],
|
self._indices_input[1],
|
||||||
|
@ -354,28 +358,29 @@ class ChatPage(BasePage):
|
||||||
)
|
)
|
||||||
|
|
||||||
# chat suggestion toggle
|
# chat suggestion toggle
|
||||||
if getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False):
|
chat_event = chat_event.success(
|
||||||
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,
|
self.chat_panel.chatbot,
|
||||||
self.chat_panel.chatbot,
|
self._use_suggestion,
|
||||||
],
|
],
|
||||||
outputs=[
|
outputs=[
|
||||||
self.state_follow_up,
|
self.followup_questions_ui,
|
||||||
self._suggestion_updated,
|
self.followup_questions,
|
||||||
],
|
],
|
||||||
show_progress="hidden",
|
show_progress="hidden",
|
||||||
).success(
|
)
|
||||||
self.chat_control.persist_chat_suggestions,
|
# .success(
|
||||||
inputs=[
|
# self.chat_control.persist_chat_suggestions,
|
||||||
self.chat_control.conversation_id,
|
# inputs=[
|
||||||
self.state_follow_up,
|
# self.chat_control.conversation_id,
|
||||||
self._suggestion_updated,
|
# self.followup_questions,
|
||||||
self._app.user_id,
|
# self._use_suggestion,
|
||||||
],
|
# self._app.user_id,
|
||||||
show_progress="hidden",
|
# ],
|
||||||
)
|
# show_progress="hidden",
|
||||||
|
# )
|
||||||
|
|
||||||
# final data persist
|
# final data persist
|
||||||
chat_event = chat_event.then(
|
chat_event = chat_event.then(
|
||||||
|
@ -428,7 +433,7 @@ class ChatPage(BasePage):
|
||||||
self.chat_control.conversation,
|
self.chat_control.conversation,
|
||||||
self.chat_control.conversation_rn,
|
self.chat_control.conversation_rn,
|
||||||
self.chat_panel.chatbot,
|
self.chat_panel.chatbot,
|
||||||
self.state_follow_up,
|
self.followup_questions,
|
||||||
self.info_panel,
|
self.info_panel,
|
||||||
self.state_plot_panel,
|
self.state_plot_panel,
|
||||||
self.state_retrieval_history,
|
self.state_retrieval_history,
|
||||||
|
@ -466,7 +471,7 @@ class ChatPage(BasePage):
|
||||||
self.chat_control.conversation,
|
self.chat_control.conversation,
|
||||||
self.chat_control.conversation_rn,
|
self.chat_control.conversation_rn,
|
||||||
self.chat_panel.chatbot,
|
self.chat_panel.chatbot,
|
||||||
self.state_follow_up,
|
self.followup_questions,
|
||||||
self.info_panel,
|
self.info_panel,
|
||||||
self.state_plot_panel,
|
self.state_plot_panel,
|
||||||
self.state_retrieval_history,
|
self.state_retrieval_history,
|
||||||
|
@ -518,7 +523,7 @@ class ChatPage(BasePage):
|
||||||
self.chat_control.conversation,
|
self.chat_control.conversation,
|
||||||
self.chat_control.conversation_rn,
|
self.chat_control.conversation_rn,
|
||||||
self.chat_panel.chatbot,
|
self.chat_panel.chatbot,
|
||||||
self.state_follow_up,
|
self.followup_questions,
|
||||||
self.info_panel,
|
self.info_panel,
|
||||||
self.state_plot_panel,
|
self.state_plot_panel,
|
||||||
self.state_retrieval_history,
|
self.state_retrieval_history,
|
||||||
|
@ -602,17 +607,27 @@ class ChatPage(BasePage):
|
||||||
outputs=[self.use_mindmap, self.use_mindmap_check],
|
outputs=[self.use_mindmap, self.use_mindmap_check],
|
||||||
show_progress="hidden",
|
show_progress="hidden",
|
||||||
)
|
)
|
||||||
|
self.use_chat_suggestion.change(
|
||||||
|
lambda x: (x, gr.update(visible=x)),
|
||||||
|
inputs=[self.use_chat_suggestion],
|
||||||
|
outputs=[self._use_suggestion, self.followup_questions_ui],
|
||||||
|
show_progress="hidden",
|
||||||
|
)
|
||||||
self.chat_control.conversation_id.change(
|
self.chat_control.conversation_id.change(
|
||||||
lambda: gr.update(visible=False),
|
lambda: gr.update(visible=False),
|
||||||
outputs=self.plot_panel,
|
outputs=self.plot_panel,
|
||||||
)
|
)
|
||||||
|
|
||||||
if getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False):
|
self.followup_questions.select(
|
||||||
self.state_follow_up.select(
|
self.chat_control.chat_suggestion.select_example,
|
||||||
self.chat_control.chat_suggestion.select_example,
|
outputs=[self.chat_panel.text_input],
|
||||||
outputs=[self.chat_panel.text_input],
|
show_progress="hidden",
|
||||||
show_progress="hidden",
|
).then(
|
||||||
)
|
fn=None,
|
||||||
|
inputs=None,
|
||||||
|
outputs=None,
|
||||||
|
js=chat_input_focus_js,
|
||||||
|
)
|
||||||
|
|
||||||
def submit_msg(
|
def submit_msg(
|
||||||
self,
|
self,
|
||||||
|
@ -621,7 +636,6 @@ class ChatPage(BasePage):
|
||||||
user_id,
|
user_id,
|
||||||
conv_id,
|
conv_id,
|
||||||
conv_name,
|
conv_name,
|
||||||
chat_suggest,
|
|
||||||
first_selector_choices,
|
first_selector_choices,
|
||||||
):
|
):
|
||||||
"""Submit a message to the chatbot"""
|
"""Submit a message to the chatbot"""
|
||||||
|
@ -660,20 +674,13 @@ class ChatPage(BasePage):
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
statement = select(Conversation).where(Conversation.id == id_)
|
statement = select(Conversation).where(Conversation.id == id_)
|
||||||
name = session.exec(statement).one().name
|
name = session.exec(statement).one().name
|
||||||
suggestion = (
|
|
||||||
session.exec(statement)
|
|
||||||
.one()
|
|
||||||
.data_source.get("chat_suggestions", [])
|
|
||||||
)
|
|
||||||
new_conv_id = id_
|
new_conv_id = id_
|
||||||
conv_update = update
|
conv_update = update
|
||||||
new_conv_name = name
|
new_conv_name = name
|
||||||
new_chat_suggestion = suggestion
|
|
||||||
else:
|
else:
|
||||||
new_conv_id = conv_id
|
new_conv_id = conv_id
|
||||||
conv_update = gr.update()
|
conv_update = gr.update()
|
||||||
new_conv_name = conv_name
|
new_conv_name = conv_name
|
||||||
new_chat_suggestion = chat_suggest
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{},
|
{},
|
||||||
|
@ -681,7 +688,6 @@ class ChatPage(BasePage):
|
||||||
new_conv_id,
|
new_conv_id,
|
||||||
conv_update,
|
conv_update,
|
||||||
new_conv_name,
|
new_conv_name,
|
||||||
new_chat_suggestion,
|
|
||||||
] + selector_output
|
] + selector_output
|
||||||
|
|
||||||
def toggle_delete(self, conv_id):
|
def toggle_delete(self, conv_id):
|
||||||
|
@ -1038,24 +1044,26 @@ class ChatPage(BasePage):
|
||||||
|
|
||||||
return new_name, renamed
|
return new_name, renamed
|
||||||
|
|
||||||
def suggest_chat_conv(self, settings, chat_history):
|
def suggest_chat_conv(self, settings, chat_history, use_suggestion):
|
||||||
suggest_pipeline = SuggestFollowupQuesPipeline()
|
if use_suggestion:
|
||||||
suggest_pipeline.lang = SUPPORTED_LANGUAGE_MAP.get(
|
suggest_pipeline = SuggestFollowupQuesPipeline()
|
||||||
settings["reasoning.lang"], "English"
|
suggest_pipeline.lang = SUPPORTED_LANGUAGE_MAP.get(
|
||||||
)
|
settings["reasoning.lang"], "English"
|
||||||
|
)
|
||||||
|
suggested_questions = []
|
||||||
|
|
||||||
updated = False
|
if len(chat_history) >= 1:
|
||||||
|
suggested_resp = suggest_pipeline(chat_history).text
|
||||||
|
if ques_res := re.search(
|
||||||
|
r"\[(.*?)\]", re.sub("\n", "", suggested_resp)
|
||||||
|
):
|
||||||
|
ques_res_str = ques_res.group()
|
||||||
|
try:
|
||||||
|
suggested_questions = json.loads(ques_res_str)
|
||||||
|
suggested_questions = [[x] for x in suggested_questions]
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
suggested_ques = []
|
return gr.update(visible=True), suggested_questions
|
||||||
if len(chat_history) >= 1:
|
|
||||||
suggested_resp = suggest_pipeline(chat_history).text
|
|
||||||
if ques_res := re.search(r"\[(.*?)\]", re.sub("\n", "", suggested_resp)):
|
|
||||||
ques_res_str = ques_res.group()
|
|
||||||
try:
|
|
||||||
suggested_ques = json.loads(ques_res_str)
|
|
||||||
suggested_ques = [[x] for x in suggested_ques]
|
|
||||||
updated = True
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return suggested_ques, updated
|
return gr.update(visible=False), gr.update()
|
||||||
|
|
|
@ -9,18 +9,29 @@ class ChatSuggestion(BasePage):
|
||||||
self.on_building_ui()
|
self.on_building_ui()
|
||||||
|
|
||||||
def on_building_ui(self):
|
def on_building_ui(self):
|
||||||
chat_samples = getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION_SAMPLES", [])
|
chat_samples = getattr(
|
||||||
chat_samples = [[each] for each in chat_samples]
|
flowsettings,
|
||||||
with gr.Accordion(label="Chat Suggestion", open=False) as self.accordion:
|
"KH_FEATURE_CHAT_SUGGESTION_SAMPLES",
|
||||||
self.example = gr.DataFrame(
|
[
|
||||||
value=chat_samples,
|
"Summary this document",
|
||||||
|
"Generate a FAQ for this document",
|
||||||
|
"Identify the main highlights in this text",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
self.chat_samples = [[each] for each in chat_samples]
|
||||||
|
with gr.Accordion(
|
||||||
|
label="Chat Suggestion",
|
||||||
|
visible=getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False),
|
||||||
|
) as self.accordion:
|
||||||
|
self.examples = gr.DataFrame(
|
||||||
|
value=self.chat_samples,
|
||||||
headers=["Next Question"],
|
headers=["Next Question"],
|
||||||
interactive=False,
|
interactive=False,
|
||||||
wrap=True,
|
wrap=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def as_gradio_component(self):
|
def as_gradio_component(self):
|
||||||
return self.example
|
return self.examples
|
||||||
|
|
||||||
def select_example(self, ev: gr.SelectData):
|
def select_example(self, ev: gr.SelectData):
|
||||||
return {"text": ev.value}
|
return {"text": ev.value}
|
||||||
|
|
|
@ -139,9 +139,7 @@ class ConversationControl(BasePage):
|
||||||
visible=False,
|
visible=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.followup_suggestions = gr.State([])
|
self.chat_suggestion = ChatSuggestion(self._app)
|
||||||
if getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False):
|
|
||||||
self.chat_suggestion = ChatSuggestion(self._app)
|
|
||||||
|
|
||||||
def load_chat_history(self, user_id):
|
def load_chat_history(self, user_id):
|
||||||
"""Reload chat history"""
|
"""Reload chat history"""
|
||||||
|
@ -259,7 +257,6 @@ class ConversationControl(BasePage):
|
||||||
selected = {}
|
selected = {}
|
||||||
|
|
||||||
chats = result.data_source.get("messages", [])
|
chats = result.data_source.get("messages", [])
|
||||||
|
|
||||||
chat_suggestions = result.data_source.get("chat_suggestions", [])
|
chat_suggestions = result.data_source.get("chat_suggestions", [])
|
||||||
|
|
||||||
retrieval_history: list[str] = result.data_source.get(
|
retrieval_history: list[str] = result.data_source.get(
|
||||||
|
|
|
@ -1 +1,33 @@
|
||||||
SUPPORTED_LANGUAGE_MAP = {"en": "English", "ja": "Japanese", "vi": "Vietnamese"}
|
SUPPORTED_LANGUAGE_MAP = {
|
||||||
|
"en": "English",
|
||||||
|
"ja": "Japanese",
|
||||||
|
"vi": "Vietnamese",
|
||||||
|
"es": "Spanish",
|
||||||
|
"fr": "French",
|
||||||
|
"de": "German",
|
||||||
|
"zh": "Chinese",
|
||||||
|
"ru": "Russian",
|
||||||
|
"ar": "Arabic",
|
||||||
|
"pt": "Portuguese",
|
||||||
|
"hi": "Hindi",
|
||||||
|
"bn": "Bengali",
|
||||||
|
"pa": "Punjabi",
|
||||||
|
"ko": "Korean",
|
||||||
|
"it": "Italian",
|
||||||
|
"nl": "Dutch",
|
||||||
|
"tr": "Turkish",
|
||||||
|
"pl": "Polish",
|
||||||
|
"uk": "Ukrainian",
|
||||||
|
"ro": "Romanian",
|
||||||
|
"el": "Greek",
|
||||||
|
"hu": "Hungarian",
|
||||||
|
"sv": "Swedish",
|
||||||
|
"cs": "Czech",
|
||||||
|
"fi": "Finnish",
|
||||||
|
"da": "Danish",
|
||||||
|
"no": "Norwegian",
|
||||||
|
"he": "Hebrew",
|
||||||
|
"th": "Thai",
|
||||||
|
"id": "Indonesian",
|
||||||
|
"ms": "Malay",
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user