CORVO-AI commited on
Commit
ed64977
·
verified ·
1 Parent(s): 147d835

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -21
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
- need_new_ids = False
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
- # Handle 403 error (authentication/authorization issue)
251
  elif response.status_code == 403:
252
- print(f"Received 403 error. Need to create new IDs.")
253
- need_new_ids = True
254
- break
255
-
256
- elif response.status_code == 404:
257
- print(f"Received 403 error. Need to create new IDs.")
258
- need_new_ids = True
259
- break
 
 
 
 
 
 
 
260
 
261
  # Handle network errors or timeouts (just retry)
262
- elif response.status_code in [443, 408, 502, 503, 504]:
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 new IDs, create them and try again
293
- if need_new_ids:
294
- print("Creating new workspace and bot IDs...")
295
 
296
- # First, try to clean up old resources
297
  if bot_id and workspace_id:
298
- delete_bot(bot_id, workspace_id)
299
- delete_workspace(workspace_id)
 
 
 
 
 
 
 
 
 
 
 
300
 
301
- # Create new resources
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