-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug_bonsai_script.py
More file actions
70 lines (63 loc) · 2.6 KB
/
debug_bonsai_script.py
File metadata and controls
70 lines (63 loc) · 2.6 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
from opensearchpy import OpenSearch
import requests
# Credentials from config
ES_HOST = "assertive-mahogany-1m2hcasg.us-east-1.bonsaisearch.net"
ES_USER = "0204784e62"
ES_PASS = "38aa998d6c5c2891232c"
ES_URL = f"https://{ES_USER}:{ES_PASS}@{ES_HOST}:443"
def debug_scripts():
try:
client = OpenSearch([ES_URL], use_ssl=True, verify_certs=True)
info = client.info()
print(f"Version: {info['version']['number']}")
print(f"Distribution: {info['version'].get('distribution', 'elasticsearch')}")
# Test a simple script score with cosineSimilarity
test_query = {
"size": 1,
"query": {
"script_score": {
"query": {"match_all": {}},
"script": {
"source": "cosineSimilarity(params.query_vector, 'embedding') + 1.0",
"params": {"query_vector": [0.1] * 768}
}
}
}
}
print("Testing cosineSimilarity script...")
try:
resp = client.search(index="clinical_trials", body=test_query)
print("✅ cosineSimilarity works!")
except Exception as e:
print(f"❌ cosineSimilarity failed: {e}")
# Try manual dot product as fallback test
print("Testing manual dot product script...")
manual_script = {
"size": 1,
"query": {
"script_score": {
"query": {"match_all": {}},
"script": {
"source": """
double dot = 0;
if (doc.containsKey('embedding') && doc['embedding'].size() > 0) {
for (int i = 0; i < params.query_vector.length; i++) {
dot += params.query_vector[i] * doc['embedding'][i];
}
}
return dot + 1.0;
""",
"params": {"query_vector": [0.1] * 768}
}
}
}
}
try:
resp = client.search(index="clinical_trials", body=manual_script)
print("✅ Manual dot product works!")
except Exception as e2:
print(f"❌ Manual dot product failed: {e2}")
except Exception as e:
print(f"General error: {e}")
if __name__ == "__main__":
debug_scripts()