mirror of
https://github.com/QuivrHQ/quivr.git
synced 2025-01-05 23:03:53 +03:00
feat(backend): use SQLAlchemy instead od supabase API (#2516)
# Description Please include a summary of the changes and the related issue. Please also include relevant motivation and context. ## Checklist before requesting a review Please delete options that are not relevant. - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented hard-to-understand areas - [ ] I have ideally added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged ## Screenshots (if appropriate):
This commit is contained in:
parent
89f86d4e68
commit
fb3aae27f1
@ -3,6 +3,7 @@ from uuid import UUID
|
|||||||
|
|
||||||
from langchain.embeddings.ollama import OllamaEmbeddings
|
from langchain.embeddings.ollama import OllamaEmbeddings
|
||||||
from langchain_openai import OpenAIEmbeddings
|
from langchain_openai import OpenAIEmbeddings
|
||||||
|
from sqlalchemy import Engine, create_engine
|
||||||
from logger import get_logger
|
from logger import get_logger
|
||||||
from models.databases.supabase.supabase import SupabaseDB
|
from models.databases.supabase.supabase import SupabaseDB
|
||||||
from posthog import Posthog
|
from posthog import Posthog
|
||||||
@ -114,6 +115,7 @@ class BrainSettings(BaseSettings):
|
|||||||
ollama_api_base_url: str = None
|
ollama_api_base_url: str = None
|
||||||
langfuse_public_key: str = None
|
langfuse_public_key: str = None
|
||||||
langfuse_secret_key: str = None
|
langfuse_secret_key: str = None
|
||||||
|
pg_database_url: str = None
|
||||||
|
|
||||||
|
|
||||||
class ResendSettings(BaseSettings):
|
class ResendSettings(BaseSettings):
|
||||||
@ -124,7 +126,15 @@ class ResendSettings(BaseSettings):
|
|||||||
# Global variables to store the Supabase client and database instances
|
# Global variables to store the Supabase client and database instances
|
||||||
_supabase_client: Optional[Client] = None
|
_supabase_client: Optional[Client] = None
|
||||||
_supabase_db: Optional[SupabaseDB] = None
|
_supabase_db: Optional[SupabaseDB] = None
|
||||||
|
_db_engine: Optional[Engine] = None
|
||||||
|
|
||||||
|
def get_pg_database_engine():
|
||||||
|
global _db_engine
|
||||||
|
if _db_engine is None:
|
||||||
|
logger.info("Creating Postgres DB engine")
|
||||||
|
settings = BrainSettings() # pyright: ignore reportPrivateUsage=none
|
||||||
|
_db_engine = create_engine(settings.pg_database_url)
|
||||||
|
return _db_engine
|
||||||
|
|
||||||
def get_supabase_client() -> Client:
|
def get_supabase_client() -> Client:
|
||||||
global _supabase_client
|
global _supabase_client
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
@ -26,7 +27,7 @@ class BrainEntity(BaseModel):
|
|||||||
max_tokens: Optional[int] = None
|
max_tokens: Optional[int] = None
|
||||||
status: Optional[str] = None
|
status: Optional[str] = None
|
||||||
prompt_id: Optional[UUID] = None
|
prompt_id: Optional[UUID] = None
|
||||||
last_update: str
|
last_update: datetime
|
||||||
brain_type: BrainType
|
brain_type: BrainType
|
||||||
brain_definition: Optional[ApiBrainDefinitionEntity] = None
|
brain_definition: Optional[ApiBrainDefinitionEntity] = None
|
||||||
connected_brains_ids: Optional[List[UUID]] = None
|
connected_brains_ids: Optional[List[UUID]] = None
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
from logger import get_logger
|
from logger import get_logger
|
||||||
from models.settings import get_embeddings, get_supabase_client
|
from models.settings import get_embeddings, get_pg_database_engine, get_supabase_client
|
||||||
from modules.brain.dto.inputs import BrainUpdatableProperties
|
from modules.brain.dto.inputs import BrainUpdatableProperties
|
||||||
from modules.brain.entity.brain_entity import BrainEntity, PublicBrain
|
from modules.brain.entity.brain_entity import BrainEntity, PublicBrain
|
||||||
from modules.brain.repository.interfaces.brains_interface import BrainsInterface
|
from modules.brain.repository.interfaces.brains_interface import BrainsInterface
|
||||||
@ -13,6 +15,8 @@ class Brains(BrainsInterface):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
supabase_client = get_supabase_client()
|
supabase_client = get_supabase_client()
|
||||||
self.db = supabase_client
|
self.db = supabase_client
|
||||||
|
pg_engine = get_pg_database_engine()
|
||||||
|
self.pg_engine = pg_engine
|
||||||
|
|
||||||
def create_brain(self, brain):
|
def create_brain(self, brain):
|
||||||
embeddings = get_embeddings()
|
embeddings = get_embeddings()
|
||||||
@ -54,27 +58,32 @@ class Brains(BrainsInterface):
|
|||||||
|
|
||||||
def update_brain_last_update_time(self, brain_id):
|
def update_brain_last_update_time(self, brain_id):
|
||||||
try:
|
try:
|
||||||
self.db.table("brains").update({"last_update": "now()"}).match(
|
with self.pg_engine.begin() as connection:
|
||||||
{"brain_id": brain_id}
|
query = """
|
||||||
).execute()
|
UPDATE brains
|
||||||
|
SET last_update = now()
|
||||||
|
WHERE brain_id = '{brain_id}'
|
||||||
|
"""
|
||||||
|
connection.execute(text(query.format(brain_id=brain_id)))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
|
|
||||||
def get_brain_details(self, brain_id):
|
def get_brain_details(self, brain_id):
|
||||||
response = (
|
with self.pg_engine.begin() as connection:
|
||||||
self.db.table("brains")
|
query = """
|
||||||
.select("*")
|
SELECT * FROM brains
|
||||||
.filter("brain_id", "eq", str(brain_id))
|
WHERE brain_id = '{brain_id}'
|
||||||
.execute()
|
"""
|
||||||
)
|
response = connection.execute(text(query.format(brain_id=brain_id))).fetchall()
|
||||||
if response.data == []:
|
if len(response) == 0:
|
||||||
return None
|
return None
|
||||||
return BrainEntity(**response.data[0])
|
return BrainEntity(**response[0]._mapping)
|
||||||
|
|
||||||
def delete_brain(self, brain_id: str):
|
def delete_brain(self, brain_id: str):
|
||||||
results = (
|
with self.pg_engine.begin() as connection:
|
||||||
self.db.table("brains").delete().match({"brain_id": brain_id}).execute()
|
results = connection.execute(
|
||||||
)
|
text(f"DELETE FROM brains WHERE brain_id = '{brain_id}'")
|
||||||
|
)
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
@ -100,14 +109,11 @@ class Brains(BrainsInterface):
|
|||||||
|
|
||||||
def get_brain_by_id(self, brain_id: UUID) -> BrainEntity | None:
|
def get_brain_by_id(self, brain_id: UUID) -> BrainEntity | None:
|
||||||
# TODO: merge this method with get_brain_details
|
# TODO: merge this method with get_brain_details
|
||||||
response = (
|
with self.pg_engine.begin() as connection:
|
||||||
self.db.from_("brains")
|
response = connection.execute(
|
||||||
.select("id:brain_id, name, *")
|
text(f"SELECT * FROM brains WHERE brain_id = '{brain_id}'")
|
||||||
.filter("brain_id", "eq", brain_id)
|
).fetchall()
|
||||||
.execute()
|
|
||||||
).data
|
|
||||||
|
|
||||||
if len(response) == 0:
|
if len(response) == 0:
|
||||||
return None
|
return None
|
||||||
|
return BrainEntity(**response[0]._mapping)
|
||||||
return BrainEntity(**response[0])
|
|
Loading…
Reference in New Issue
Block a user