mirror of
https://github.com/StanGirard/quivr.git
synced 2024-09-11 13:15:41 +03:00
use supabase function
This commit is contained in:
parent
a70c2e6367
commit
ed734a66a9
@ -1,7 +1,7 @@
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import HTTPException
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
from models.users import User
|
||||
from pydantic import DateError
|
||||
|
||||
@ -12,10 +12,9 @@ async def verify_api_key(
|
||||
try:
|
||||
# Use UTC time to avoid timezone issues
|
||||
current_date = datetime.utcnow().date()
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
result = (
|
||||
commons["supabase"]
|
||||
.table("api_keys")
|
||||
supabase_client.table("api_keys")
|
||||
.select("api_key", "creation_time")
|
||||
.filter("api_key", "eq", api_key)
|
||||
.filter("is_active", "eq", True)
|
||||
@ -39,12 +38,11 @@ async def verify_api_key(
|
||||
async def get_user_from_api_key(
|
||||
api_key: str,
|
||||
) -> User:
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
|
||||
# Lookup the user_id from the api_keys table
|
||||
user_id_data = (
|
||||
commons["supabase"]
|
||||
.table("api_keys")
|
||||
supabase_client.table("api_keys")
|
||||
.select("user_id")
|
||||
.filter("api_key", "eq", api_key)
|
||||
.execute()
|
||||
@ -57,8 +55,7 @@ async def get_user_from_api_key(
|
||||
|
||||
# Lookup the email from the users table. Todo: remove and use user_id for credentials
|
||||
user_email_data = (
|
||||
commons["supabase"]
|
||||
.table("users")
|
||||
supabase_client.table("users")
|
||||
.select("email")
|
||||
.filter("user_id", "eq", user_id)
|
||||
.execute()
|
||||
|
@ -2,11 +2,11 @@ from typing import Any, List, Optional
|
||||
from uuid import UUID
|
||||
|
||||
from logger import get_logger
|
||||
from pydantic import BaseModel
|
||||
from utils.vectors import get_unique_files_from_vector_ids
|
||||
|
||||
from models.settings import BrainRateLimiting, CommonsDep, common_dependencies
|
||||
from models.settings import BrainRateLimiting, common_dependencies, get_supabase_client
|
||||
from models.users import User
|
||||
from pydantic import BaseModel
|
||||
from supabase.client import Client
|
||||
from utils.vectors import get_unique_files_from_vector_ids
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@ -27,8 +27,8 @@ class Brain(BaseModel):
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
@property
|
||||
def commons(self) -> CommonsDep:
|
||||
return common_dependencies()
|
||||
def supabase_client(self) -> Client:
|
||||
return get_supabase_client()
|
||||
|
||||
@property
|
||||
def brain_size(self):
|
||||
@ -54,8 +54,7 @@ class Brain(BaseModel):
|
||||
# TODO: move this to a brand new BrainService
|
||||
def get_brain_users(self):
|
||||
response = (
|
||||
self.commons["supabase"]
|
||||
.table("brains_users")
|
||||
self.supabase_client.table("brains_users")
|
||||
.select("id:brain_id, *")
|
||||
.filter("brain_id", "eq", self.id)
|
||||
.execute()
|
||||
@ -65,22 +64,20 @@ class Brain(BaseModel):
|
||||
# TODO: move this to a brand new BrainService
|
||||
def delete_user_from_brain(self, user_id):
|
||||
results = (
|
||||
self.commons["supabase"]
|
||||
.table("brains_users")
|
||||
self.supabase_client.table("brains_users")
|
||||
.select("*")
|
||||
.match({"brain_id": self.id, "user_id": user_id})
|
||||
.execute()
|
||||
)
|
||||
|
||||
if len(results.data) != 0:
|
||||
self.commons["supabase"].table("brains_users").delete().match(
|
||||
self.supabase_client.table("brains_users").delete().match(
|
||||
{"brain_id": self.id, "user_id": user_id}
|
||||
).execute()
|
||||
|
||||
def get_user_brains(self, user_id):
|
||||
response = (
|
||||
self.commons["supabase"]
|
||||
.from_("brains_users")
|
||||
self.supabase_client.from_("brains_users")
|
||||
.select("id:brain_id, rights, brains (id: brain_id, name)")
|
||||
.filter("user_id", "eq", user_id)
|
||||
.execute()
|
||||
@ -93,8 +90,7 @@ class Brain(BaseModel):
|
||||
|
||||
def get_brain_for_user(self, user_id):
|
||||
response = (
|
||||
self.commons["supabase"]
|
||||
.from_("brains_users")
|
||||
self.supabase_client.from_("brains_users")
|
||||
.select("id:brain_id, rights, brains (id: brain_id, name)")
|
||||
.filter("user_id", "eq", user_id)
|
||||
.filter("brain_id", "eq", self.id)
|
||||
@ -106,8 +102,7 @@ class Brain(BaseModel):
|
||||
|
||||
def get_brain_details(self):
|
||||
response = (
|
||||
self.commons["supabase"]
|
||||
.from_("brains")
|
||||
self.supabase_client.from_("brains")
|
||||
.select("id:brain_id, name, *")
|
||||
.filter("brain_id", "eq", self.id)
|
||||
.execute()
|
||||
@ -118,8 +113,7 @@ class Brain(BaseModel):
|
||||
|
||||
def delete_brain(self, user_id):
|
||||
results = (
|
||||
self.commons["supabase"]
|
||||
.table("brains_users")
|
||||
self.supabase_client.table("brains_users")
|
||||
.select("*")
|
||||
.match({"brain_id": self.id, "user_id": user_id, "rights": "Owner"})
|
||||
.execute()
|
||||
@ -128,34 +122,29 @@ class Brain(BaseModel):
|
||||
return {"message": "You are not the owner of this brain."}
|
||||
else:
|
||||
results = (
|
||||
self.commons["supabase"]
|
||||
.table("brains_vectors")
|
||||
self.supabase_client.table("brains_vectors")
|
||||
.delete()
|
||||
.match({"brain_id": self.id})
|
||||
.execute()
|
||||
)
|
||||
|
||||
results = (
|
||||
self.commons["supabase"]
|
||||
.table("brains_users")
|
||||
self.supabase_client.table("brains_users")
|
||||
.delete()
|
||||
.match({"brain_id": self.id})
|
||||
.execute()
|
||||
)
|
||||
|
||||
results = (
|
||||
self.commons["supabase"]
|
||||
.table("brains")
|
||||
self.supabase_client.table("brains")
|
||||
.delete()
|
||||
.match({"brain_id": self.id})
|
||||
.execute()
|
||||
)
|
||||
|
||||
def create_brain(self):
|
||||
commons = common_dependencies()
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.table("brains")
|
||||
self.supabase_client.table("brains")
|
||||
.insert(
|
||||
{
|
||||
"name": self.name,
|
||||
@ -174,10 +163,8 @@ class Brain(BaseModel):
|
||||
return response.data
|
||||
|
||||
def create_brain_user(self, user_id: UUID, rights, default_brain: bool):
|
||||
commons = common_dependencies()
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.table("brains_users")
|
||||
self.supabase_client.table("brains_users")
|
||||
.insert(
|
||||
{
|
||||
"brain_id": str(self.id),
|
||||
@ -195,18 +182,17 @@ class Brain(BaseModel):
|
||||
old_default_brain = get_default_user_brain(user)
|
||||
|
||||
if old_default_brain is not None:
|
||||
self.commons["supabase"].table("brains_users").update(
|
||||
self.supabase_client.table("brains_users").update(
|
||||
{"default_brain": False}
|
||||
).match({"brain_id": old_default_brain["id"], "user_id": user.id}).execute()
|
||||
|
||||
self.commons["supabase"].table("brains_users").update(
|
||||
self.supabase_client.table("brains_users").update(
|
||||
{"default_brain": True}
|
||||
).match({"brain_id": self.id, "user_id": user.id}).execute()
|
||||
|
||||
def create_brain_vector(self, vector_id, file_sha1):
|
||||
response = (
|
||||
self.commons["supabase"]
|
||||
.table("brains_vectors")
|
||||
self.supabase_client.table("brains_vectors")
|
||||
.insert(
|
||||
{
|
||||
"brain_id": str(self.id),
|
||||
@ -221,8 +207,7 @@ class Brain(BaseModel):
|
||||
def get_vector_ids_from_file_sha1(self, file_sha1: str):
|
||||
# move to vectors class
|
||||
vectorsResponse = (
|
||||
self.commons["supabase"]
|
||||
.table("vectors")
|
||||
self.supabase_client.table("vectors")
|
||||
.select("id")
|
||||
.filter("metadata->>file_sha1", "eq", file_sha1)
|
||||
.execute()
|
||||
@ -230,7 +215,7 @@ class Brain(BaseModel):
|
||||
return vectorsResponse.data
|
||||
|
||||
def update_brain_fields(self):
|
||||
self.commons["supabase"].table("brains").update(
|
||||
self.supabase_client.table("brains").update(
|
||||
{
|
||||
"name": self.name,
|
||||
"description": self.description,
|
||||
@ -248,8 +233,7 @@ class Brain(BaseModel):
|
||||
"""
|
||||
|
||||
response = (
|
||||
self.commons["supabase"]
|
||||
.from_("brains_vectors")
|
||||
self.supabase_client.from_("brains_vectors")
|
||||
.select("vector_id")
|
||||
.filter("brain_id", "eq", self.id)
|
||||
.execute()
|
||||
@ -267,8 +251,7 @@ class Brain(BaseModel):
|
||||
def delete_file_from_brain(self, file_name: str):
|
||||
# First, get the vector_ids associated with the file_name
|
||||
vector_response = (
|
||||
self.commons["supabase"]
|
||||
.table("vectors")
|
||||
self.supabase_client.table("vectors")
|
||||
.select("id")
|
||||
.filter("metadata->>file_name", "eq", file_name)
|
||||
.execute()
|
||||
@ -277,14 +260,13 @@ class Brain(BaseModel):
|
||||
|
||||
# For each vector_id, delete the corresponding entry from the 'brains_vectors' table
|
||||
for vector_id in vector_ids:
|
||||
self.commons["supabase"].table("brains_vectors").delete().filter(
|
||||
self.supabase_client.table("brains_vectors").delete().filter(
|
||||
"vector_id", "eq", vector_id
|
||||
).filter("brain_id", "eq", self.id).execute()
|
||||
|
||||
# Check if the vector is still associated with any other brains
|
||||
associated_brains_response = (
|
||||
self.commons["supabase"]
|
||||
.table("brains_vectors")
|
||||
self.supabase_client.table("brains_vectors")
|
||||
.select("brain_id")
|
||||
.filter("vector_id", "eq", vector_id)
|
||||
.execute()
|
||||
@ -295,7 +277,7 @@ class Brain(BaseModel):
|
||||
|
||||
# If the vector is not associated with any other brains, delete it from 'vectors' table
|
||||
if not associated_brains:
|
||||
self.commons["supabase"].table("vectors").delete().filter(
|
||||
self.supabase_client.table("vectors").delete().filter(
|
||||
"id", "eq", vector_id
|
||||
).execute()
|
||||
|
||||
@ -303,10 +285,9 @@ class Brain(BaseModel):
|
||||
|
||||
|
||||
def get_default_user_brain(user: User):
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.from_("brains_users")
|
||||
supabase_client.from_("brains_users")
|
||||
.select("brain_id")
|
||||
.filter("user_id", "eq", user.id)
|
||||
.filter("default_brain", "eq", True)
|
||||
@ -319,8 +300,7 @@ def get_default_user_brain(user: User):
|
||||
|
||||
if default_brain_id:
|
||||
brain_response = (
|
||||
commons["supabase"]
|
||||
.from_("brains")
|
||||
supabase_client.from_("brains")
|
||||
.select("id:brain_id, name, *")
|
||||
.filter("brain_id", "eq", default_brain_id)
|
||||
.execute()
|
||||
@ -339,3 +319,5 @@ def get_default_user_brain_or_create_new(user: User) -> Brain:
|
||||
brain.create_brain()
|
||||
brain.create_brain_user(user.id, "Owner", True)
|
||||
return brain
|
||||
return brain
|
||||
return brain
|
||||
|
@ -1,9 +1,9 @@
|
||||
from uuid import UUID
|
||||
|
||||
from logger import get_logger
|
||||
from models.settings import get_supabase_client
|
||||
from pydantic import BaseModel
|
||||
|
||||
from models.settings import CommonsDep, common_dependencies
|
||||
from supabase.client import Client
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@ -17,14 +17,13 @@ class BrainSubscription(BaseModel):
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
@property
|
||||
def commons(self) -> CommonsDep:
|
||||
return common_dependencies()
|
||||
def supabase_client(self) -> Client:
|
||||
return get_supabase_client()
|
||||
|
||||
def create_subscription_invitation(self):
|
||||
logger.info("Creating subscription invitation")
|
||||
response = (
|
||||
self.commons["supabase"]
|
||||
.table("brain_subscription_invitations")
|
||||
self.supabase_client.table("brain_subscription_invitations")
|
||||
.insert(
|
||||
{
|
||||
"brain_id": str(self.brain_id),
|
||||
@ -39,8 +38,7 @@ class BrainSubscription(BaseModel):
|
||||
def update_subscription_invitation(self):
|
||||
logger.info("Updating subscription invitation")
|
||||
response = (
|
||||
self.commons["supabase"]
|
||||
.table("brain_subscription_invitations")
|
||||
self.supabase_client.table("brain_subscription_invitations")
|
||||
.update({"rights": self.rights})
|
||||
.eq("brain_id", str(self.brain_id))
|
||||
.eq("email", self.email)
|
||||
@ -50,8 +48,7 @@ class BrainSubscription(BaseModel):
|
||||
|
||||
def create_or_update_subscription_invitation(self):
|
||||
response = (
|
||||
self.commons["supabase"]
|
||||
.table("brain_subscription_invitations")
|
||||
self.supabase_client.table("brain_subscription_invitations")
|
||||
.select("*")
|
||||
.eq("brain_id", str(self.brain_id))
|
||||
.eq("email", self.email)
|
||||
|
@ -7,7 +7,7 @@ from fastapi import UploadFile
|
||||
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
||||
from logger import get_logger
|
||||
from models.brains import Brain
|
||||
from models.settings import CommonsDep, common_dependencies
|
||||
from models.settings import CommonsDep, get_supabase_client
|
||||
from pydantic import BaseModel
|
||||
from utils.file import compute_sha1_from_file
|
||||
|
||||
@ -95,10 +95,9 @@ class File(BaseModel):
|
||||
that are associated with the file in the vectors table
|
||||
"""
|
||||
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.table("vectors")
|
||||
supabase_client.table("vectors")
|
||||
.select("id")
|
||||
.filter("metadata->>file_sha1", "eq", self.file_sha1)
|
||||
.execute()
|
||||
@ -132,12 +131,11 @@ class File(BaseModel):
|
||||
Args:
|
||||
brain_id (str): Brain id
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
self.set_file_vectors_ids()
|
||||
# Check if file exists in that brain
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.table("brains_vectors")
|
||||
supabase_client.table("brains_vectors")
|
||||
.select("brain_id, vector_id")
|
||||
.filter("brain_id", "eq", brain_id)
|
||||
.filter("file_sha1", "eq", self.file_sha1)
|
||||
|
@ -33,6 +33,15 @@ class CommonDependencies(TypedDict):
|
||||
summaries_vector_store: SupabaseVectorStore
|
||||
|
||||
|
||||
def get_supabase_client() -> Client:
|
||||
settings = BrainSettings() # pyright: ignore reportPrivateUsage=none
|
||||
supabase_client: Client = create_client(
|
||||
settings.supabase_url, settings.supabase_service_key
|
||||
)
|
||||
|
||||
return supabase_client
|
||||
|
||||
|
||||
def common_dependencies() -> CommonDependencies:
|
||||
settings = BrainSettings() # pyright: ignore reportPrivateUsage=none
|
||||
embeddings = OpenAIEmbeddings(
|
||||
@ -52,7 +61,7 @@ def common_dependencies() -> CommonDependencies:
|
||||
"supabase": supabase_client,
|
||||
"embeddings": embeddings,
|
||||
"documents_vector_store": documents_vector_store,
|
||||
"summaries_vector_store": summaries_vector_store,
|
||||
"summaries_vector_store": summaries_vector_store, # delete
|
||||
}
|
||||
|
||||
|
||||
|
@ -2,10 +2,9 @@ from typing import Optional
|
||||
from uuid import UUID
|
||||
|
||||
from logger import get_logger
|
||||
from models.settings import get_supabase_client
|
||||
from pydantic import BaseModel
|
||||
|
||||
from models.settings import common_dependencies
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
@ -23,12 +22,11 @@ class User(BaseModel):
|
||||
Args:
|
||||
date (str): Date of the request
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
logger.info(f"New user entry in db document for user {self.email}")
|
||||
|
||||
return (
|
||||
commons["supabase"]
|
||||
.table("users")
|
||||
supabase_client.table("users")
|
||||
.insert(
|
||||
{
|
||||
"user_id": self.id,
|
||||
@ -44,10 +42,9 @@ class User(BaseModel):
|
||||
"""
|
||||
Fetch the user request stats from the database
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
requests_stats = (
|
||||
commons["supabase"]
|
||||
.from_("users")
|
||||
supabase_client.from_("users")
|
||||
.select("*")
|
||||
.filter("user_id", "eq", self.id)
|
||||
.execute()
|
||||
@ -58,10 +55,9 @@ class User(BaseModel):
|
||||
"""
|
||||
Fetch the user request count from the database
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.from_("users")
|
||||
supabase_client.from_("users")
|
||||
.select("*")
|
||||
.filter("user_id", "eq", self.id)
|
||||
.filter("date", "eq", date)
|
||||
@ -75,10 +71,10 @@ class User(BaseModel):
|
||||
"""
|
||||
Increment the user request count in the database
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
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()
|
||||
supabase_client.table("users").update({"requests_count": requests_count}).match(
|
||||
{"user_id": self.id, "date": date}
|
||||
).execute()
|
||||
self.requests_count = requests_count
|
||||
|
@ -1,12 +1,12 @@
|
||||
from uuid import UUID
|
||||
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
|
||||
|
||||
def update_brain_user_rights(brain_id: UUID, user_id: UUID, rights: str) -> None:
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
|
||||
commons["supabase"].table("brains_users").update({"rights": rights}).eq(
|
||||
supabase_client.table("brains_users").update({"rights": rights}).eq(
|
||||
"brain_id",
|
||||
brain_id,
|
||||
).eq("user_id", user_id).execute()
|
||||
|
@ -2,7 +2,7 @@ from typing import Optional
|
||||
|
||||
from logger import get_logger
|
||||
from models.brains_subscription_invitations import BrainSubscription
|
||||
from models.settings import CommonsDep, common_dependencies
|
||||
from models.settings import CommonsDep, get_supabase_client
|
||||
from utils.db_commands import (
|
||||
delete_data_in_table,
|
||||
insert_data_in_table,
|
||||
@ -15,7 +15,7 @@ logger = get_logger(__name__)
|
||||
|
||||
class SubscriptionInvitationService:
|
||||
def __init__(self, commons: Optional[CommonsDep] = None):
|
||||
self.commons = common_dependencies()
|
||||
self.supabase_client = get_supabase_client()
|
||||
|
||||
def create_subscription_invitation(self, brain_subscription: BrainSubscription):
|
||||
logger.info("Creating subscription invitation")
|
||||
@ -25,7 +25,7 @@ class SubscriptionInvitationService:
|
||||
"rights": brain_subscription.rights,
|
||||
}
|
||||
response_data = insert_data_in_table(
|
||||
supabase_client=self.commons["supabase"],
|
||||
supabase_client=self.supabase_client,
|
||||
table_name="brain_subscription_invitations",
|
||||
data=data,
|
||||
message="Creating subscription invitation",
|
||||
@ -36,7 +36,7 @@ class SubscriptionInvitationService:
|
||||
def update_subscription_invitation(self, brain_subscription: BrainSubscription):
|
||||
logger.info("Updating subscription invitation")
|
||||
response_data = update_data_in_table(
|
||||
supabase_client=self.commons["supabase"],
|
||||
supabase_client=self.supabase_client,
|
||||
table_name="brain_subscription_invitations",
|
||||
data={"rights": brain_subscription.rights},
|
||||
identifier={
|
||||
@ -50,7 +50,7 @@ class SubscriptionInvitationService:
|
||||
self, brain_subscription: BrainSubscription
|
||||
):
|
||||
response_data = select_data_in_table(
|
||||
supabase_client=self.commons["supabase"],
|
||||
supabase_client=self.supabase_client,
|
||||
table_name="brain_subscription_invitations",
|
||||
identifier={
|
||||
"brain_id": str(brain_subscription.brain_id),
|
||||
@ -68,7 +68,7 @@ class SubscriptionInvitationService:
|
||||
def fetch_invitation(self, subscription: BrainSubscription):
|
||||
logger.info("Fetching subscription invitation")
|
||||
response_data = select_data_in_table(
|
||||
supabase_client=self.commons["supabase"],
|
||||
supabase_client=self.supabase_client,
|
||||
table_name="brain_subscription_invitations",
|
||||
identifier={
|
||||
"brain_id": str(subscription.brain_id),
|
||||
@ -85,7 +85,7 @@ class SubscriptionInvitationService:
|
||||
f"Removing subscription invitation for email {subscription.email} and brain {subscription.brain_id}"
|
||||
)
|
||||
response_data = delete_data_in_table(
|
||||
supabase_client=self.commons["supabase"],
|
||||
supabase_client=self.supabase_client,
|
||||
table_name="brain_subscription_invitations",
|
||||
identifier={
|
||||
"brain_id": str(subscription.brain_id),
|
||||
|
@ -3,7 +3,7 @@ from uuid import UUID
|
||||
|
||||
from logger import get_logger
|
||||
from models.chat import Chat
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@ -17,7 +17,7 @@ class CreateChatProperties:
|
||||
|
||||
|
||||
def create_chat(user_id: UUID, chat_data: CreateChatProperties) -> Chat:
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
|
||||
# Chat is created upon the user's first question asked
|
||||
logger.info(f"New chat entry in chats table for user {user_id}")
|
||||
@ -27,7 +27,7 @@ def create_chat(user_id: UUID, chat_data: CreateChatProperties) -> Chat:
|
||||
"user_id": str(user_id),
|
||||
"chat_name": chat_data.name,
|
||||
}
|
||||
insert_response = commons["supabase"].table("chats").insert(new_chat).execute()
|
||||
insert_response = supabase_client.table("chats").insert(new_chat).execute()
|
||||
logger.info(f"Insert response {insert_response.data}")
|
||||
|
||||
return insert_response.data[0]
|
||||
|
@ -1,13 +1,12 @@
|
||||
from models.chat import Chat
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
|
||||
|
||||
def get_chat_by_id(chat_id: str) -> Chat:
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.from_("chats")
|
||||
supabase_client.from_("chats")
|
||||
.select("*")
|
||||
.filter("chat_id", "eq", chat_id)
|
||||
.execute()
|
||||
|
@ -1,14 +1,13 @@
|
||||
from typing import List # For type hinting
|
||||
|
||||
from models.chat import ChatHistory
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
|
||||
|
||||
def get_chat_history(chat_id: str) -> List[ChatHistory]:
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
history: List[ChatHistory] = (
|
||||
commons["supabase"]
|
||||
.from_("chat_history")
|
||||
supabase_client.from_("chat_history")
|
||||
.select("*")
|
||||
.filter("chat_id", "eq", chat_id)
|
||||
.order("message_time", desc=False) # Add the ORDER BY clause
|
||||
|
@ -1,14 +1,13 @@
|
||||
from typing import List
|
||||
|
||||
from models.chat import Chat
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
|
||||
|
||||
def get_user_chats(user_id: str) -> List[Chat]:
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.from_("chats")
|
||||
supabase_client.from_("chats")
|
||||
.select("chat_id,user_id,creation_time,chat_name")
|
||||
.filter("user_id", "eq", user_id)
|
||||
.execute()
|
||||
|
@ -3,7 +3,7 @@ from typing import Optional
|
||||
|
||||
from logger import get_logger
|
||||
from models.chat import Chat
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@ -17,7 +17,7 @@ class ChatUpdatableProperties:
|
||||
|
||||
|
||||
def update_chat(chat_id, chat_data: ChatUpdatableProperties) -> Chat:
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
|
||||
if not chat_id:
|
||||
logger.error("No chat_id provided")
|
||||
@ -32,8 +32,7 @@ def update_chat(chat_id, chat_data: ChatUpdatableProperties) -> Chat:
|
||||
|
||||
if updates:
|
||||
updated_chat = (
|
||||
commons["supabase"]
|
||||
.table("chats")
|
||||
supabase_client.table("chats")
|
||||
.update(updates)
|
||||
.match({"chat_id": chat_id})
|
||||
.execute()
|
||||
|
@ -2,14 +2,13 @@ from typing import List # For type hinting
|
||||
|
||||
from fastapi import HTTPException
|
||||
from models.chat import ChatHistory
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
|
||||
|
||||
def update_chat_history(chat_id: str, user_message: str, assistant: str) -> ChatHistory:
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
response: List[ChatHistory] = (
|
||||
commons["supabase"]
|
||||
.table("chat_history")
|
||||
supabase_client.table("chat_history")
|
||||
.insert(
|
||||
{
|
||||
"chat_id": str(chat_id),
|
||||
|
@ -1,6 +1,6 @@
|
||||
from logger import get_logger
|
||||
from models.chat import ChatHistory
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@ -10,7 +10,7 @@ def update_message_by_id(
|
||||
user_message: str = None, # pyright: ignore reportPrivateUsage=none
|
||||
assistant: str = None, # pyright: ignore reportPrivateUsage=none
|
||||
) -> ChatHistory:
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
|
||||
if not message_id:
|
||||
logger.error("No message_id provided")
|
||||
@ -28,8 +28,7 @@ def update_message_by_id(
|
||||
|
||||
if updates:
|
||||
updated_message = (
|
||||
commons["supabase"]
|
||||
.table("chat_history")
|
||||
supabase_client.table("chat_history")
|
||||
.update(updates)
|
||||
.match({"message_id": message_id})
|
||||
.execute()
|
||||
|
@ -1,13 +1,11 @@
|
||||
from uuid import UUID
|
||||
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
|
||||
|
||||
def get_user_email_by_user_id(user_id: UUID) -> str:
|
||||
commons = common_dependencies()
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.rpc("get_user_email_by_user_id", {"user_id": user_id})
|
||||
.execute()
|
||||
)
|
||||
supabase_client = get_supabase_client()
|
||||
response = supabase_client.rpc(
|
||||
"get_user_email_by_user_id", {"user_id": user_id}
|
||||
).execute()
|
||||
return response.data[0]["email"]
|
||||
|
@ -1,13 +1,11 @@
|
||||
from uuid import UUID
|
||||
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
|
||||
|
||||
def get_user_id_by_user_email(email: str) -> UUID:
|
||||
commons = common_dependencies()
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.rpc("get_user_id_by_user_email", {"user_email": email})
|
||||
.execute()
|
||||
)
|
||||
supabase_client = get_supabase_client()
|
||||
response = supabase_client.rpc(
|
||||
"get_user_id_by_user_email", {"user_email": email}
|
||||
).execute()
|
||||
return response.data[0]["user_id"]
|
||||
|
@ -1,13 +1,13 @@
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
from models.user_identity import UserIdentity
|
||||
|
||||
|
||||
def create_user_identity(user_identity: UserIdentity) -> UserIdentity:
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
user_identity_dict = user_identity.dict()
|
||||
user_identity_dict["user_id"] = str(user_identity.user_id)
|
||||
response = (
|
||||
commons["supabase"].from_("user_identity").insert(user_identity_dict).execute()
|
||||
supabase_client.from_("user_identity").insert(user_identity_dict).execute()
|
||||
)
|
||||
|
||||
return UserIdentity(**response.data[0])
|
||||
|
@ -1,15 +1,14 @@
|
||||
from uuid import UUID
|
||||
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
from models.user_identity import UserIdentity
|
||||
from repository.user_identity.create_user_identity import create_user_identity
|
||||
|
||||
|
||||
def get_user_identity(user_id: UUID) -> UserIdentity:
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.from_("user_identity")
|
||||
supabase_client.from_("user_identity")
|
||||
.select("*")
|
||||
.filter("user_id", "eq", user_id)
|
||||
.execute()
|
||||
|
@ -1,12 +1,10 @@
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
from models.user_identity import UserIdentity
|
||||
from pydantic import BaseModel
|
||||
from repository.user_identity.create_user_identity import (
|
||||
create_user_identity,
|
||||
)
|
||||
from repository.user_identity.create_user_identity import create_user_identity
|
||||
|
||||
|
||||
class UserIdentityUpdatableProperties(BaseModel):
|
||||
@ -17,10 +15,9 @@ def update_user_identity(
|
||||
user_id: UUID,
|
||||
user_identity_updatable_properties: UserIdentityUpdatableProperties,
|
||||
) -> UserIdentity:
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.from_("user_identity")
|
||||
supabase_client.from_("user_identity")
|
||||
.update(user_identity_updatable_properties.__dict__)
|
||||
.filter("user_id", "eq", user_id)
|
||||
.execute()
|
||||
|
@ -12,7 +12,7 @@ from llm.openai import OpenAIBrainPicking
|
||||
from models.brains import Brain, get_default_user_brain_or_create_new
|
||||
from models.chat import Chat, ChatHistory
|
||||
from models.chats import ChatQuestion
|
||||
from models.settings import LLMSettings, common_dependencies
|
||||
from models.settings import LLMSettings, get_supabase_client
|
||||
from models.users import User
|
||||
from repository.chat.create_chat import CreateChatProperties, create_chat
|
||||
from repository.chat.get_chat_by_id import get_chat_by_id
|
||||
@ -20,6 +20,7 @@ from repository.chat.get_chat_history import get_chat_history
|
||||
from repository.chat.get_user_chats import get_user_chats
|
||||
from repository.chat.update_chat import ChatUpdatableProperties, update_chat
|
||||
from repository.user_identity.get_user_identity import get_user_identity
|
||||
from supabase.client import Client
|
||||
|
||||
chat_router = APIRouter()
|
||||
|
||||
@ -39,47 +40,21 @@ class NullableUUID(UUID):
|
||||
return None
|
||||
|
||||
|
||||
def get_chat_details(commons, chat_id):
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.from_("chats")
|
||||
.select("*")
|
||||
.filter("chat_id", "eq", chat_id)
|
||||
.execute()
|
||||
)
|
||||
return response.data
|
||||
|
||||
|
||||
def delete_chat_from_db(commons, chat_id):
|
||||
def delete_chat_from_db(supabase_client: Client, chat_id: str):
|
||||
try:
|
||||
commons["supabase"].table("chat_history").delete().match(
|
||||
supabase_client.table("chat_history").delete().match(
|
||||
{"chat_id": chat_id}
|
||||
).execute()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
pass
|
||||
try:
|
||||
commons["supabase"].table("chats").delete().match(
|
||||
{"chat_id": chat_id}
|
||||
).execute()
|
||||
supabase_client.table("chats").delete().match({"chat_id": chat_id}).execute()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
pass
|
||||
|
||||
|
||||
def fetch_user_stats(commons, user, date):
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.from_("users")
|
||||
.select("*")
|
||||
.filter("email", "eq", user.email)
|
||||
.filter("date", "eq", date)
|
||||
.execute()
|
||||
)
|
||||
userItem = next(iter(response.data or []), {"requests_count": 0})
|
||||
return userItem
|
||||
|
||||
|
||||
def check_user_limit(
|
||||
user: User,
|
||||
):
|
||||
@ -121,8 +96,8 @@ async def delete_chat(chat_id: UUID):
|
||||
"""
|
||||
Delete a specific chat by chat ID.
|
||||
"""
|
||||
commons = common_dependencies()
|
||||
delete_chat_from_db(commons, chat_id)
|
||||
supabase_client = get_supabase_client()
|
||||
delete_chat_from_db(supabase_client, chat_id)
|
||||
return {"message": f"{chat_id} has been deleted."}
|
||||
|
||||
|
||||
|
@ -3,9 +3,8 @@ from uuid import UUID
|
||||
from auth import AuthBearer, get_current_user
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from models.brains import Brain
|
||||
from models.settings import common_dependencies
|
||||
from models.settings import get_supabase_client
|
||||
from models.users import User
|
||||
|
||||
from routes.authorizations.brain_authorization import (
|
||||
RoleEnum,
|
||||
has_brain_authorization,
|
||||
@ -64,10 +63,9 @@ async def download_endpoint(
|
||||
"""
|
||||
# check if user has the right to get the file: add brain_id to the query
|
||||
|
||||
commons = common_dependencies()
|
||||
supabase_client = get_supabase_client()
|
||||
response = (
|
||||
commons["supabase"]
|
||||
.table("vectors")
|
||||
supabase_client.table("vectors")
|
||||
.select(
|
||||
"metadata->>file_name, metadata->>file_size, metadata->>file_extension, metadata->>file_url",
|
||||
"content",
|
||||
|
@ -1,18 +0,0 @@
|
||||
from logger import get_logger
|
||||
from models.settings import CommonsDep
|
||||
from models.users import User
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
def create_user(commons: CommonsDep, user: User, date):
|
||||
logger.info(f"New user entry in db document for user {user.email}")
|
||||
|
||||
return (
|
||||
commons["supabase"]
|
||||
.table("users")
|
||||
.insert(
|
||||
{"user_id": user.id, "email": user.email, "date": date, "requests_count": 1}
|
||||
)
|
||||
.execute()
|
||||
)
|
@ -3,7 +3,7 @@ from typing import List
|
||||
|
||||
from langchain.embeddings.openai import OpenAIEmbeddings
|
||||
from logger import get_logger
|
||||
from models.settings import BrainSettings, CommonsDep, common_dependencies
|
||||
from models.settings import BrainSettings, CommonsDep, get_supabase_client
|
||||
from pydantic import BaseModel
|
||||
|
||||
logger = get_logger(__name__)
|
||||
@ -48,19 +48,17 @@ class Neurons(BaseModel):
|
||||
return summaries.data
|
||||
|
||||
|
||||
|
||||
|
||||
def error_callback(exception):
|
||||
print("An exception occurred:", exception)
|
||||
|
||||
|
||||
def process_batch(batch_ids: List[str]):
|
||||
commons = common_dependencies()
|
||||
supabase = commons["supabase"]
|
||||
supabase_client = get_supabase_client()
|
||||
|
||||
try:
|
||||
if len(batch_ids) == 1:
|
||||
return (
|
||||
supabase.table("vectors")
|
||||
supabase_client.table("vectors")
|
||||
.select(
|
||||
"name:metadata->>file_name, size:metadata->>file_size",
|
||||
count="exact",
|
||||
@ -70,7 +68,7 @@ def process_batch(batch_ids: List[str]):
|
||||
).data
|
||||
else:
|
||||
return (
|
||||
supabase.table("vectors")
|
||||
supabase_client.table("vectors")
|
||||
.select(
|
||||
"name:metadata->>file_name, size:metadata->>file_size",
|
||||
count="exact",
|
||||
|
Loading…
Reference in New Issue
Block a user