Skip to content

Commit 8288932

Browse files
authored
Merge pull request #10 from pbv0/genie-dev-ctd
Improved Genie
2 parents 29a2678 + 6fd7119 commit 8288932

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

dash/pages/genie_api.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
from databricks.sdk import WorkspaceClient
2828
2929
30+
# Refer to the source code for the full implmenetation.
31+
32+
3033
def get_query_result(statement_id):
3134
# For simplicity, let's say data fits in one chunk, query.manifest.total_chunk_count = 1
3235
@@ -274,28 +277,31 @@ def update_chat(n_clicks, genie_space_id, conversation_id, prompt, chat_history)
274277
try:
275278
if conversation_id:
276279
conversation = w.genie.create_message_and_wait(genie_space_id, conversation_id, prompt)
280+
# Access error codes via conversation.error for detailed exception handling.
277281
else:
278282
conversation = w.genie.start_conversation_and_wait(genie_space_id, prompt)
279283
conversation_id = conversation.conversation_id
284+
# Access error codes via conversation.error for detailed exception handling.
280285

281286
chat_history = process_genie_response(conversation, chat_history)
282287
chat_display = format_message_display(chat_history)
283288

284289
return chat_history, chat_display, conversation_id
285290

286291
except Exception as e:
287-
return dash.no_update, dbc.Alert(f"An error occurred: {str(e)}", color="danger")
292+
return dash.no_update, dbc.Alert(f"Check the required permissions. An error occurred: {str(e)}", color="danger"), ""
288293

289294

290295
@callback(
291296
[Output("chat-history-store", "data", allow_duplicate=True),
292297
Output("chat-history", "children", allow_duplicate=True),
293298
Output("conversation-id", "value", allow_duplicate=True),],
294-
Input("clear-button", "n_clicks"),
299+
[Input("clear-button", "n_clicks"),
300+
Input("genie-space-id", "value"),],
295301
prevent_initial_call=True,
296302
)
297-
def clear_chat(n_clicks):
298-
return [], [], None if n_clicks else (dash.no_update, dash.no_update, None)
303+
def clear_chat(n_clicks, value):
304+
return [], [], None if n_clicks or value else (dash.no_update, dash.no_update, None)
299305

300306

301307
@callback(

streamlit/views/genie_api.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import streamlit as st
22
from databricks.sdk import WorkspaceClient
3+
from databricks.sdk.errors.sdk import OperationFailed
34
from databricks.sdk.service.dashboards import GenieMessage
45
import pandas as pd
56
from typing import Dict
@@ -20,10 +21,16 @@
2021
tab_a, tab_b, tab_c = st.tabs(["**Try it**", "**Code snippet**", "**Requirements**"])
2122

2223
with tab_a:
24+
def reset_conversation():
25+
st.session_state.conversation_id = None
26+
st.session_state.messages = []
27+
2328
genie_space_id = st.text_input(
2429
"Genie Space ID", placeholder="01efe16a65e21836acefb797ae6a8fe4", help="Room ID in the Genie Space URL"
2530
)
26-
st.session_state.genie_space_id = genie_space_id
31+
if genie_space_id != st.session_state.get("genie_space_id", ""):
32+
reset_conversation()
33+
st.session_state.genie_space_id = genie_space_id
2734

2835

2936
def display_message(message: Dict):
@@ -65,18 +72,13 @@ def process_genie_response(response: GenieMessage):
6572
display_message(message)
6673
st.session_state.messages.append(message)
6774

68-
def reset_conversation():
69-
st.session_state.conversation_id = None
70-
st.session_state.messages = []
71-
7275

7376
if "messages" not in st.session_state:
7477
st.session_state.messages = []
7578

76-
if st.session_state.genie_space_id == genie_space_id:
77-
for message in st.session_state.messages:
78-
with st.chat_message(message["role"]):
79-
display_message(message)
79+
for message in st.session_state.messages:
80+
with st.chat_message(message["role"]):
81+
display_message(message)
8082

8183
if prompt := st.chat_input("Ask your question..."):
8284
st.chat_message("user").markdown(prompt)
@@ -86,22 +88,36 @@ def reset_conversation():
8688
if genie_space_id:
8789
status = st.status("Thinking")
8890
if st.session_state.get("conversation_id"):
89-
conversation = w.genie.create_message_and_wait(
90-
genie_space_id, st.session_state.conversation_id, prompt
91-
)
91+
try:
92+
conversation = w.genie.create_message_and_wait(
93+
genie_space_id, st.session_state.conversation_id, prompt
94+
)
95+
except Exception as e:
96+
status.update(label="Conversation failed. Check the required permissions.", state="error")
97+
st.button("New Chat", on_click=reset_conversation)
98+
raise e
99+
if conversation.error:
100+
st.error(conversation.error.type, conversation.error.error)
92101
process_genie_response(conversation)
93102
else:
94-
conversation = w.genie.start_conversation_and_wait(genie_space_id, prompt)
103+
try:
104+
conversation = w.genie.start_conversation_and_wait(genie_space_id, prompt)
105+
except Exception as e:
106+
status.update(label="Failed to initialize Genie. Check the required permissions.", state="error")
107+
st.button("New Chat", on_click=reset_conversation)
108+
raise e
109+
if conversation.error:
110+
st.error(conversation.error.type, conversation.error.error)
95111
process_genie_response(conversation)
96112
status.update(label="", state="complete")
97-
98113
st.button("New Chat", on_click=reset_conversation)
99114
st.link_button("Open Genie", f"{w.config.host}/genie/rooms/{genie_space_id}/chats/{st.session_state.conversation_id}")
100115
else:
101116
st.error("Please fill in the Genie Space ID.")
102117

103118

104119
with tab_b:
120+
st.markdown("Refer to the source code for the full implmenetation.")
105121
st.code(
106122
"""
107123
import streamlit as st

0 commit comments

Comments
 (0)