CORVO-AI commited on
Commit
758e870
·
verified ·
1 Parent(s): 24fb0b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -43
app.py CHANGED
@@ -209,55 +209,28 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
209
  if new_workspace_id:
210
  new_bot_id = create_bot(new_workspace_id)
211
 
212
- # Step 3: Install integration for the new bot
213
- if new_bot_id:
214
- install_bot_integration(new_bot_id, new_workspace_id)
215
-
216
- # Step 4: Retry with the new IDs
217
- headers["x-bot-id"] = new_bot_id
218
- retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
219
- if retry_response.status_code == 200:
220
- data = retry_response.json()
221
- assistant_content = data.get('output', {}).get('choices', [{}])[0].get('content', '')
222
- return assistant_content, new_bot_id, new_workspace_id
223
- else:
224
- return f"Unable to get a response from the assistant.", new_bot_id, new_workspace_id
225
 
226
  # Any other error status code
227
  else:
228
- raise Exception("Invalid or expired bot ID.")
229
 
230
  except Exception as e:
231
- # If invalid/expired bot or any other error, regenerate workspace/bot
232
- if attempt == max_retries - 1: # Last attempt
233
- # Attempt to delete old IDs if they exist
234
- if bot_id and workspace_id:
235
- delete_bot(bot_id, workspace_id)
236
- delete_workspace(workspace_id)
237
 
238
- # Create fresh IDs
239
- new_workspace = create_workspace()
240
- if new_workspace:
241
- new_bot = create_bot(new_workspace)
242
- if new_bot:
243
- # Update headers with the new bot ID
244
- headers["x-bot-id"] = new_bot
245
- # Retry with new IDs
246
- try:
247
- retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
248
- if retry_response.status_code == 200:
249
- data = retry_response.json()
250
- assistant_content = data.get('output', {}).get('choices', [{}])[0].get('content', '')
251
- return assistant_content, new_bot, new_workspace
252
- else:
253
- return f"Unable to get a response from the assistant.", new_bot, new_workspace
254
- except Exception as retry_error:
255
- return f"Unable to get a response from the assistant.", new_bot, new_workspace
256
- # If we get here, all regeneration attempts failed
257
- return "Unable to get a response from the assistant.", None, None
258
-
259
- # Not the last attempt, wait and retry
260
- time.sleep(2)
261
 
262
  # Should not reach here due to the handling in the loop
263
  return "Unable to get a response from the assistant.", bot_id, workspace_id
 
209
  if new_workspace_id:
210
  new_bot_id = create_bot(new_workspace_id)
211
 
212
+ # Step 3: Retry with the new IDs
213
+ headers["x-bot-id"] = new_bot_id
214
+ retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
215
+ if retry_response.status_code == 200:
216
+ data = retry_response.json()
217
+ assistant_content = data.get('output', {}).get('choices', [{}])[0].get('content', '')
218
+ return assistant_content, new_bot_id, new_workspace_id
219
+ else:
220
+ return f"Unable to get a response from the assistant.", new_bot_id, new_workspace_id
 
 
 
 
221
 
222
  # Any other error status code
223
  else:
224
+ raise Exception("Unexpected error occurred.")
225
 
226
  except Exception as e:
227
+ # If this is not the last attempt, retry the same request
228
+ if attempt < max_retries - 1:
229
+ time.sleep(2) # Wait before retrying
230
+ continue
 
 
231
 
232
+ # If this is the last attempt, handle it gracefully
233
+ return f"Unable to get a response from the assistant after multiple attempts.", bot_id, workspace_id
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
 
235
  # Should not reach here due to the handling in the loop
236
  return "Unable to get a response from the assistant.", bot_id, workspace_id