-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_rerank_binding.py
More file actions
71 lines (63 loc) · 2.24 KB
/
Copy pathdebug_rerank_binding.py
File metadata and controls
71 lines (63 loc) · 2.24 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import sqlalchemy
from sqlalchemy import text
from google.cloud.alloydb.connector import Connector, IPTypes
PROJECT_ID = "alloydbtest-374215"
REGION = "us-central1"
CLUSTER = "hybridsearch"
INSTANCE = "hybridsearch-primary"
DATABASE = "ecom"
DB_USER = "postgres"
DB_PASS = os.environ.get("DB_PASSWORD", "Qbertq!1")
def debug_binding():
connector = Connector()
pool = sqlalchemy.create_engine(
"postgresql+pg8000://",
creator=lambda: connector.connect(
f"projects/{PROJECT_ID}/locations/{REGION}/clusters/{CLUSTER}/instances/{INSTANCE}",
"pg8000",
user=DB_USER,
password=DB_PASS,
db=DATABASE,
ip_type=IPTypes.PUBLIC
)
)
try:
with pool.connect() as conn:
print("Testing binding list to text[]...")
query = text("""
SELECT * FROM ai.rank(
model_id => 'semantic-ranker-512@latest',
search_string => :query,
documents => :docs
)
""")
docs = ["red dress", "blue shoes"]
q = "dress"
# SQLAlchemy might need type hint or specific cast
try:
result = conn.execute(query, {"query": q, "docs": docs}).fetchall()
print("Success!")
for row in result:
print(row)
except Exception as e:
print(f"Binding failed: {e}")
print("\nTesting CAST(:docs AS text[])...")
query_cast = text("""
SELECT * FROM ai.rank(
model_id => 'semantic-ranker-512@latest',
search_string => :query,
documents => CAST(:docs AS text[])
)
""")
try:
result = conn.execute(query_cast, {"query": q, "docs": docs}).fetchall()
print("Success with CAST!")
for row in result:
print(row)
except Exception as e:
print(f"CAST also failed: {e}")
except Exception as e:
print(f"Connection Error: {e}")
if __name__ == "__main__":
debug_binding()