-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_utils.py
37 lines (30 loc) · 1.17 KB
/
db_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from functools import lru_cache
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from qdrant_client import QdrantClient
from dotenv import load_dotenv
from loguru import logger
from schemas import Base
load_dotenv()
@lru_cache(maxsize=1)
def init_qdrant_client() -> QdrantClient:
logger.info("Initializing qdrant client")
qdrant_host = os.environ.get("QDRANT_HOST", None)
qdrant_port = os.environ.get("QDRANT_PORT", None)
qdrant_path = os.environ.get("QDRANT_PATH", None)
qdrant_timeout = float(os.environ.get("QDRANT_TIMEOUT", 5.0))
qd_client = QdrantClient(
host=qdrant_host, port=qdrant_port, path=qdrant_path, timeout=qdrant_timeout
)
logger.success("Initialized qdrant client")
return qd_client
def init_sql_session() -> sessionmaker:
logger.info("Initializing SQL session")
db_url = os.environ.get("DB_URL", "sqlite:///sonaveeb.db")
engine = create_engine(db_url, connect_args={"check_same_thread": False})
Base.metadata.create_all(engine)
session_maker = sessionmaker()
session_maker.configure(bind=engine)
logger.info("SQL session initialized")
return session_maker