Skip to content

Commit

Permalink
fix: clean chatUI and simplify regen logic (#452) bump:patch
Browse files Browse the repository at this point in the history
  • Loading branch information
taprosoft authored Nov 2, 2024
1 parent fb241a4 commit bd2490b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 151 deletions.
4 changes: 3 additions & 1 deletion flowsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
KH_DOC_DIR = this_dir / "docs"

KH_MODE = "dev"
KH_FEATURE_CHAT_SUGGESTION = config("KH_FEATURE_CHAT_SUGGESTION", default=False)
KH_FEATURE_CHAT_SUGGESTION = config(
"KH_FEATURE_CHAT_SUGGESTION", default=False, cast=bool
)
KH_FEATURE_USER_MANAGEMENT = config(
"KH_FEATURE_USER_MANAGEMENT", default=True, cast=bool
)
Expand Down
4 changes: 4 additions & 0 deletions libs/ktem/ktem/assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ mark {
right: 40px;
}

.upload-button {
display: none;
}

.scrollable {
overflow-y: auto;
}
Expand Down
149 changes: 17 additions & 132 deletions libs/ktem/ktem/pages/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ def on_register_events(self):
gr.on(
triggers=[
self.chat_panel.text_input.submit,
self.chat_panel.submit_btn.click,
],
fn=self.submit_msg,
inputs=[
Expand Down Expand Up @@ -312,105 +311,6 @@ def on_register_events(self):
concurrency_limit=20,
)

regen_event = (
self.chat_panel.regen_btn.click(
fn=self.regen_fn,
inputs=[
self.chat_control.conversation_id,
self.chat_panel.chatbot,
self._app.settings_state,
self._reasoning_type,
self._llm_type,
self.state_chat,
self._app.user_id,
]
+ self._indices_input,
outputs=[
self.chat_panel.chatbot,
self.info_panel,
self.plot_panel,
self.state_plot_panel,
self.state_chat,
],
concurrency_limit=20,
show_progress="minimal",
)
.then(
fn=lambda: True,
inputs=None,
outputs=[self._preview_links],
js=pdfview_js,
)
.success(
fn=self.check_and_suggest_name_conv,
inputs=self.chat_panel.chatbot,
outputs=[
self.chat_control.conversation_rn,
self._conversation_renamed,
],
)
.success(
self.chat_control.rename_conv,
inputs=[
self.chat_control.conversation_id,
self.chat_control.conversation_rn,
self._conversation_renamed,
self._app.user_id,
],
outputs=[
self.chat_control.conversation,
self.chat_control.conversation,
self.chat_control.conversation_rn,
],
show_progress="hidden",
)
)

# 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,
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",
)

# final data persist
regen_event = regen_event.then(
fn=self.persist_data_source,
inputs=[
self.chat_control.conversation_id,
self._app.user_id,
self.info_panel,
self.state_plot_panel,
self.state_retrieval_history,
self.state_plot_history,
self.chat_panel.chatbot,
self.state_chat,
]
+ self._indices_input,
outputs=[
self.state_retrieval_history,
self.state_plot_history,
],
concurrency_limit=20,
)

self.chat_control.btn_info_expand.click(
fn=lambda is_expanded: (
gr.update(scale=INFO_PANEL_SCALES[is_expanded]),
Expand Down Expand Up @@ -616,6 +516,15 @@ def submit_msg(
if not chat_input:
raise ValueError("Input is empty")

chat_input_text = chat_input.get("text", "")

# check if regen mode is active
if chat_input_text:
chat_history = chat_history + [(chat_input_text, None)]
else:
if not chat_history:
raise gr.Error("Empty chat")

if not conv_id:
id_, update = self.chat_control.new_conv(user_id)
with Session(engine) as session:
Expand All @@ -637,8 +546,8 @@ def submit_msg(
new_chat_suggestion = chat_suggest

return (
"",
chat_history + [(chat_input, None)],
{},
chat_history,
new_conv_id,
conv_update,
new_conv_name,
Expand Down Expand Up @@ -871,9 +780,13 @@ def chat_fn(
*selecteds,
):
"""Chat function"""
chat_input = chat_history[-1][0]
chat_input, chat_output = chat_history[-1]
chat_history = chat_history[:-1]

# if chat_input is empty, assume regen mode
if chat_output:
state["app"]["regen"] = True

queue: asyncio.Queue[Optional[dict]] = asyncio.Queue()

# construct the pipeline
Expand Down Expand Up @@ -921,6 +834,7 @@ def chat_fn(
plot_gr = self._json_to_plot(plot)

state[pipeline.get_info()["id"]] = reasoning_state["pipeline"]

yield (
chat_history + [(chat_input, text or msg_placeholder)],
refs,
Expand All @@ -942,35 +856,6 @@ def chat_fn(
state,
)

def regen_fn(
self,
conversation_id,
chat_history,
settings,
reasoning_type,
llm_type,
state,
user_id,
*selecteds,
):
"""Regen function"""
if not chat_history:
gr.Warning("Empty chat")
yield chat_history, "", state
return

state["app"]["regen"] = True
yield from self.chat_fn(
conversation_id,
chat_history,
settings,
reasoning_type,
llm_type,
state,
user_id,
*selecteds,
)

def check_and_suggest_name_conv(self, chat_history):
suggest_pipeline = SuggestConvNamePipeline()
new_name = gr.update()
Expand Down
21 changes: 5 additions & 16 deletions libs/ktem/ktem/pages/chat/chat_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,13 @@ def on_building_ui(self):
bubble_full_width=False,
)
with gr.Row():
self.text_input = gr.Text(
self.text_input = gr.MultimodalTextbox(
interactive=True,
scale=20,
file_count="multiple",
placeholder="Chat input",
scale=15,
container=False,
max_lines=10,
)
self.submit_btn = gr.Button(
value="Send",
scale=1,
min_width=10,
variant="primary",
elem_classes=["cap-button-height"],
)
self.regen_btn = gr.Button(
value="Regen",
scale=1,
min_width=10,
elem_classes=["cap-button-height"],
show_label=False,
)

def submit_msg(self, chat_input, chat_history):
Expand Down
4 changes: 2 additions & 2 deletions libs/ktem/ktem/pages/chat/chat_suggestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def on_building_ui(self):
with gr.Accordion(label="Chat Suggestion", open=False) as self.accordion:
self.example = gr.DataFrame(
value=chat_samples,
headers=["Sample"],
headers=["Next Question"],
interactive=False,
wrap=True,
)
Expand All @@ -23,4 +23,4 @@ def as_gradio_component(self):
return self.example

def select_example(self, ev: gr.SelectData):
return ev.value
return {"text": ev.value}

0 comments on commit bd2490b

Please sign in to comment.