Spaces:
Build error
Build error
T-K-O-H commited on
Commit ·
501ef25
1
Parent(s): bbc5090
Update code to use environment variables and improve error handling
Browse files
app.py
CHANGED
|
@@ -42,8 +42,11 @@ Try these examples:
|
|
| 42 |
What would you like to know?"""
|
| 43 |
|
| 44 |
# Send welcome message
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
logger.info("Chat session started successfully")
|
| 49 |
except Exception as e:
|
|
@@ -82,37 +85,48 @@ async def handle_message(message: cl.Message):
|
|
| 82 |
# Add response to history
|
| 83 |
history.append({"role": "assistant", "content": formatted_response})
|
| 84 |
|
| 85 |
-
# Create a message with proper formatting
|
| 86 |
-
elements = []
|
| 87 |
-
|
| 88 |
# Check if the response contains ticker suggestions
|
| 89 |
if "Did you mean one of these?" in formatted_response:
|
| 90 |
# Split the response into main message and suggestions
|
| 91 |
main_msg, suggestions = formatted_response.split("Did you mean one of these?")
|
| 92 |
|
| 93 |
# Send the main message
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# Create a list of suggestions
|
| 98 |
suggestion_list = suggestions.strip().split('\n')
|
|
|
|
| 99 |
for suggestion in suggestion_list:
|
| 100 |
if suggestion.strip():
|
| 101 |
elements.append(cl.Text(name="Suggestion", content=suggestion.strip()))
|
| 102 |
|
| 103 |
# Send the suggestions
|
| 104 |
if elements:
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
else:
|
| 108 |
# Send regular response
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
else:
|
| 112 |
error_msg = "Received an invalid response format from the agent."
|
| 113 |
logger.error(f"{error_msg} Response: {response}")
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# Update history in storage
|
| 118 |
chat_histories[session_id] = history
|
|
@@ -120,8 +134,11 @@ async def handle_message(message: cl.Message):
|
|
| 120 |
except Exception as e:
|
| 121 |
error_msg = f"Error occurred: {str(e)}"
|
| 122 |
logger.error(error_msg)
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
@cl.on_chat_end
|
| 127 |
async def end_chat():
|
|
|
|
| 42 |
What would you like to know?"""
|
| 43 |
|
| 44 |
# Send welcome message
|
| 45 |
+
await cl.Message(
|
| 46 |
+
content=welcome_message,
|
| 47 |
+
author="Assistant",
|
| 48 |
+
type="text"
|
| 49 |
+
).send()
|
| 50 |
|
| 51 |
logger.info("Chat session started successfully")
|
| 52 |
except Exception as e:
|
|
|
|
| 85 |
# Add response to history
|
| 86 |
history.append({"role": "assistant", "content": formatted_response})
|
| 87 |
|
|
|
|
|
|
|
|
|
|
| 88 |
# Check if the response contains ticker suggestions
|
| 89 |
if "Did you mean one of these?" in formatted_response:
|
| 90 |
# Split the response into main message and suggestions
|
| 91 |
main_msg, suggestions = formatted_response.split("Did you mean one of these?")
|
| 92 |
|
| 93 |
# Send the main message
|
| 94 |
+
await cl.Message(
|
| 95 |
+
content=main_msg.strip(),
|
| 96 |
+
author="Assistant",
|
| 97 |
+
type="text"
|
| 98 |
+
).send()
|
| 99 |
|
| 100 |
# Create a list of suggestions
|
| 101 |
suggestion_list = suggestions.strip().split('\n')
|
| 102 |
+
elements = []
|
| 103 |
for suggestion in suggestion_list:
|
| 104 |
if suggestion.strip():
|
| 105 |
elements.append(cl.Text(name="Suggestion", content=suggestion.strip()))
|
| 106 |
|
| 107 |
# Send the suggestions
|
| 108 |
if elements:
|
| 109 |
+
await cl.Message(
|
| 110 |
+
content="",
|
| 111 |
+
elements=elements,
|
| 112 |
+
author="Assistant",
|
| 113 |
+
type="text"
|
| 114 |
+
).send()
|
| 115 |
else:
|
| 116 |
# Send regular response
|
| 117 |
+
await cl.Message(
|
| 118 |
+
content=formatted_response,
|
| 119 |
+
author="Assistant",
|
| 120 |
+
type="text"
|
| 121 |
+
).send()
|
| 122 |
else:
|
| 123 |
error_msg = "Received an invalid response format from the agent."
|
| 124 |
logger.error(f"{error_msg} Response: {response}")
|
| 125 |
+
await cl.Message(
|
| 126 |
+
content=error_msg,
|
| 127 |
+
author="Assistant",
|
| 128 |
+
type="text"
|
| 129 |
+
).send()
|
| 130 |
|
| 131 |
# Update history in storage
|
| 132 |
chat_histories[session_id] = history
|
|
|
|
| 134 |
except Exception as e:
|
| 135 |
error_msg = f"Error occurred: {str(e)}"
|
| 136 |
logger.error(error_msg)
|
| 137 |
+
await cl.Message(
|
| 138 |
+
content="Sorry, something went wrong while processing your request. Please try again.",
|
| 139 |
+
author="Assistant",
|
| 140 |
+
type="text"
|
| 141 |
+
).send()
|
| 142 |
|
| 143 |
@cl.on_chat_end
|
| 144 |
async def end_chat():
|