fix: 🐛 user-usage

fixed calculation
This commit is contained in:
Stan Girard 2024-01-04 01:40:23 +01:00
parent a65eb5a9cd
commit ff59ce99a6
3 changed files with 79 additions and 3 deletions

View File

@ -40,6 +40,14 @@ class UserUsage(UserIdentity):
return request return request
def get_user_daily_usage(self, date):
"""
Fetch the user daily usage from the database
"""
request = self.supabase_db.get_user_requests_count_for_day(self.id, date)
return request
def handle_increment_user_request_count(self, date, number=1): def handle_increment_user_request_count(self, date, number=1):
""" """
Increment the user request count in the database Increment the user request count in the database
@ -54,7 +62,7 @@ class UserUsage(UserIdentity):
self.supabase_db.create_user_daily_usage( self.supabase_db.create_user_daily_usage(
user_id=self.id, date=date, user_email=self.email, number=number user_id=self.id, date=date, user_email=self.email, number=number
) )
self.daily_requests_count = number self.daily_requests_count = current_requests_count + number
return return
self.supabase_db.increment_user_request_count( self.supabase_db.increment_user_request_count(

View File

@ -29,13 +29,19 @@ def check_user_requests_limit(user: UserIdentity, model: str):
date = time.strftime("%Y%m%d") date = time.strftime("%Y%m%d")
daily_chat_credit = userSettings.get("daily_chat_credit", 0) daily_chat_credit = userSettings.get("daily_chat_credit", 0)
daily_user_count = userDailyUsage.get_user_daily_usage(date)
models_price = userDailyUsage.get_model_settings() models_price = userDailyUsage.get_model_settings()
user_choosen_model_price = 1000 user_choosen_model_price = 1000
for model_setting in models_price: for model_setting in models_price:
if model_setting["name"] == model: if model_setting["name"] == model:
user_choosen_model_price = model_setting["price"] user_choosen_model_price = model_setting["price"]
if int(userDailyUsage.daily_requests_count) >= int(daily_chat_credit): print(f"User {user.id} has {daily_user_count} requests for today")
print(f"User {user.id} has {daily_chat_credit} daily chat credit")
print(f"User {user.id} has {user_choosen_model_price} price for the model")
print(f"User {user.id} has {models_price} models price")
if int(daily_user_count + user_choosen_model_price) > int(daily_chat_credit):
raise HTTPException( raise HTTPException(
status_code=429, # pyright: ignore reportPrivateUsage=none status_code=429, # pyright: ignore reportPrivateUsage=none
detail=f"You have reached your daily chat limit of {daily_chat_credit} requests per day. Please upgrade your plan to increase your daily chat limit.", detail=f"You have reached your daily chat limit of {daily_chat_credit} requests per day. Please upgrade your plan to increase your daily chat limit.",

View File

@ -1 +1,63 @@
{"actions_bar_placeholder":"Ask a question to a @brain and choose your #prompt","add_document":"Add document","ask":"Ask a question, or describe a task.","back_to_chat":"Back to chat","brain":"brain","brains":"brains","change_brain":"Change brain","chat":"Chat","chatDeleted":"Chat successfully deleted. Id: {{id}}","chatNameUpdated":"Chat name updated","error_occurred":"Error occurred while getting answer","errorCallingAPI":"Error calling the API","errorDeleting":"Error deleting chat: {{error}}","errorFetching":"Error occurred while fetching your chats","errorParsingData":"Error parsing data","feed_brain_placeholder":"Choose which @brain you want to feed with these files","feedingBrain":"Your newly added knowledge is being processed, you can keep chatting in the meantime !","history":"History","keyboard_shortcuts":"Keyboard shortcuts","last30Days":"Previous 30 days","last7Days":"Previous 7 days","limit_reached":"You have reached the limit of requests, please try again later","menu":"Menu","missing_brain":"Please select a brain to chat with","new_discussion":"New discussion","new_prompt":"Create new prompt","noCurrentBrain":"No current brain","onboarding":{"answer":{"how_to_use_quivr":"Check the documentation https://brain.quivr.app/docs/intro.html","what_is_brain":"A brain is a virtual folder for organizing information on a specific topic. It can store documents and connect to external apps or APIs. For instance, a 'Medical Science' brain could contain health-related data, and a 'Legal' brain could have legal information. Brains can be made public for others to use without revealing the contents, promoting knowledge sharing.","what_is_quivr":"Quivr is a helpful assistant. You can easily drag and drop files into the chat or the knowledge section to interact with them. It's not just a chat tool; you can also communicate with apps using APIs.\nTo keep your work organized, you can create brains, essentially virtual folders, and subscribe to others' brains in the explore section for seamless collaboration and information sharing."},"how_to_use_quivr":"How to use Quivr ?","step_1_1":"1. Drag and drop file on the chat or in the 📎.","step_1_2":"Don't have a file ? Download 'Quivr documentation'","step_2":"2. Start chatting with your file","step_3":"3. Enjoy !","title":"Hi 👋🏻 Want to discover Quivr ? 😇","what_is_brain":"What is a brain ?","what_is_quivr":"What is Quivr ?"},"parameters":"Parameters","receivedResponse":"Received response. Starting to handle stream...","resposeBodyNull":"Response body is null","shortcut_choose_prompt":"#: Choose a specific prompt","shortcut_create_brain":"@+: Create a new brain","shortcut_create_prompt":"#+: Create a new custom prompt","shortcut_feed_brain":"/+: Feed a brain with knowledge","shortcut_go_to_shortcuts":"CMDK: Go to shortcuts","shortcut_go_to_user_page":"CMDU: Go to user page","shortcut_manage_brains":"CMDB: Manage your brains","shortcut_select_brain":"@: Select a brain","shortcut_select_file":"/: Select a file to talk to","subtitle":"Talk to a language model about your uploaded data","thinking":"Thinking...","title":"Chat with {{brain}}","today":"Today","tooManyRequests":"You have exceeded the number of requests per day. To continue chatting, please enter an OpenAI API key in your profile or in used brain.","welcome":"Welcome","yesterday":"Yesterday"} {
"actions_bar_placeholder": "Ask a question to a @brain and choose your #prompt",
"add_document": "Add document",
"ask": "Ask a question, or describe a task.",
"back_to_chat": "Back to chat",
"brain": "brain",
"brains": "brains",
"change_brain": "Change brain",
"chat": "Chat",
"chatDeleted": "Chat successfully deleted. Id: {{id}}",
"chatNameUpdated": "Chat name updated",
"error_occurred": "Error occurred while getting answer",
"errorCallingAPI": "Error calling the API",
"errorDeleting": "Error deleting chat: {{error}}",
"errorFetching": "Error occurred while fetching your chats",
"errorParsingData": "Error parsing data",
"feed_brain_placeholder": "Choose which @brain you want to feed with these files",
"feedingBrain": "Your newly added knowledge is being processed, you can keep chatting in the meantime !",
"history": "History",
"keyboard_shortcuts": "Keyboard shortcuts",
"last30Days": "Previous 30 days",
"last7Days": "Previous 7 days",
"limit_reached": "You have reached the limit of requests, please try again later",
"menu": "Menu",
"missing_brain": "Please select a brain to chat with",
"new_discussion": "New discussion",
"new_prompt": "Create new prompt",
"noCurrentBrain": "No current brain",
"onboarding": {
"answer": {
"how_to_use_quivr": "Check the documentation https://brain.quivr.app/docs/intro.html",
"what_is_brain": "A brain is a virtual folder for organizing information on a specific topic. It can store documents and connect to external apps or APIs. For instance, a 'Medical Science' brain could contain health-related data, and a 'Legal' brain could have legal information. Brains can be made public for others to use without revealing the contents, promoting knowledge sharing.",
"what_is_quivr": "Quivr is a helpful assistant. You can easily drag and drop files into the chat or the knowledge section to interact with them. It's not just a chat tool; you can also communicate with apps using APIs.\nTo keep your work organized, you can create brains, essentially virtual folders, and subscribe to others' brains in the explore section for seamless collaboration and information sharing."
},
"how_to_use_quivr": "How to use Quivr ?",
"step_1_1": "1. Drag and drop file on the chat or in the 📎.",
"step_1_2": "Don't have a file ? Download 'Quivr documentation'",
"step_2": "2. Start chatting with your file",
"step_3": "3. Enjoy !",
"title": "Hi 👋🏻 Want to discover Quivr ? 😇",
"what_is_brain": "What is a brain ?",
"what_is_quivr": "What is Quivr ?"
},
"parameters": "Parameters",
"receivedResponse": "Received response. Starting to handle stream...",
"resposeBodyNull": "Response body is null",
"shortcut_choose_prompt": "#: Choose a specific prompt",
"shortcut_create_brain": "@+: Create a new brain",
"shortcut_create_prompt": "#+: Create a new custom prompt",
"shortcut_feed_brain": "/+: Feed a brain with knowledge",
"shortcut_go_to_shortcuts": "CMDK: Go to shortcuts",
"shortcut_go_to_user_page": "CMDU: Go to user page",
"shortcut_manage_brains": "CMDB: Manage your brains",
"shortcut_select_brain": "@: Select a brain",
"shortcut_select_file": "/: Select a file to talk to",
"subtitle": "Talk to a language model about your uploaded data",
"thinking": "Thinking...",
"title": "Chat with {{brain}}",
"today": "Today",
"tooManyRequests": "You have exceeded the number of requests per day. To continue chatting, please upgrade your account or come back tomorrow.",
"welcome": "Welcome",
"yesterday": "Yesterday"
}