Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import string
|
|
| 5 |
import time
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
|
|
|
| 8 |
# Global variables to store workspace and bot IDs
|
| 9 |
GLOBAL_WORKSPACE_ID = None
|
| 10 |
GLOBAL_BOT_ID = None
|
|
@@ -231,8 +232,8 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
|
|
| 231 |
max_retries = 3
|
| 232 |
timeout = 120 # Increased timeout for long messages
|
| 233 |
|
| 234 |
-
# Flag to track if we need to create new IDs
|
| 235 |
-
|
| 236 |
|
| 237 |
# Attempt to send the request
|
| 238 |
for attempt in range(max_retries):
|
|
@@ -247,19 +248,26 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
|
|
| 247 |
print(f"Successfully received response from Botpress API")
|
| 248 |
return assistant_content, bot_id, workspace_id
|
| 249 |
|
| 250 |
-
#
|
| 251 |
elif response.status_code == 403:
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
# Handle network errors or timeouts (just retry)
|
| 262 |
-
elif response.status_code in [
|
| 263 |
print(f"Received error {response.status_code}. Retrying...")
|
| 264 |
time.sleep(3) # Wait before retrying
|
| 265 |
continue
|
|
@@ -289,23 +297,35 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
|
|
| 289 |
else:
|
| 290 |
return f"Unable to get a response from the assistant: {str(e)}", bot_id, workspace_id
|
| 291 |
|
| 292 |
-
# If we need
|
| 293 |
-
if
|
| 294 |
-
print("Creating new workspace and bot
|
| 295 |
|
| 296 |
-
# First,
|
| 297 |
if bot_id and workspace_id:
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
-
# Create new
|
| 302 |
new_workspace_id = create_workspace()
|
| 303 |
if not new_workspace_id:
|
| 304 |
-
return "Failed to create a new workspace. Please try again later.", bot_id, workspace_id
|
| 305 |
|
|
|
|
| 306 |
new_bot_id = create_bot(new_workspace_id)
|
| 307 |
if not new_bot_id:
|
| 308 |
-
return "Failed to create a new bot. Please try again later.", new_workspace_id, workspace_id
|
| 309 |
|
| 310 |
# Update headers with new bot ID
|
| 311 |
headers["x-bot-id"] = new_bot_id
|
|
|
|
| 5 |
import time
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
# Global variables to store workspace and bot IDs
|
| 10 |
GLOBAL_WORKSPACE_ID = None
|
| 11 |
GLOBAL_BOT_ID = None
|
|
|
|
| 232 |
max_retries = 3
|
| 233 |
timeout = 120 # Increased timeout for long messages
|
| 234 |
|
| 235 |
+
# Flag to track if we need to create new IDs due to quota exceeded
|
| 236 |
+
quota_exceeded = False
|
| 237 |
|
| 238 |
# Attempt to send the request
|
| 239 |
for attempt in range(max_retries):
|
|
|
|
| 248 |
print(f"Successfully received response from Botpress API")
|
| 249 |
return assistant_content, bot_id, workspace_id
|
| 250 |
|
| 251 |
+
# Check for quota exceeded error specifically
|
| 252 |
elif response.status_code == 403:
|
| 253 |
+
error_data = response.json()
|
| 254 |
+
error_message = error_data.get('message', '')
|
| 255 |
+
|
| 256 |
+
# Check if this is the specific quota exceeded error
|
| 257 |
+
if "has reached its usage limit for ai spend" in error_message:
|
| 258 |
+
print(f"Quota exceeded error detected: {error_message}")
|
| 259 |
+
quota_exceeded = True
|
| 260 |
+
break
|
| 261 |
+
else:
|
| 262 |
+
print(f"Received 403 error but not quota exceeded: {error_message}")
|
| 263 |
+
if attempt < max_retries - 1:
|
| 264 |
+
time.sleep(2)
|
| 265 |
+
continue
|
| 266 |
+
else:
|
| 267 |
+
return f"Unable to get a response from the assistant (Error 403).", bot_id, workspace_id
|
| 268 |
|
| 269 |
# Handle network errors or timeouts (just retry)
|
| 270 |
+
elif response.status_code in [404, 408, 502, 503, 504]:
|
| 271 |
print(f"Received error {response.status_code}. Retrying...")
|
| 272 |
time.sleep(3) # Wait before retrying
|
| 273 |
continue
|
|
|
|
| 297 |
else:
|
| 298 |
return f"Unable to get a response from the assistant: {str(e)}", bot_id, workspace_id
|
| 299 |
|
| 300 |
+
# If quota exceeded, we need to create new resources
|
| 301 |
+
if quota_exceeded:
|
| 302 |
+
print("Quota exceeded. Creating new workspace and bot...")
|
| 303 |
|
| 304 |
+
# First delete the bot, then the workspace (in that order)
|
| 305 |
if bot_id and workspace_id:
|
| 306 |
+
print(f"Deleting bot {bot_id} first...")
|
| 307 |
+
delete_success = delete_bot(bot_id, workspace_id)
|
| 308 |
+
if delete_success:
|
| 309 |
+
print(f"Successfully deleted bot {bot_id}")
|
| 310 |
+
else:
|
| 311 |
+
print(f"Failed to delete bot {bot_id}")
|
| 312 |
+
|
| 313 |
+
print(f"Now deleting workspace {workspace_id}...")
|
| 314 |
+
ws_delete_success = delete_workspace(workspace_id)
|
| 315 |
+
if ws_delete_success:
|
| 316 |
+
print(f"Successfully deleted workspace {workspace_id}")
|
| 317 |
+
else:
|
| 318 |
+
print(f"Failed to delete workspace {workspace_id}")
|
| 319 |
|
| 320 |
+
# Create new workspace
|
| 321 |
new_workspace_id = create_workspace()
|
| 322 |
if not new_workspace_id:
|
| 323 |
+
return "Failed to create a new workspace after quota exceeded. Please try again later.", bot_id, workspace_id
|
| 324 |
|
| 325 |
+
# Create new bot in the new workspace
|
| 326 |
new_bot_id = create_bot(new_workspace_id)
|
| 327 |
if not new_bot_id:
|
| 328 |
+
return "Failed to create a new bot after quota exceeded. Please try again later.", new_workspace_id, workspace_id
|
| 329 |
|
| 330 |
# Update headers with new bot ID
|
| 331 |
headers["x-bot-id"] = new_bot_id
|