Skip to content

Commit ca0420a

Browse files
committed
Improve Genie in Streamlit
1 parent f4fcce6 commit ca0420a

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

streamlit/views/genie_api.py

Lines changed: 26 additions & 12 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,9 +21,14 @@
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
)
31+
reset_conversation()
2632
st.session_state.genie_space_id = genie_space_id
2733

2834

@@ -65,18 +71,13 @@ def process_genie_response(response: GenieMessage):
6571
display_message(message)
6672
st.session_state.messages.append(message)
6773

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

7375
if "messages" not in st.session_state:
7476
st.session_state.messages = []
7577

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)
78+
for message in st.session_state.messages:
79+
with st.chat_message(message["role"]):
80+
display_message(message)
8081

8182
if prompt := st.chat_input("Ask your question..."):
8283
st.chat_message("user").markdown(prompt)
@@ -86,12 +87,24 @@ def reset_conversation():
8687
if genie_space_id:
8788
status = st.status("Thinking")
8889
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-
)
90+
try:
91+
conversation = w.genie.create_message_and_wait(
92+
genie_space_id, st.session_state.conversation_id, prompt
93+
)
94+
except OperationFailed as e:
95+
st.error("Conversation failed. Check the required permissions.")
96+
raise e
97+
if conversation.error:
98+
st.error(conversation.error.type, conversation.error.error)
9299
process_genie_response(conversation)
93100
else:
94-
conversation = w.genie.start_conversation_and_wait(genie_space_id, prompt)
101+
try:
102+
conversation = w.genie.start_conversation_and_wait(genie_space_id, prompt)
103+
except OperationFailed as e:
104+
st.error("Failed to initialize Genie. Check the required permissions.")
105+
raise e
106+
if conversation.error:
107+
st.error(conversation.error.type, conversation.error.error)
95108
process_genie_response(conversation)
96109
status.update(label="", state="complete")
97110

@@ -102,6 +115,7 @@ def reset_conversation():
102115

103116

104117
with tab_b:
118+
st.markdown("Refer to the source code for the full implmenetation.")
105119
st.code(
106120
"""
107121
import streamlit as st

0 commit comments

Comments
 (0)