mirror of
https://github.com/Cinnamon/kotaemon.git
synced 2025-06-26 23:19:56 +00:00
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 decouple import config
|
||||
from ktem.utils.lang import SUPPORTED_LANGUAGE_MAP
|
||||
from theflow.settings.default import * # noqa
|
||||
|
||||
cur_frame = currentframe()
|
||||
@ -284,7 +285,7 @@ SETTINGS_REASONING = {
|
||||
"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": {
|
||||
|
@ -109,7 +109,9 @@ class ChatPage(BasePage):
|
||||
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 @@ class ChatPage(BasePage):
|
||||
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 @@ class ChatPage(BasePage):
|
||||
gr.HTML("Reasoning method")
|
||||
gr.HTML("Model")
|
||||
gr.HTML("Language")
|
||||
gr.HTML("Suggestion")
|
||||
|
||||
with gr.Row():
|
||||
reasoning_type_values = [
|
||||
@ -223,6 +225,12 @@ class ChatPage(BasePage):
|
||||
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 @@ class ChatPage(BasePage):
|
||||
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 @@ class ChatPage(BasePage):
|
||||
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 @@ class ChatPage(BasePage):
|
||||
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 @@ class ChatPage(BasePage):
|
||||
)
|
||||
|
||||
# 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 @@ class ChatPage(BasePage):
|
||||
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 @@ class ChatPage(BasePage):
|
||||
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 @@ class ChatPage(BasePage):
|
||||
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 @@ class ChatPage(BasePage):
|
||||
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 @@ class ChatPage(BasePage):
|
||||
user_id,
|
||||
conv_id,
|
||||
conv_name,
|
||||
chat_suggest,
|
||||
first_selector_choices,
|
||||
):
|
||||
"""Submit a message to the chatbot"""
|
||||
@ -660,20 +674,13 @@ class ChatPage(BasePage):
|
||||
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 @@ class ChatPage(BasePage):
|
||||
new_conv_id,
|
||||
conv_update,
|
||||
new_conv_name,
|
||||
new_chat_suggestion,
|
||||
] + selector_output
|
||||
|
||||
def toggle_delete(self, conv_id):
|
||||
@ -1038,24 +1044,26 @@ class ChatPage(BasePage):
|
||||
|
||||
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 = []
|
||||
|
||||
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 = []
|
||||
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 gr.update(visible=True), suggested_questions
|
||||
|
||||
return suggested_ques, updated
|
||||
return gr.update(visible=False), gr.update()
|
||||
|
@ -9,18 +9,29 @@ class ChatSuggestion(BasePage):
|
||||
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}
|
||||
|
@ -139,9 +139,7 @@ class ConversationControl(BasePage):
|
||||
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 @@ class ConversationControl(BasePage):
|
||||
selected = {}
|
||||
|
||||
chats = result.data_source.get("messages", [])
|
||||
|
||||
chat_suggestions = result.data_source.get("chat_suggestions", [])
|
||||
|
||||
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…
x
Reference in New Issue
Block a user