Skip to content

Commit

Permalink
feat: add quick setting for follow up chat suggestions & add more com…
Browse files Browse the repository at this point in the history
…mon languages (#556) bump:patch

* feat: add chat suggestion quick setting

* feat: add more common languages

* fix: minor fixes
  • Loading branch information
taprosoft authored Dec 7, 2024
1 parent 6650b15 commit a0c9a6e
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 80 deletions.
3 changes: 2 additions & 1 deletion flowsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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": {
Expand Down
144 changes: 76 additions & 68 deletions libs/ktem/ktem/pages/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand All @@ -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=[
Expand All @@ -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],
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -621,7 +636,6 @@ def submit_msg(
user_id,
conv_id,
conv_name,
chat_suggest,
first_selector_choices,
):
"""Submit a message to the chatbot"""
Expand Down Expand Up @@ -660,28 +674,20 @@ 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 [
{},
chat_history,
new_conv_id,
conv_update,
new_conv_name,
new_chat_suggestion,
] + selector_output

def toggle_delete(self, conv_id):
Expand Down Expand Up @@ -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()
23 changes: 17 additions & 6 deletions libs/ktem/ktem/pages/chat/chat_suggestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
5 changes: 1 addition & 4 deletions libs/ktem/ktem/pages/chat/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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(
Expand Down
34 changes: 33 additions & 1 deletion libs/ktem/ktem/utils/lang.py
Original file line number Diff line number Diff line change
@@ -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",
}

0 comments on commit a0c9a6e

Please sign in to comment.