|
1 | 1 | from dash import html, dcc, callback, Input, Output, State |
2 | 2 | import dash_bootstrap_components as dbc |
3 | | -from dash.exceptions import PreventUpdate |
4 | 3 | import dash |
5 | 4 | from databricks.sdk import WorkspaceClient |
6 | 5 | from databricks.sdk.service.dashboards import GenieMessage |
|
13 | 12 | __name__, |
14 | 13 | path='/bi/genie', |
15 | 14 | title='Genie', |
16 | | - name='Converse with your data', |
| 15 | + name='Genie', |
17 | 16 | category='Business Intelligence', |
18 | 17 | icon='material-symbols:model-training' |
19 | 18 | ) |
@@ -151,6 +150,7 @@ def layout(): |
151 | 150 |
|
152 | 151 | # Chat history area |
153 | 152 | html.Div(id="chat-history-genie", className="mt-4"), |
| 153 | + dcc.Store(id='conversation-id'), |
154 | 154 |
|
155 | 155 | # Status/error messages |
156 | 156 | html.Div(id="status-area-genie", className="mt-3") |
@@ -237,39 +237,41 @@ def process_genie_response(response): |
237 | 237 | Output("status-area-genie", "children")], |
238 | 238 | Input("chat-button", "n_clicks"), |
239 | 239 | [State("genie-space-id-input", "value"), |
| 240 | + State("conversation-id", "value"), |
240 | 241 | State("question-input", "value")], |
241 | 242 | prevent_initial_call=True |
242 | 243 | ) |
243 | | -def update_chat(n_clicks, genie_space_id, question): |
244 | | - if not all([genie_space_id, question]): |
245 | | - return no_update, dbc.Alert( |
| 244 | +def update_chat(n_clicks, genie_space_id, conversation_id, prompt): |
| 245 | + if not all([genie_space_id, prompt]): |
| 246 | + return dash.no_update, dbc.Alert( |
246 | 247 | "Please fill in all fields", |
247 | 248 | color="warning" |
248 | 249 | ) |
249 | 250 |
|
250 | 251 | try: |
251 | | - if st.session_state.get("conversation_id"): |
| 252 | + if conversation_id: |
252 | 253 | conversation = w.genie.create_message_and_wait( |
253 | | - genie_space_id, st.session_state.conversation_id, prompt |
| 254 | + genie_space_id, conversation_id, prompt |
254 | 255 | ) |
255 | 256 | process_genie_response(conversation) |
256 | 257 | else: |
257 | 258 | conversation = w.genie.start_conversation_and_wait(genie_space_id, prompt) |
| 259 | + conversation_id = conversation.conversation_id |
258 | 260 | process_genie_response(conversation) |
259 | 261 |
|
260 | 262 |
|
261 | 263 | return [ |
262 | 264 | html.Div([ |
263 | 265 | dbc.Card( |
264 | 266 | dbc.CardBody([ |
265 | | - html.P(f"Q: {question}"), |
| 267 | + html.P(f"Q: {prompt}"), |
266 | 268 | html.P("A: Processing your question...") |
267 | 269 | ]) |
268 | 270 | ) |
269 | 271 | ], className="mb-3") |
270 | 272 | ], None |
271 | 273 | except Exception as e: |
272 | | - return no_update, dbc.Alert( |
| 274 | + return dash.no_update, dbc.Alert( |
273 | 275 | f"An error occurred: {str(e)}", |
274 | 276 | color="danger" |
275 | 277 | ) |
|
0 commit comments