Farid Karimli commited on
Commit
96bddf1
·
1 Parent(s): 97415a1

Metadata fix

Browse files
apps/ai_tutor/app.py CHANGED
@@ -211,12 +211,10 @@ async def auth_google(request: Request):
211
 
212
  # add literalai user info to session store to be sent to chainlit
213
  literalai_user = await get_user_details(email)
214
- print(f"Literalai user: {literalai_user}")
215
  session_store[session_token]["literalai_info"] = literalai_user.to_dict()
216
  session_store[session_token]["literalai_info"]["metadata"]["role"] = role
217
 
218
  user_info_json = json.dumps(session_store[session_token])
219
- print(f"User info json: {user_info_json}")
220
  user_info_encoded = base64.b64encode(user_info_json.encode()).decode()
221
 
222
  # Set cookies
@@ -272,10 +270,10 @@ async def post_signin(request: Request):
272
  current_datetime = get_time()
273
  user_details.metadata["last_login"] = current_datetime
274
  # if new user, set the number of tries
 
 
275
  if "tokens_left" not in user_details.metadata:
276
- user_details.metadata["tokens_left"] = (
277
- TOKENS_LEFT # set the number of tokens left for the new user
278
- )
279
  if "last_message_time" not in user_details.metadata:
280
  user_details.metadata["last_message_time"] = current_datetime
281
  if "all_time_tokens_allocated" not in user_details.metadata:
 
211
 
212
  # add literalai user info to session store to be sent to chainlit
213
  literalai_user = await get_user_details(email)
 
214
  session_store[session_token]["literalai_info"] = literalai_user.to_dict()
215
  session_store[session_token]["literalai_info"]["metadata"]["role"] = role
216
 
217
  user_info_json = json.dumps(session_store[session_token])
 
218
  user_info_encoded = base64.b64encode(user_info_json.encode()).decode()
219
 
220
  # Set cookies
 
270
  current_datetime = get_time()
271
  user_details.metadata["last_login"] = current_datetime
272
  # if new user, set the number of tries
273
+ if "role" not in user_details.metadata:
274
+ user_details.metadata["role"] = get_user_role(user_info["email"])
275
  if "tokens_left" not in user_details.metadata:
276
+ user_details.metadata["tokens_left"] = TOKENS_LEFT
 
 
277
  if "last_message_time" not in user_details.metadata:
278
  user_details.metadata["last_message_time"] = current_datetime
279
  if "all_time_tokens_allocated" not in user_details.metadata:
apps/ai_tutor/chainlit_app.py CHANGED
@@ -505,7 +505,7 @@ class Chatbot:
505
  await self.start()
506
 
507
  @cl.header_auth_callback
508
- def header_auth_callback(headers: dict) -> Optional[cl.User]:
509
  # try: # TODO: Add try-except block after testing
510
  # TODO: Implement to get the user information from the headers (not the cookie)
511
  cookie = headers.get("cookie") # gets back a str
@@ -521,10 +521,14 @@ class Chatbot:
521
  ).decode()
522
  decoded_user_info = json.loads(decoded_user_info)
523
 
 
 
 
 
524
  return cl.User(
525
  id=decoded_user_info["literalai_info"]["id"],
526
  identifier=decoded_user_info["literalai_info"]["identifier"],
527
- metadata=decoded_user_info["literalai_info"]["metadata"],
528
  )
529
 
530
  async def on_follow_up(self, action: cl.Action):
 
505
  await self.start()
506
 
507
  @cl.header_auth_callback
508
+ async def header_auth_callback(headers: dict) -> Optional[cl.User]:
509
  # try: # TODO: Add try-except block after testing
510
  # TODO: Implement to get the user information from the headers (not the cookie)
511
  cookie = headers.get("cookie") # gets back a str
 
521
  ).decode()
522
  decoded_user_info = json.loads(decoded_user_info)
523
 
524
+ user_id = decoded_user_info["literalai_info"]["identifier"]
525
+ user_info = await get_user_details(user_id)
526
+ metadata = user_info.metadata
527
+
528
  return cl.User(
529
  id=decoded_user_info["literalai_info"]["id"],
530
  identifier=decoded_user_info["literalai_info"]["identifier"],
531
+ metadata=metadata,
532
  )
533
 
534
  async def on_follow_up(self, action: cl.Action):
apps/ai_tutor/helpers.py CHANGED
@@ -46,10 +46,13 @@ async def check_user_cooldown(
46
  async def reset_tokens_for_user(user_info, TOKENS_LEFT, REGEN_TIME):
47
  user_info = convert_to_dict(user_info)
48
  last_message_time_str = user_info["metadata"].get("last_message_time")
 
 
 
 
 
 
49
 
50
- last_message_time = datetime.fromisoformat(last_message_time_str).replace(
51
- tzinfo=timezone.utc
52
- )
53
  current_time = datetime.fromisoformat(get_time()).replace(tzinfo=timezone.utc)
54
 
55
  # Calculate the elapsed time since the last message
 
46
  async def reset_tokens_for_user(user_info, TOKENS_LEFT, REGEN_TIME):
47
  user_info = convert_to_dict(user_info)
48
  last_message_time_str = user_info["metadata"].get("last_message_time")
49
+ try:
50
+ last_message_time = datetime.fromisoformat(last_message_time_str).replace(
51
+ tzinfo=timezone.utc
52
+ )
53
+ except Exception: # this probably means the user has never sent a message before
54
+ last_message_time = datetime.min.replace(tzinfo=timezone.utc)
55
 
 
 
 
56
  current_time = datetime.fromisoformat(get_time()).replace(tzinfo=timezone.utc)
57
 
58
  # Calculate the elapsed time since the last message