Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
remove connection pooling; fix user query storage
Browse files
src/db.py
CHANGED
|
@@ -27,26 +27,6 @@ _secret_dict = json.loads(_secret_value["SecretString"])
|
|
| 27 |
_reader_host = os.getenv("RDS_READER_HOST")
|
| 28 |
_writer_host = os.getenv("RDS_WRITER_HOST")
|
| 29 |
|
| 30 |
-
_reader_pool = SimpleConnectionPool(
|
| 31 |
-
1, 10,
|
| 32 |
-
host=_reader_host,
|
| 33 |
-
port=int(_secret_dict.get("port", 5432)),
|
| 34 |
-
dbname=_dbname or _secret_dict.get("dbname"),
|
| 35 |
-
user=_secret_dict["username"],
|
| 36 |
-
password=_secret_dict["password"],
|
| 37 |
-
sslmode="require",
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
_writer_pool = SimpleConnectionPool(
|
| 41 |
-
1, 5,
|
| 42 |
-
host=_writer_host,
|
| 43 |
-
port=int(_secret_dict.get("port", 5432)),
|
| 44 |
-
dbname=_dbname or _secret_dict.get("dbname"),
|
| 45 |
-
user=_secret_dict["username"],
|
| 46 |
-
password=_secret_dict["password"],
|
| 47 |
-
sslmode="require",
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
def embed_query(query: str):
|
| 51 |
response = _openai_client.embeddings.create(
|
| 52 |
model="Qwen/Qwen3-Embedding-8B",
|
|
@@ -59,9 +39,9 @@ def cached_embed(query):
|
|
| 59 |
return embed_query(query)
|
| 60 |
|
| 61 |
@contextmanager
|
| 62 |
-
def
|
| 63 |
with psycopg2.connect(
|
| 64 |
-
host=
|
| 65 |
port=int(_secret_dict.get("port", 5432)),
|
| 66 |
dbname=_dbname or _secret_dict.get("dbname"),
|
| 67 |
user=_secret_dict["username"],
|
|
@@ -71,23 +51,18 @@ def get_rds_conn(host: str):
|
|
| 71 |
register_vector(conn)
|
| 72 |
yield conn
|
| 73 |
|
| 74 |
-
@contextmanager
|
| 75 |
-
def reader_conn():
|
| 76 |
-
conn = _reader_pool.getconn()
|
| 77 |
-
try:
|
| 78 |
-
register_vector(conn)
|
| 79 |
-
yield conn
|
| 80 |
-
finally:
|
| 81 |
-
_reader_pool.putconn(conn)
|
| 82 |
-
|
| 83 |
@contextmanager
|
| 84 |
def writer_conn():
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
register_vector(conn)
|
| 88 |
yield conn
|
| 89 |
-
finally:
|
| 90 |
-
_writer_pool.putconn(conn)
|
| 91 |
|
| 92 |
@st.cache_data(ttl=60*60*24*7)
|
| 93 |
def load_sources():
|
|
@@ -192,7 +167,6 @@ def fetch_candidate_ids(
|
|
| 192 |
extra_where = " AND " + " AND ".join(filter_clauses)
|
| 193 |
|
| 194 |
with reader_conn() as conn, conn.cursor() as cur:
|
| 195 |
-
# Tune these
|
| 196 |
per_source_multiplier = 3
|
| 197 |
ef_search = max(80, top_k * 4)
|
| 198 |
|
|
|
|
| 27 |
_reader_host = os.getenv("RDS_READER_HOST")
|
| 28 |
_writer_host = os.getenv("RDS_WRITER_HOST")
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def embed_query(query: str):
|
| 31 |
response = _openai_client.embeddings.create(
|
| 32 |
model="Qwen/Qwen3-Embedding-8B",
|
|
|
|
| 39 |
return embed_query(query)
|
| 40 |
|
| 41 |
@contextmanager
|
| 42 |
+
def reader_conn():
|
| 43 |
with psycopg2.connect(
|
| 44 |
+
host=_reader_host,
|
| 45 |
port=int(_secret_dict.get("port", 5432)),
|
| 46 |
dbname=_dbname or _secret_dict.get("dbname"),
|
| 47 |
user=_secret_dict["username"],
|
|
|
|
| 51 |
register_vector(conn)
|
| 52 |
yield conn
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
@contextmanager
|
| 55 |
def writer_conn():
|
| 56 |
+
with psycopg2.connect(
|
| 57 |
+
host=_writer_host,
|
| 58 |
+
port=int(_secret_dict.get("port", 5432)),
|
| 59 |
+
dbname=_dbname or _secret_dict.get("dbname"),
|
| 60 |
+
user=_secret_dict["username"],
|
| 61 |
+
password=_secret_dict["password"],
|
| 62 |
+
sslmode="require",
|
| 63 |
+
) as conn:
|
| 64 |
register_vector(conn)
|
| 65 |
yield conn
|
|
|
|
|
|
|
| 66 |
|
| 67 |
@st.cache_data(ttl=60*60*24*7)
|
| 68 |
def load_sources():
|
|
|
|
| 167 |
extra_where = " AND " + " AND ".join(filter_clauses)
|
| 168 |
|
| 169 |
with reader_conn() as conn, conn.cursor() as cur:
|
|
|
|
| 170 |
per_source_multiplier = 3
|
| 171 |
ef_search = max(80, top_k * 4)
|
| 172 |
|