Spaces:
Build error
Build error
XThomasBU
commited on
Commit
·
ab2ff67
1
Parent(s):
7bce799
updates
Browse files- code/main.py +33 -11
- code/modules/chat_processor/helpers.py +16 -28
- code/modules/config/constants.py +2 -2
code/main.py
CHANGED
|
@@ -21,6 +21,7 @@ from modules.chat_processor.helpers import (
|
|
| 21 |
get_time,
|
| 22 |
check_user_cooldown,
|
| 23 |
reset_tokens_for_user,
|
|
|
|
| 24 |
)
|
| 25 |
import copy
|
| 26 |
from typing import Optional
|
|
@@ -55,6 +56,24 @@ async def setup_data_layer():
|
|
| 55 |
return data_layer
|
| 56 |
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
class Chatbot:
|
| 59 |
def __init__(self, config):
|
| 60 |
"""
|
|
@@ -367,6 +386,10 @@ class Chatbot:
|
|
| 367 |
|
| 368 |
# update user info with last message time
|
| 369 |
user = cl.user_session.get("user")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
|
| 371 |
print("\n\n User Tokens Left: ", user.metadata["tokens_left"])
|
| 372 |
|
|
@@ -406,6 +429,15 @@ class Chatbot:
|
|
| 406 |
user.metadata["in_cooldown"] = True
|
| 407 |
await update_user_info(user)
|
| 408 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
|
| 410 |
user.metadata["in_cooldown"] = False
|
| 411 |
|
|
@@ -486,18 +518,8 @@ class Chatbot:
|
|
| 486 |
print("Time taken to generate questions: ", time.time() - start_time)
|
| 487 |
|
| 488 |
# # update user info with token count
|
| 489 |
-
|
| 490 |
-
user.metadata["tokens_left"] = user.metadata["tokens_left"] - token_count
|
| 491 |
-
user.metadata["all_time_tokens_allocated"] = (
|
| 492 |
-
user.metadata["all_time_tokens_allocated"] - token_count
|
| 493 |
-
)
|
| 494 |
-
await reset_tokens_for_user(user) # regenerate tokens for the user
|
| 495 |
-
user.metadata["last_message_time"] = get_time()
|
| 496 |
-
await update_user_info(user)
|
| 497 |
|
| 498 |
-
tokens_left = user.metadata["tokens_left"]
|
| 499 |
-
if tokens_left < 0:
|
| 500 |
-
tokens_left = 0
|
| 501 |
answer_with_sources += (
|
| 502 |
'\n\n<footer><span style="font-size: 0.8em; text-align: right; display: block;">Tokens Left: '
|
| 503 |
+ str(tokens_left)
|
|
|
|
| 21 |
get_time,
|
| 22 |
check_user_cooldown,
|
| 23 |
reset_tokens_for_user,
|
| 24 |
+
get_user_details,
|
| 25 |
)
|
| 26 |
import copy
|
| 27 |
from typing import Optional
|
|
|
|
| 56 |
return data_layer
|
| 57 |
|
| 58 |
|
| 59 |
+
async def update_user_from_chainlit(user, token_count=0):
|
| 60 |
+
if "admin" not in user.metadata["role"]:
|
| 61 |
+
user.metadata["tokens_left"] = user.metadata["tokens_left"] - token_count
|
| 62 |
+
user.metadata["all_time_tokens_allocated"] = (
|
| 63 |
+
user.metadata["all_time_tokens_allocated"] - token_count
|
| 64 |
+
)
|
| 65 |
+
user.metadata["tokens_left_at_last_message"] = user.metadata[
|
| 66 |
+
"tokens_left"
|
| 67 |
+
] # tokens_left will keep regenerating outside of chainlit
|
| 68 |
+
user.metadata["last_message_time"] = get_time()
|
| 69 |
+
await update_user_info(user)
|
| 70 |
+
|
| 71 |
+
tokens_left = user.metadata["tokens_left"]
|
| 72 |
+
if tokens_left < 0:
|
| 73 |
+
tokens_left = 0
|
| 74 |
+
return tokens_left
|
| 75 |
+
|
| 76 |
+
|
| 77 |
class Chatbot:
|
| 78 |
def __init__(self, config):
|
| 79 |
"""
|
|
|
|
| 386 |
|
| 387 |
# update user info with last message time
|
| 388 |
user = cl.user_session.get("user")
|
| 389 |
+
await reset_tokens_for_user(user)
|
| 390 |
+
updated_user = await get_user_details(user.identifier)
|
| 391 |
+
user.metadata = updated_user.metadata
|
| 392 |
+
cl.user_session.set("user", user)
|
| 393 |
|
| 394 |
print("\n\n User Tokens Left: ", user.metadata["tokens_left"])
|
| 395 |
|
|
|
|
| 429 |
user.metadata["in_cooldown"] = True
|
| 430 |
await update_user_info(user)
|
| 431 |
return
|
| 432 |
+
else:
|
| 433 |
+
await cl.Message(
|
| 434 |
+
content=(
|
| 435 |
+
"Ah, seems like you don't have any tokens left...Please wait while we regenerate your tokens. Click "
|
| 436 |
+
'<a href="/cooldown" style="color: #0000CD; text-decoration: none;" target="_self">here</a> to view your token credits.'
|
| 437 |
+
),
|
| 438 |
+
author=SYSTEM,
|
| 439 |
+
).send()
|
| 440 |
+
return
|
| 441 |
|
| 442 |
user.metadata["in_cooldown"] = False
|
| 443 |
|
|
|
|
| 518 |
print("Time taken to generate questions: ", time.time() - start_time)
|
| 519 |
|
| 520 |
# # update user info with token count
|
| 521 |
+
tokens_left = await update_user_from_chainlit(user, token_count)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 522 |
|
|
|
|
|
|
|
|
|
|
| 523 |
answer_with_sources += (
|
| 524 |
'\n\n<footer><span style="font-size: 0.8em; text-align: right; display: block;">Tokens Left: '
|
| 525 |
+ str(tokens_left)
|
code/modules/chat_processor/helpers.py
CHANGED
|
@@ -206,46 +206,34 @@ async def reset_tokens_for_user(user_info):
|
|
| 206 |
elapsed_time_in_seconds = (current_time - last_message_time).total_seconds()
|
| 207 |
|
| 208 |
# Current token count (can be negative)
|
| 209 |
-
current_tokens = user_info["metadata"].get("
|
|
|
|
| 210 |
|
| 211 |
# Maximum tokens that can be regenerated
|
| 212 |
max_tokens = user_info["metadata"].get("max_tokens", TOKENS_LEFT)
|
| 213 |
|
| 214 |
# Calculate how many tokens should have been regenerated proportionally
|
| 215 |
if current_tokens < max_tokens:
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
else:
|
| 222 |
-
time_to_full_regen = REGEN_TIME * (
|
| 223 |
-
1 - current_tokens / max_tokens
|
| 224 |
-
) # less time to regenerate if tokens left is positive
|
| 225 |
-
|
| 226 |
-
# Calculate the proportion of this time that has elapsed
|
| 227 |
-
proportion_of_time_elapsed = elapsed_time_in_seconds / time_to_full_regen
|
| 228 |
-
|
| 229 |
-
# Ensure the proportion doesn't exceed 1.0
|
| 230 |
-
proportion_of_time_elapsed = min(proportion_of_time_elapsed, 1.0)
|
| 231 |
-
|
| 232 |
-
# Calculate the tokens to regenerate based on the elapsed proportion
|
| 233 |
tokens_to_regenerate = int(
|
| 234 |
-
|
| 235 |
)
|
| 236 |
|
| 237 |
-
# Ensure
|
| 238 |
-
|
| 239 |
-
new_token_count = min(current_tokens + tokens_to_regenerate, max_tokens)
|
| 240 |
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
|
| 245 |
-
|
| 246 |
-
|
| 247 |
|
| 248 |
-
|
| 249 |
|
| 250 |
|
| 251 |
async def get_thread_step_info(thread_id):
|
|
|
|
| 206 |
elapsed_time_in_seconds = (current_time - last_message_time).total_seconds()
|
| 207 |
|
| 208 |
# Current token count (can be negative)
|
| 209 |
+
current_tokens = user_info["metadata"].get("tokens_left_at_last_message", 0)
|
| 210 |
+
current_tokens = min(current_tokens, TOKENS_LEFT)
|
| 211 |
|
| 212 |
# Maximum tokens that can be regenerated
|
| 213 |
max_tokens = user_info["metadata"].get("max_tokens", TOKENS_LEFT)
|
| 214 |
|
| 215 |
# Calculate how many tokens should have been regenerated proportionally
|
| 216 |
if current_tokens < max_tokens:
|
| 217 |
+
|
| 218 |
+
# Calculate the regeneration rate per second based on REGEN_TIME for full regeneration
|
| 219 |
+
regeneration_rate_per_second = max_tokens / REGEN_TIME
|
| 220 |
+
|
| 221 |
+
# Calculate how many tokens should have been regenerated based on the elapsed time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
tokens_to_regenerate = int(
|
| 223 |
+
elapsed_time_in_seconds * regeneration_rate_per_second
|
| 224 |
)
|
| 225 |
|
| 226 |
+
# Ensure the new token count does not exceed max_tokens
|
| 227 |
+
new_token_count = min(current_tokens + tokens_to_regenerate, max_tokens)
|
|
|
|
| 228 |
|
| 229 |
+
print(
|
| 230 |
+
f"\n\n Adding {tokens_to_regenerate} tokens to the user, Time elapsed: {elapsed_time_in_seconds} seconds, Tokens after regeneration: {new_token_count}, Tokens before: {current_tokens} \n\n"
|
| 231 |
+
)
|
| 232 |
|
| 233 |
+
# Update the user's token count
|
| 234 |
+
user_info["metadata"]["tokens_left"] = new_token_count
|
| 235 |
|
| 236 |
+
await update_user_info(user_info)
|
| 237 |
|
| 238 |
|
| 239 |
async def get_thread_step_info(thread_id):
|
code/modules/config/constants.py
CHANGED
|
@@ -4,8 +4,8 @@ import os
|
|
| 4 |
load_dotenv()
|
| 5 |
|
| 6 |
TIMEOUT = 60
|
| 7 |
-
COOLDOWN_TIME =
|
| 8 |
-
REGEN_TIME =
|
| 9 |
TOKENS_LEFT = 2000
|
| 10 |
ALL_TIME_TOKENS_ALLOCATED = 1000000
|
| 11 |
|
|
|
|
| 4 |
load_dotenv()
|
| 5 |
|
| 6 |
TIMEOUT = 60
|
| 7 |
+
COOLDOWN_TIME = 60
|
| 8 |
+
REGEN_TIME = 180
|
| 9 |
TOKENS_LEFT = 2000
|
| 10 |
ALL_TIME_TOKENS_ALLOCATED = 1000000
|
| 11 |
|