Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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:
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 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("
|
| 229 |
|
| 230 |
except Exception as e:
|
| 231 |
-
# If
|
| 232 |
-
if attempt
|
| 233 |
-
#
|
| 234 |
-
|
| 235 |
-
delete_bot(bot_id, workspace_id)
|
| 236 |
-
delete_workspace(workspace_id)
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
| 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
|