Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Added exponential backoff
Browse files- chat_application/main.py +31 -1
chat_application/main.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from flask import Flask, request, render_template, redirect, url_for, session, make_response, render_template_string
|
| 2 |
from flask_socketio import SocketIO, join_room, leave_room, send
|
| 3 |
from pymongo import MongoClient
|
|
@@ -319,7 +320,7 @@ def get_response_delay(response):
|
|
| 319 |
|
| 320 |
# Ask a bot for its response, store in DB, and send to client
|
| 321 |
# Returns true if the bot passed
|
| 322 |
-
def ask_bot(room_id, bot, bot_display_name, initial_prompt, instruct_prompt):
|
| 323 |
# Prevents crashing if bot model did not load
|
| 324 |
if bot is None:
|
| 325 |
return False
|
|
@@ -352,6 +353,35 @@ def ask_bot(room_id, bot, bot_display_name, initial_prompt, instruct_prompt):
|
|
| 352 |
),
|
| 353 |
)
|
| 354 |
parsed_response = response.candidates[0].content.parts[0].text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
except Exception as e:
|
| 356 |
print("Error in bot response: ", e)
|
| 357 |
print("Treating this bot's response as a pass.")
|
|
|
|
| 1 |
+
from google.api_core import exceptions
|
| 2 |
from flask import Flask, request, render_template, redirect, url_for, session, make_response, render_template_string
|
| 3 |
from flask_socketio import SocketIO, join_room, leave_room, send
|
| 4 |
from pymongo import MongoClient
|
|
|
|
| 320 |
|
| 321 |
# Ask a bot for its response, store in DB, and send to client
|
| 322 |
# Returns true if the bot passed
|
| 323 |
+
def ask_bot(room_id, bot, bot_display_name, initial_prompt, instruct_prompt , wait_time = 1):
|
| 324 |
# Prevents crashing if bot model did not load
|
| 325 |
if bot is None:
|
| 326 |
return False
|
|
|
|
| 353 |
),
|
| 354 |
)
|
| 355 |
parsed_response = response.candidates[0].content.parts[0].text.strip()
|
| 356 |
+
|
| 357 |
+
# Deal with rate limit issues
|
| 358 |
+
except exceptions.TooManyRequests:
|
| 359 |
+
print(f"429 Rate Limit Exceeded")
|
| 360 |
+
socketio.sleep(wait_time)
|
| 361 |
+
wait_time = wait_time *= 2
|
| 362 |
+
|
| 363 |
+
# Prevent Stack Overflow
|
| 364 |
+
if wait_time > 32:
|
| 365 |
+
print("Rate Limit Exceeded and Exponential Backoff Too Long")
|
| 366 |
+
print("Treating this bot's response as a pass.")
|
| 367 |
+
room_doc = rooms_collection.find_one({"_id": room_id})
|
| 368 |
+
if not room_doc or room_doc.get("ended", False):
|
| 369 |
+
return False
|
| 370 |
+
# Store the error response in the database
|
| 371 |
+
bot_message = {
|
| 372 |
+
"sender": bot_display_name,
|
| 373 |
+
"message": "ERROR in bot response - treated as a (pass)",
|
| 374 |
+
"timestamp": datetime.utcnow()
|
| 375 |
+
}
|
| 376 |
+
rooms_collection.update_one(
|
| 377 |
+
{"_id": room_id},
|
| 378 |
+
{"$push": {"messages": bot_message}}
|
| 379 |
+
)
|
| 380 |
+
return True
|
| 381 |
+
|
| 382 |
+
ask_bot(room_id, bot, bot_display_name, initial_prompt, instruct_prompt , wait_time = wait_time)
|
| 383 |
+
|
| 384 |
+
|
| 385 |
except Exception as e:
|
| 386 |
print("Error in bot response: ", e)
|
| 387 |
print("Treating this bot's response as a pass.")
|