Update app.py
Browse files
app.py
CHANGED
|
@@ -125,8 +125,8 @@ def install_bot_integration(bot_id, workspace_id):
|
|
| 125 |
return False
|
| 126 |
|
| 127 |
|
| 128 |
-
def
|
| 129 |
-
"""
|
| 130 |
if not bot_id or not workspace_id:
|
| 131 |
print("Cannot delete bot: Missing bot ID or workspace ID")
|
| 132 |
return False
|
|
@@ -151,8 +151,8 @@ def delete_bot(bot_id, workspace_id):
|
|
| 151 |
return False
|
| 152 |
|
| 153 |
|
| 154 |
-
def
|
| 155 |
-
"""
|
| 156 |
if not workspace_id:
|
| 157 |
print("Cannot delete workspace: No workspace ID provided")
|
| 158 |
return False
|
|
@@ -223,7 +223,7 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperat
|
|
| 223 |
"systemPrompt": system_prompt,
|
| 224 |
"messages": messages,
|
| 225 |
"temperature": temperature,
|
| 226 |
-
|
| 227 |
"debug": False,
|
| 228 |
}
|
| 229 |
}
|
|
@@ -236,9 +236,6 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperat
|
|
| 236 |
max_retries = 3
|
| 237 |
timeout = 120 # Increased timeout for long messages
|
| 238 |
|
| 239 |
-
# Flag to track if we need to create new IDs due to quota exceeded
|
| 240 |
-
quota_exceeded = False
|
| 241 |
-
|
| 242 |
# Attempt to send the request
|
| 243 |
for attempt in range(max_retries):
|
| 244 |
try:
|
|
@@ -252,23 +249,72 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperat
|
|
| 252 |
print(f"Successfully received response from Botpress API")
|
| 253 |
return assistant_content, bot_id, workspace_id
|
| 254 |
|
| 255 |
-
# Check for
|
| 256 |
-
elif response.status_code
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
-
|
| 261 |
-
if
|
| 262 |
-
print(
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
if attempt < max_retries - 1:
|
| 268 |
time.sleep(2)
|
| 269 |
continue
|
| 270 |
else:
|
| 271 |
-
return f"
|
| 272 |
|
| 273 |
# Handle network errors or timeouts (just retry)
|
| 274 |
elif response.status_code in [404, 408, 502, 503, 504]:
|
|
@@ -301,61 +347,6 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperat
|
|
| 301 |
else:
|
| 302 |
return f"Unable to get a response from the assistant: {str(e)}", bot_id, workspace_id
|
| 303 |
|
| 304 |
-
# If quota exceeded, we need to create new resources
|
| 305 |
-
if quota_exceeded:
|
| 306 |
-
print("Quota exceeded. Creating new workspace and bot...")
|
| 307 |
-
|
| 308 |
-
# First delete the bot, then the workspace (in that order)
|
| 309 |
-
if bot_id and workspace_id:
|
| 310 |
-
print(f"Deleting bot {bot_id} first...")
|
| 311 |
-
delete_success = delete_bot(bot_id, workspace_id)
|
| 312 |
-
if delete_success:
|
| 313 |
-
print(f"Successfully deleted bot {bot_id}")
|
| 314 |
-
else:
|
| 315 |
-
print(f"Failed to delete bot {bot_id}")
|
| 316 |
-
|
| 317 |
-
# Wait 3 seconds before deleting the workspace
|
| 318 |
-
print("Waiting 3 seconds before deleting workspace...")
|
| 319 |
-
time.sleep(3)
|
| 320 |
-
|
| 321 |
-
print(f"Now deleting workspace {workspace_id}...")
|
| 322 |
-
ws_delete_success = delete_workspace(workspace_id)
|
| 323 |
-
if ws_delete_success:
|
| 324 |
-
print(f"Successfully deleted workspace {workspace_id}")
|
| 325 |
-
else:
|
| 326 |
-
print(f"Failed to delete workspace {workspace_id}")
|
| 327 |
-
|
| 328 |
-
# Create new workspace
|
| 329 |
-
new_workspace_id = create_workspace()
|
| 330 |
-
if not new_workspace_id:
|
| 331 |
-
return "Failed to create a new workspace after quota exceeded. Please try again later.", bot_id, workspace_id
|
| 332 |
-
|
| 333 |
-
# Create new bot in the new workspace
|
| 334 |
-
new_bot_id = create_bot(new_workspace_id)
|
| 335 |
-
if not new_bot_id:
|
| 336 |
-
return "Failed to create a new bot after quota exceeded. Please try again later.", new_workspace_id, workspace_id
|
| 337 |
-
|
| 338 |
-
# Update headers with new bot ID
|
| 339 |
-
headers["x-bot-id"] = new_bot_id
|
| 340 |
-
|
| 341 |
-
# Try one more time with the new IDs
|
| 342 |
-
try:
|
| 343 |
-
print(f"Retrying with new bot_id={new_bot_id}, workspace_id={new_workspace_id}")
|
| 344 |
-
retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
|
| 345 |
-
|
| 346 |
-
if retry_response.status_code == 200:
|
| 347 |
-
data = retry_response.json()
|
| 348 |
-
assistant_content = data.get('output', {}).get('choices', [{}])[0].get('content', '')
|
| 349 |
-
print(f"Successfully received response with new IDs")
|
| 350 |
-
return assistant_content, new_bot_id, new_workspace_id
|
| 351 |
-
else:
|
| 352 |
-
print(f"Failed with new IDs: {retry_response.status_code}, {retry_response.text}")
|
| 353 |
-
return f"Unable to get a response from the assistant with new credentials.", new_bot_id, new_workspace_id
|
| 354 |
-
|
| 355 |
-
except Exception as e:
|
| 356 |
-
print(f"Error with new IDs: {str(e)}")
|
| 357 |
-
return f"Unable to get a response from the assistant with new credentials: {str(e)}", new_bot_id, new_workspace_id
|
| 358 |
-
|
| 359 |
# Should not reach here due to the handling in the loop
|
| 360 |
return "Unable to get a response from the assistant.", bot_id, workspace_id
|
| 361 |
|
|
|
|
| 125 |
return False
|
| 126 |
|
| 127 |
|
| 128 |
+
def try_delete_bot(bot_id, workspace_id):
|
| 129 |
+
"""Attempt to delete a bot from the specified workspace but continue if it fails"""
|
| 130 |
if not bot_id or not workspace_id:
|
| 131 |
print("Cannot delete bot: Missing bot ID or workspace ID")
|
| 132 |
return False
|
|
|
|
| 151 |
return False
|
| 152 |
|
| 153 |
|
| 154 |
+
def try_delete_workspace(workspace_id):
|
| 155 |
+
"""Attempt to delete a workspace but continue if it fails"""
|
| 156 |
if not workspace_id:
|
| 157 |
print("Cannot delete workspace: No workspace ID provided")
|
| 158 |
return False
|
|
|
|
| 223 |
"systemPrompt": system_prompt,
|
| 224 |
"messages": messages,
|
| 225 |
"temperature": temperature,
|
| 226 |
+
|
| 227 |
"debug": False,
|
| 228 |
}
|
| 229 |
}
|
|
|
|
| 236 |
max_retries = 3
|
| 237 |
timeout = 120 # Increased timeout for long messages
|
| 238 |
|
|
|
|
|
|
|
|
|
|
| 239 |
# Attempt to send the request
|
| 240 |
for attempt in range(max_retries):
|
| 241 |
try:
|
|
|
|
| 249 |
print(f"Successfully received response from Botpress API")
|
| 250 |
return assistant_content, bot_id, workspace_id
|
| 251 |
|
| 252 |
+
# Check for authentication or permission errors (401, 403)
|
| 253 |
+
elif response.status_code in [401, 403]:
|
| 254 |
+
error_message = "Authentication error"
|
| 255 |
+
try:
|
| 256 |
+
error_data = response.json()
|
| 257 |
+
error_message = error_data.get('message', 'Authentication error')
|
| 258 |
+
except:
|
| 259 |
+
pass
|
| 260 |
+
|
| 261 |
+
print(f"Authentication error detected: {error_message}")
|
| 262 |
+
|
| 263 |
+
# We need to create new resources immediately
|
| 264 |
+
print("Creating new workspace and bot...")
|
| 265 |
+
new_workspace_id = create_workspace()
|
| 266 |
+
if not new_workspace_id:
|
| 267 |
+
print("Failed to create a new workspace")
|
| 268 |
+
if attempt < max_retries - 1:
|
| 269 |
+
time.sleep(3)
|
| 270 |
+
continue
|
| 271 |
+
else:
|
| 272 |
+
return "Unable to create new resources. Please try again later.", bot_id, workspace_id
|
| 273 |
|
| 274 |
+
new_bot_id = create_bot(new_workspace_id)
|
| 275 |
+
if not new_bot_id:
|
| 276 |
+
print("Failed to create a new bot")
|
| 277 |
+
if attempt < max_retries - 1:
|
| 278 |
+
time.sleep(3)
|
| 279 |
+
continue
|
| 280 |
+
else:
|
| 281 |
+
return "Unable to create new bot. Please try again later.", new_workspace_id, workspace_id
|
| 282 |
+
|
| 283 |
+
print(f"Created new workspace: {new_workspace_id} and bot: {new_bot_id}")
|
| 284 |
+
|
| 285 |
+
# Try again with new IDs
|
| 286 |
+
headers["x-bot-id"] = new_bot_id
|
| 287 |
+
try:
|
| 288 |
+
print(f"Retrying with new bot_id={new_bot_id}")
|
| 289 |
+
retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
|
| 290 |
+
|
| 291 |
+
if retry_response.status_code == 200:
|
| 292 |
+
data = retry_response.json()
|
| 293 |
+
assistant_content = data.get('output', {}).get('choices', [{}])[0].get('content', '')
|
| 294 |
+
print(f"Successfully received response with new IDs")
|
| 295 |
+
|
| 296 |
+
# Try to clean up old resources in the background, but don't wait for result
|
| 297 |
+
if bot_id and workspace_id:
|
| 298 |
+
print(f"Attempting to clean up old resources in the background")
|
| 299 |
+
try_delete_bot(bot_id, workspace_id)
|
| 300 |
+
try_delete_workspace(workspace_id)
|
| 301 |
+
|
| 302 |
+
return assistant_content, new_bot_id, new_workspace_id
|
| 303 |
+
else:
|
| 304 |
+
print(f"Failed with new IDs: {retry_response.status_code}")
|
| 305 |
+
if attempt < max_retries - 1:
|
| 306 |
+
time.sleep(2)
|
| 307 |
+
continue
|
| 308 |
+
else:
|
| 309 |
+
return f"Unable to get a response even with new credentials.", new_bot_id, new_workspace_id
|
| 310 |
+
|
| 311 |
+
except Exception as e:
|
| 312 |
+
print(f"Error with new IDs: {str(e)}")
|
| 313 |
if attempt < max_retries - 1:
|
| 314 |
time.sleep(2)
|
| 315 |
continue
|
| 316 |
else:
|
| 317 |
+
return f"Error with new credentials: {str(e)}", new_bot_id, new_workspace_id
|
| 318 |
|
| 319 |
# Handle network errors or timeouts (just retry)
|
| 320 |
elif response.status_code in [404, 408, 502, 503, 504]:
|
|
|
|
| 347 |
else:
|
| 348 |
return f"Unable to get a response from the assistant: {str(e)}", bot_id, workspace_id
|
| 349 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
# Should not reach here due to the handling in the loop
|
| 351 |
return "Unable to get a response from the assistant.", bot_id, workspace_id
|
| 352 |
|