mirror of
https://github.com/StanGirard/quivr.git
synced 2024-11-27 18:32:55 +03:00
e931d29017
* feat: new classes * chore: private llm refactor and enable streaming * fix: validation errors in pytest * fix: default brain behavior * Update backend/llm/base.py * chore: remove 🪦 * fix: remove 🪦
69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
from typing import Optional
|
|
from uuid import UUID
|
|
|
|
from logger import get_logger
|
|
from models.settings import common_dependencies
|
|
from pydantic import BaseModel
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
class User(BaseModel):
|
|
id: UUID
|
|
email: Optional[str]
|
|
user_openai_api_key: Optional[str] = None
|
|
requests_count: int = 0
|
|
|
|
# [TODO] Rename the user table and its references to 'user_usage'
|
|
def create_user(self, date):
|
|
commons = common_dependencies()
|
|
logger.info(f"New user entry in db document for user {self.email}")
|
|
|
|
return (
|
|
commons["supabase"]
|
|
.table("users")
|
|
.insert(
|
|
{
|
|
"user_id": self.id,
|
|
"email": self.email,
|
|
"date": date,
|
|
"requests_count": 1,
|
|
}
|
|
)
|
|
.execute()
|
|
)
|
|
|
|
def get_user_request_stats(self):
|
|
commons = common_dependencies()
|
|
requests_stats = (
|
|
commons["supabase"]
|
|
.from_("users")
|
|
.select("*")
|
|
.filter("user_id", "eq", self.id)
|
|
.execute()
|
|
)
|
|
return requests_stats.data
|
|
|
|
def fetch_user_requests_count(self, date):
|
|
commons = common_dependencies()
|
|
response = (
|
|
commons["supabase"]
|
|
.from_("users")
|
|
.select("*")
|
|
.filter("user_id", "eq", self.id)
|
|
.filter("date", "eq", date)
|
|
.execute()
|
|
)
|
|
userItem = next(iter(response.data or []), {"requests_count": 0})
|
|
|
|
return userItem["requests_count"]
|
|
|
|
def increment_user_request_count(self, date):
|
|
commons = common_dependencies()
|
|
requests_count = self.fetch_user_requests_count(date) + 1
|
|
logger.info(f"User {self.email} request count updated to {requests_count}")
|
|
commons["supabase"].table("users").update(
|
|
{"requests_count": requests_count}
|
|
).match({"user_id": self.id, "date": date}).execute()
|
|
self.requests_count = requests_count
|