diff --git a/flowsettings.py b/flowsettings.py index e248fac9..4fe8b4ab 100644 --- a/flowsettings.py +++ b/flowsettings.py @@ -4,6 +4,7 @@ from pathlib import Path from decouple import config +from ktem.utils.lang import SUPPORTED_LANGUAGE_MAP from theflow.settings.default import * # noqa cur_frame = currentframe() @@ -284,7 +285,7 @@ "lang": { "name": "Language", "value": "en", - "choices": [("English", "en"), ("Japanese", "ja"), ("Vietnamese", "vi")], + "choices": [(lang, code) for code, lang in SUPPORTED_LANGUAGE_MAP.items()], "component": "dropdown", }, "max_context_length": { diff --git a/libs/ktem/ktem/pages/chat/__init__.py b/libs/ktem/ktem/pages/chat/__init__.py index 326a0009..86ed46fa 100644 --- a/libs/ktem/ktem/pages/chat/__init__.py +++ b/libs/ktem/ktem/pages/chat/__init__.py @@ -109,7 +109,9 @@ def __init__(self, app): self._preview_links = gr.State(value=None) self._reasoning_type = gr.State(value=None) 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) def on_building_ui(self): @@ -118,7 +120,6 @@ def on_building_ui(self): self.state_retrieval_history = gr.State([]) self.state_plot_history = gr.State([]) self.state_plot_panel = gr.State(None) - self.state_follow_up = gr.State(None) self.first_selector_choices = gr.State(None) with gr.Column(scale=1, elem_id="conv-settings-panel") as self.conv_column: @@ -189,6 +190,7 @@ def on_building_ui(self): gr.HTML("Reasoning method") gr.HTML("Model") gr.HTML("Language") + gr.HTML("Suggestion") with gr.Row(): reasoning_type_values = [ @@ -223,6 +225,12 @@ def on_building_ui(self): container=False, show_label=False, ) + self.use_chat_suggestion = gr.Checkbox( + label="Chat suggestion", + container=False, + elem_id="use-suggestion-checkbox", + ) + self.citation = gr.Dropdown( choices=[ (DEFAULT_SETTING, DEFAULT_SETTING), @@ -263,10 +271,8 @@ def _json_to_plot(self, json_dict: dict | None): return plot def on_register_events(self): - if getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False): - self.state_follow_up = self.chat_control.chat_suggestion.example - else: - self.state_follow_up = self.chat_control.followup_suggestions + self.followup_questions = self.chat_control.chat_suggestion.examples + self.followup_questions_ui = self.chat_control.chat_suggestion.accordion chat_event = ( gr.on( @@ -280,7 +286,6 @@ def on_register_events(self): self._app.user_id, self.chat_control.conversation_id, self.chat_control.conversation_rn, - self.state_follow_up, self.first_selector_choices, ], outputs=[ @@ -289,7 +294,6 @@ def on_register_events(self): self.chat_control.conversation_id, self.chat_control.conversation, self.chat_control.conversation_rn, - self.state_follow_up, # file selector from the first index self._indices_input[0], self._indices_input[1], @@ -354,28 +358,29 @@ def on_register_events(self): ) # 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, - self.chat_panel.chatbot, - ], - outputs=[ - self.state_follow_up, - self._suggestion_updated, - ], - show_progress="hidden", - ).success( - self.chat_control.persist_chat_suggestions, - inputs=[ - self.chat_control.conversation_id, - self.state_follow_up, - self._suggestion_updated, - self._app.user_id, - ], - show_progress="hidden", - ) + chat_event = chat_event.success( + fn=self.suggest_chat_conv, + inputs=[ + self._app.settings_state, + self.chat_panel.chatbot, + self._use_suggestion, + ], + outputs=[ + self.followup_questions_ui, + self.followup_questions, + ], + show_progress="hidden", + ) + # .success( + # self.chat_control.persist_chat_suggestions, + # inputs=[ + # self.chat_control.conversation_id, + # self.followup_questions, + # self._use_suggestion, + # self._app.user_id, + # ], + # show_progress="hidden", + # ) # final data persist chat_event = chat_event.then( @@ -428,7 +433,7 @@ def on_register_events(self): self.chat_control.conversation, self.chat_control.conversation_rn, self.chat_panel.chatbot, - self.state_follow_up, + self.followup_questions, self.info_panel, self.state_plot_panel, self.state_retrieval_history, @@ -466,7 +471,7 @@ def on_register_events(self): self.chat_control.conversation, self.chat_control.conversation_rn, self.chat_panel.chatbot, - self.state_follow_up, + self.followup_questions, self.info_panel, self.state_plot_panel, self.state_retrieval_history, @@ -518,7 +523,7 @@ def on_register_events(self): self.chat_control.conversation, self.chat_control.conversation_rn, self.chat_panel.chatbot, - self.state_follow_up, + self.followup_questions, self.info_panel, self.state_plot_panel, self.state_retrieval_history, @@ -602,17 +607,27 @@ def on_register_events(self): outputs=[self.use_mindmap, self.use_mindmap_check], 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( lambda: gr.update(visible=False), outputs=self.plot_panel, ) - if getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False): - self.state_follow_up.select( - self.chat_control.chat_suggestion.select_example, - outputs=[self.chat_panel.text_input], - show_progress="hidden", - ) + self.followup_questions.select( + self.chat_control.chat_suggestion.select_example, + outputs=[self.chat_panel.text_input], + show_progress="hidden", + ).then( + fn=None, + inputs=None, + outputs=None, + js=chat_input_focus_js, + ) def submit_msg( self, @@ -621,7 +636,6 @@ def submit_msg( user_id, conv_id, conv_name, - chat_suggest, first_selector_choices, ): """Submit a message to the chatbot""" @@ -660,20 +674,13 @@ def submit_msg( with Session(engine) as session: statement = select(Conversation).where(Conversation.id == id_) name = session.exec(statement).one().name - suggestion = ( - session.exec(statement) - .one() - .data_source.get("chat_suggestions", []) - ) new_conv_id = id_ conv_update = update new_conv_name = name - new_chat_suggestion = suggestion else: new_conv_id = conv_id conv_update = gr.update() new_conv_name = conv_name - new_chat_suggestion = chat_suggest return [ {}, @@ -681,7 +688,6 @@ def submit_msg( new_conv_id, conv_update, new_conv_name, - new_chat_suggestion, ] + selector_output def toggle_delete(self, conv_id): @@ -1038,24 +1044,26 @@ def check_and_suggest_name_conv(self, chat_history): return new_name, renamed - def suggest_chat_conv(self, settings, chat_history): - suggest_pipeline = SuggestFollowupQuesPipeline() - suggest_pipeline.lang = SUPPORTED_LANGUAGE_MAP.get( - settings["reasoning.lang"], "English" - ) + def suggest_chat_conv(self, settings, chat_history, use_suggestion): + if use_suggestion: + suggest_pipeline = SuggestFollowupQuesPipeline() + suggest_pipeline.lang = SUPPORTED_LANGUAGE_MAP.get( + settings["reasoning.lang"], "English" + ) + 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_questions = json.loads(ques_res_str) + suggested_questions = [[x] for x in suggested_questions] + except Exception: + pass + + return gr.update(visible=True), suggested_questions - updated = False - - suggested_ques = [] - 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() diff --git a/libs/ktem/ktem/pages/chat/chat_suggestion.py b/libs/ktem/ktem/pages/chat/chat_suggestion.py index 1f60284a..2bfe03c2 100644 --- a/libs/ktem/ktem/pages/chat/chat_suggestion.py +++ b/libs/ktem/ktem/pages/chat/chat_suggestion.py @@ -9,18 +9,29 @@ def __init__(self, app): self.on_building_ui() def on_building_ui(self): - chat_samples = getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION_SAMPLES", []) - chat_samples = [[each] for each in chat_samples] - with gr.Accordion(label="Chat Suggestion", open=False) as self.accordion: - self.example = gr.DataFrame( - value=chat_samples, + chat_samples = getattr( + flowsettings, + "KH_FEATURE_CHAT_SUGGESTION_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"], interactive=False, wrap=True, ) def as_gradio_component(self): - return self.example + return self.examples def select_example(self, ev: gr.SelectData): return {"text": ev.value} diff --git a/libs/ktem/ktem/pages/chat/control.py b/libs/ktem/ktem/pages/chat/control.py index ec11ef26..44b8835e 100644 --- a/libs/ktem/ktem/pages/chat/control.py +++ b/libs/ktem/ktem/pages/chat/control.py @@ -139,9 +139,7 @@ def on_building_ui(self): visible=False, ) - self.followup_suggestions = gr.State([]) - if getattr(flowsettings, "KH_FEATURE_CHAT_SUGGESTION", False): - self.chat_suggestion = ChatSuggestion(self._app) + self.chat_suggestion = ChatSuggestion(self._app) def load_chat_history(self, user_id): """Reload chat history""" @@ -259,7 +257,6 @@ def select_conv(self, conversation_id, user_id): selected = {} chats = result.data_source.get("messages", []) - chat_suggestions = result.data_source.get("chat_suggestions", []) retrieval_history: list[str] = result.data_source.get( diff --git a/libs/ktem/ktem/utils/lang.py b/libs/ktem/ktem/utils/lang.py index af6601d4..2a394076 100644 --- a/libs/ktem/ktem/utils/lang.py +++ b/libs/ktem/ktem/utils/lang.py @@ -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", +}