ragaai-catalyst 2.2.1__py3-none-any.whl → 2.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ragaai_catalyst/ragaai_catalyst.py +119 -82
- ragaai_catalyst/tracers/agentic_tracing/upload/trace_uploader.py +14 -15
- ragaai_catalyst/tracers/agentic_tracing/upload/upload_agentic_traces.py +132 -67
- ragaai_catalyst/tracers/agentic_tracing/upload/upload_code.py +156 -72
- ragaai_catalyst/tracers/tracer.py +67 -39
- {ragaai_catalyst-2.2.1.dist-info → ragaai_catalyst-2.2.2.dist-info}/METADATA +1 -1
- {ragaai_catalyst-2.2.1.dist-info → ragaai_catalyst-2.2.2.dist-info}/RECORD +10 -10
- {ragaai_catalyst-2.2.1.dist-info → ragaai_catalyst-2.2.2.dist-info}/WHEEL +1 -1
- {ragaai_catalyst-2.2.1.dist-info → ragaai_catalyst-2.2.2.dist-info}/licenses/LICENSE +0 -0
- {ragaai_catalyst-2.2.1.dist-info → ragaai_catalyst-2.2.2.dist-info}/top_level.txt +0 -0
@@ -1,25 +1,28 @@
|
|
1
|
-
import requests
|
2
1
|
import json
|
2
|
+
import logging
|
3
3
|
import os
|
4
|
+
import re
|
4
5
|
import time
|
5
|
-
import logging
|
6
|
-
from datetime import datetime
|
7
6
|
from urllib.parse import urlparse, urlunparse
|
8
|
-
|
7
|
+
|
8
|
+
import requests
|
9
9
|
|
10
10
|
logger = logging.getLogger(__name__)
|
11
11
|
|
12
|
+
from ragaai_catalyst.ragaai_catalyst import RagaAICatalyst
|
13
|
+
|
12
14
|
|
13
15
|
class UploadAgenticTraces:
|
14
|
-
def __init__(
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
def __init__(
|
17
|
+
self,
|
18
|
+
json_file_path,
|
19
|
+
project_name,
|
20
|
+
project_id,
|
21
|
+
dataset_name,
|
22
|
+
user_detail,
|
23
|
+
base_url,
|
24
|
+
timeout=120,
|
25
|
+
):
|
23
26
|
self.json_file_path = json_file_path
|
24
27
|
self.project_name = project_name
|
25
28
|
self.project_id = project_id
|
@@ -28,12 +31,13 @@ class UploadAgenticTraces:
|
|
28
31
|
self.base_url = base_url
|
29
32
|
self.timeout = timeout
|
30
33
|
|
31
|
-
|
32
34
|
def _get_presigned_url(self):
|
33
|
-
payload = json.dumps(
|
35
|
+
payload = json.dumps(
|
36
|
+
{
|
34
37
|
"datasetName": self.dataset_name,
|
35
38
|
"numFiles": 1,
|
36
|
-
}
|
39
|
+
}
|
40
|
+
)
|
37
41
|
headers = {
|
38
42
|
"Content-Type": "application/json",
|
39
43
|
"Authorization": f"Bearer {os.getenv('RAGAAI_CATALYST_TOKEN')}",
|
@@ -44,50 +48,82 @@ class UploadAgenticTraces:
|
|
44
48
|
start_time = time.time()
|
45
49
|
endpoint = f"{self.base_url}/v1/llm/presigned-url"
|
46
50
|
# Changed to POST from GET
|
47
|
-
response = requests.request(
|
48
|
-
|
49
|
-
|
50
|
-
data=payload,
|
51
|
-
timeout=self.timeout)
|
51
|
+
response = requests.request(
|
52
|
+
"POST", endpoint, headers=headers, data=payload, timeout=self.timeout
|
53
|
+
)
|
52
54
|
elapsed_ms = (time.time() - start_time) * 1000
|
53
55
|
logger.debug(
|
54
|
-
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
55
|
-
|
56
|
+
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
57
|
+
)
|
58
|
+
|
56
59
|
if response.status_code == 200:
|
57
60
|
presignedURLs = response.json()["data"]["presignedUrls"][0]
|
58
|
-
presignedurl = self.update_presigned_url(presignedURLs,self.base_url)
|
61
|
+
presignedurl = self.update_presigned_url(presignedURLs, self.base_url)
|
59
62
|
return presignedurl
|
60
63
|
else:
|
61
64
|
# If POST fails, try GET
|
62
|
-
response = requests.request(
|
63
|
-
|
64
|
-
|
65
|
-
data=payload,
|
66
|
-
timeout=self.timeout)
|
65
|
+
response = requests.request(
|
66
|
+
"GET", endpoint, headers=headers, data=payload, timeout=self.timeout
|
67
|
+
)
|
67
68
|
elapsed_ms = (time.time() - start_time) * 1000
|
68
69
|
logger.debug(
|
69
|
-
f"API Call: [GET] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
70
|
+
f"API Call: [GET] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
71
|
+
)
|
70
72
|
if response.status_code == 200:
|
71
73
|
presignedURLs = response.json()["data"]["presignedUrls"][0]
|
72
|
-
presignedurl = self.update_presigned_url(
|
74
|
+
presignedurl = self.update_presigned_url(
|
75
|
+
presignedURLs, self.base_url
|
76
|
+
)
|
73
77
|
return presignedurl
|
78
|
+
elif response.status_code == 401:
|
79
|
+
logger.warning("Received 401 error. Attempting to refresh token.")
|
80
|
+
token = RagaAICatalyst.get_token(force_refresh=True)
|
81
|
+
headers = {
|
82
|
+
"Content-Type": "application/json",
|
83
|
+
"Authorization": f"Bearer {token}",
|
84
|
+
"X-Project-Name": self.project_name,
|
85
|
+
}
|
86
|
+
response = requests.request(
|
87
|
+
"POST",
|
88
|
+
endpoint,
|
89
|
+
headers=headers,
|
90
|
+
data=payload,
|
91
|
+
timeout=self.timeout,
|
92
|
+
)
|
93
|
+
elapsed_ms = (time.time() - start_time) * 1000
|
94
|
+
logger.debug(
|
95
|
+
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
96
|
+
)
|
97
|
+
if response.status_code == 200:
|
98
|
+
presignedURLs = response.json()["data"]["presignedUrls"][0]
|
99
|
+
presignedurl = self.update_presigned_url(
|
100
|
+
presignedURLs, self.base_url
|
101
|
+
)
|
102
|
+
return presignedurl
|
103
|
+
else:
|
104
|
+
logger.error(
|
105
|
+
f"Error while getting presigned url: {response.json()['message']}"
|
106
|
+
)
|
107
|
+
return None
|
108
|
+
else:
|
109
|
+
logger.error(
|
110
|
+
f"Error while getting presigned url: {response.json()['message']}"
|
111
|
+
)
|
112
|
+
return None
|
74
113
|
|
75
|
-
logger.error(f"Error while getting presigned url: {response.json()['message']}")
|
76
|
-
return None
|
77
|
-
|
78
114
|
except requests.exceptions.RequestException as e:
|
79
115
|
logger.error(f"Error while getting presigned url: {e}")
|
80
116
|
return None
|
81
|
-
|
117
|
+
|
82
118
|
def update_presigned_url(self, presigned_url, base_url):
|
83
|
-
"""Replaces the domain (and port, if applicable) of the presigned URL
|
119
|
+
"""Replaces the domain (and port, if applicable) of the presigned URL
|
84
120
|
with that of the base URL only if the base URL contains 'localhost' or an IP address."""
|
85
|
-
#To Do: If Proxy URL has domain name how do we handle such cases
|
86
|
-
|
121
|
+
# To Do: If Proxy URL has domain name how do we handle such cases
|
122
|
+
|
87
123
|
presigned_parts = urlparse(presigned_url)
|
88
124
|
base_parts = urlparse(base_url)
|
89
125
|
# Check if base_url contains localhost or an IP address
|
90
|
-
if re.match(r
|
126
|
+
if re.match(r"^(localhost|\d{1,3}(\.\d{1,3}){3})$", base_parts.hostname):
|
91
127
|
new_netloc = base_parts.hostname # Extract domain from base_url
|
92
128
|
if base_parts.port: # Add port if present in base_url
|
93
129
|
new_netloc += f":{base_parts.port}"
|
@@ -97,12 +133,12 @@ class UploadAgenticTraces:
|
|
97
133
|
|
98
134
|
def _put_presigned_url(self, presignedUrl, filename):
|
99
135
|
headers = {
|
100
|
-
|
101
|
-
|
136
|
+
"Content-Type": "application/json",
|
137
|
+
}
|
102
138
|
|
103
139
|
if "blob.core.windows.net" in presignedUrl: # Azure
|
104
140
|
headers["x-ms-blob-type"] = "BlockBlob"
|
105
|
-
print(
|
141
|
+
print("Uploading agentic traces...")
|
106
142
|
try:
|
107
143
|
with open(filename) as f:
|
108
144
|
payload = f.read().replace("\n", "").replace("\r", "").encode()
|
@@ -111,14 +147,13 @@ class UploadAgenticTraces:
|
|
111
147
|
return None
|
112
148
|
try:
|
113
149
|
start_time = time.time()
|
114
|
-
response = requests.request(
|
115
|
-
|
116
|
-
|
117
|
-
data=payload,
|
118
|
-
timeout=self.timeout)
|
150
|
+
response = requests.request(
|
151
|
+
"PUT", presignedUrl, headers=headers, data=payload, timeout=self.timeout
|
152
|
+
)
|
119
153
|
elapsed_ms = (time.time() - start_time) * 1000
|
120
154
|
logger.debug(
|
121
|
-
f"API Call: [PUT] {presignedUrl} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
155
|
+
f"API Call: [PUT] {presignedUrl} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
156
|
+
)
|
122
157
|
if response.status_code != 200 or response.status_code != 201:
|
123
158
|
return response, response.status_code
|
124
159
|
except requests.exceptions.RequestException as e:
|
@@ -127,29 +162,55 @@ class UploadAgenticTraces:
|
|
127
162
|
|
128
163
|
def insert_traces(self, presignedUrl):
|
129
164
|
headers = {
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
payload = json.dumps(
|
165
|
+
"Authorization": f"Bearer {os.getenv('RAGAAI_CATALYST_TOKEN')}",
|
166
|
+
"Content-Type": "application/json",
|
167
|
+
"X-Project-Name": self.project_name,
|
168
|
+
}
|
169
|
+
payload = json.dumps(
|
170
|
+
{
|
135
171
|
"datasetName": self.dataset_name,
|
136
172
|
"presignedUrl": presignedUrl,
|
137
|
-
"datasetSpans": self._get_dataset_spans(),
|
138
|
-
}
|
173
|
+
"datasetSpans": self._get_dataset_spans(), # Extra key for agentic traces
|
174
|
+
}
|
175
|
+
)
|
139
176
|
try:
|
140
177
|
start_time = time.time()
|
141
178
|
endpoint = f"{self.base_url}/v1/llm/insert/trace"
|
142
|
-
response = requests.request(
|
143
|
-
|
144
|
-
|
145
|
-
data=payload,
|
146
|
-
timeout=self.timeout)
|
179
|
+
response = requests.request(
|
180
|
+
"POST", endpoint, headers=headers, data=payload, timeout=self.timeout
|
181
|
+
)
|
147
182
|
elapsed_ms = (time.time() - start_time) * 1000
|
148
183
|
logger.debug(
|
149
|
-
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
184
|
+
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
185
|
+
)
|
150
186
|
if response.status_code != 200:
|
151
187
|
print(f"Error inserting traces: {response.json()['message']}")
|
152
188
|
return None
|
189
|
+
elif response.status_code == 401:
|
190
|
+
logger.warning("Received 401 error. Attempting to refresh token.")
|
191
|
+
token = RagaAICatalyst.get_token(force_refresh=True)
|
192
|
+
headers = {
|
193
|
+
"Authorization": f"Bearer {token}",
|
194
|
+
"Content-Type": "application/json",
|
195
|
+
"X-Project-Name": self.project_name,
|
196
|
+
}
|
197
|
+
response = requests.request(
|
198
|
+
"POST",
|
199
|
+
endpoint,
|
200
|
+
headers=headers,
|
201
|
+
data=payload,
|
202
|
+
timeout=self.timeout,
|
203
|
+
)
|
204
|
+
elapsed_ms = (time.time() - start_time) * 1000
|
205
|
+
logger.debug(
|
206
|
+
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
207
|
+
)
|
208
|
+
if response.status_code != 200:
|
209
|
+
print(f"Error inserting traces: {response.json()['message']}")
|
210
|
+
return None
|
211
|
+
else:
|
212
|
+
print("Error while inserting traces")
|
213
|
+
return None
|
153
214
|
except requests.exceptions.RequestException as e:
|
154
215
|
print(f"Error while inserting traces: {e}")
|
155
216
|
return None
|
@@ -166,12 +227,16 @@ class UploadAgenticTraces:
|
|
166
227
|
dataset_spans = []
|
167
228
|
for span in spans:
|
168
229
|
try:
|
169
|
-
dataset_spans.append(
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
230
|
+
dataset_spans.append(
|
231
|
+
{
|
232
|
+
"spanId": span.get("context", {}).get("span_id", ""),
|
233
|
+
"spanName": span.get("name", ""),
|
234
|
+
"spanHash": span.get("hash_id", ""),
|
235
|
+
"spanType": span.get("attributes", {}).get(
|
236
|
+
"openinference.span.kind", ""
|
237
|
+
),
|
238
|
+
}
|
239
|
+
)
|
175
240
|
except Exception as e:
|
176
241
|
logger.warning(f"Error processing span: {e}")
|
177
242
|
continue
|
@@ -1,26 +1,43 @@
|
|
1
|
-
from aiohttp import payload
|
2
|
-
import requests
|
3
1
|
import json
|
2
|
+
import logging
|
4
3
|
import os
|
5
4
|
import time
|
6
|
-
|
5
|
+
|
6
|
+
import requests
|
7
|
+
|
7
8
|
from ragaai_catalyst.ragaai_catalyst import RagaAICatalyst
|
9
|
+
|
8
10
|
logger = logging.getLogger(__name__)
|
9
|
-
from urllib.parse import urlparse, urlunparse
|
10
11
|
import re
|
12
|
+
from urllib.parse import urlparse, urlunparse
|
13
|
+
|
11
14
|
|
12
|
-
def upload_code(
|
13
|
-
|
15
|
+
def upload_code(
|
16
|
+
hash_id, zip_path, project_name, dataset_name, base_url=None, timeout=120
|
17
|
+
):
|
18
|
+
code_hashes_list = _fetch_dataset_code_hashes(
|
19
|
+
project_name, dataset_name, base_url, timeout=timeout
|
20
|
+
)
|
14
21
|
|
15
22
|
if hash_id not in code_hashes_list:
|
16
|
-
presigned_url = _fetch_presigned_url(
|
23
|
+
presigned_url = _fetch_presigned_url(
|
24
|
+
project_name, dataset_name, base_url, timeout=timeout
|
25
|
+
)
|
17
26
|
_put_zip_presigned_url(project_name, presigned_url, zip_path, timeout=timeout)
|
18
27
|
|
19
|
-
response = _insert_code(
|
28
|
+
response = _insert_code(
|
29
|
+
dataset_name,
|
30
|
+
hash_id,
|
31
|
+
presigned_url,
|
32
|
+
project_name,
|
33
|
+
base_url,
|
34
|
+
timeout=timeout,
|
35
|
+
)
|
20
36
|
return response
|
21
37
|
else:
|
22
38
|
return "Code already exists"
|
23
39
|
|
40
|
+
|
24
41
|
def _fetch_dataset_code_hashes(project_name, dataset_name, base_url=None, timeout=120):
|
25
42
|
payload = {}
|
26
43
|
headers = {
|
@@ -32,32 +49,49 @@ def _fetch_dataset_code_hashes(project_name, dataset_name, base_url=None, timeou
|
|
32
49
|
url_base = base_url if base_url is not None else RagaAICatalyst.BASE_URL
|
33
50
|
start_time = time.time()
|
34
51
|
endpoint = f"{url_base}/v2/llm/dataset/code?datasetName={dataset_name}"
|
35
|
-
response = requests.request(
|
36
|
-
|
37
|
-
|
38
|
-
data=payload,
|
39
|
-
timeout=timeout)
|
52
|
+
response = requests.request(
|
53
|
+
"GET", endpoint, headers=headers, data=payload, timeout=timeout
|
54
|
+
)
|
40
55
|
elapsed_ms = (time.time() - start_time) * 1000
|
41
56
|
logger.debug(
|
42
|
-
f"API Call: [GET] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
57
|
+
f"API Call: [GET] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
58
|
+
)
|
43
59
|
|
44
60
|
if response.status_code == 200:
|
45
61
|
return response.json()["data"]["codeHashes"]
|
46
|
-
|
47
|
-
|
62
|
+
elif response.status_code == 401:
|
63
|
+
logger.warning("Received 401 error. Attempting to refresh token.")
|
64
|
+
RagaAICatalyst.get_token(force_refresh=True)
|
65
|
+
headers = {
|
66
|
+
"Authorization": f"Bearer {os.getenv('RAGAAI_CATALYST_TOKEN')}",
|
67
|
+
"X-Project-Name": project_name,
|
68
|
+
}
|
69
|
+
response = requests.request(
|
70
|
+
"GET", endpoint, headers=headers, data=payload, timeout=timeout
|
71
|
+
)
|
72
|
+
elapsed_ms = (time.time() - start_time) * 1000
|
73
|
+
logger.debug(
|
74
|
+
f"API Call: [GET] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
75
|
+
)
|
76
|
+
if response.status_code == 200:
|
77
|
+
return response.json()["data"]["codeHashes"]
|
78
|
+
else:
|
79
|
+
raise Exception(
|
80
|
+
f"Failed to fetch code hashes: {response.json()['message']}"
|
81
|
+
)
|
48
82
|
except requests.exceptions.RequestException as e:
|
49
83
|
logger.error(f"Failed to list datasets: {e}")
|
50
|
-
raise
|
84
|
+
raise
|
51
85
|
|
52
86
|
|
53
87
|
def update_presigned_url(presigned_url, base_url):
|
54
88
|
"""Replaces the domain (and port, if applicable) of the presigned URL with that of the base URL."""
|
55
|
-
#To Do: If Proxy URL has domain name how do we handle such cases? Engineering Dependency.
|
56
|
-
|
89
|
+
# To Do: If Proxy URL has domain name how do we handle such cases? Engineering Dependency.
|
90
|
+
|
57
91
|
presigned_parts = urlparse(presigned_url)
|
58
92
|
base_parts = urlparse(base_url)
|
59
93
|
# Check if base_url contains localhost or an IP address
|
60
|
-
if re.match(r
|
94
|
+
if re.match(r"^(localhost|\d{1,3}(\.\d{1,3}){3})$", base_parts.hostname):
|
61
95
|
new_netloc = base_parts.hostname # Extract domain from base_url
|
62
96
|
if base_parts.port: # Add port if present in base_url
|
63
97
|
new_netloc += f":{base_parts.port}"
|
@@ -67,11 +101,9 @@ def update_presigned_url(presigned_url, base_url):
|
|
67
101
|
|
68
102
|
|
69
103
|
def _fetch_presigned_url(project_name, dataset_name, base_url=None, timeout=120):
|
70
|
-
payload = json.dumps(
|
71
|
-
|
72
|
-
|
73
|
-
"contentType": "application/zip"
|
74
|
-
})
|
104
|
+
payload = json.dumps(
|
105
|
+
{"datasetName": dataset_name, "numFiles": 1, "contentType": "application/zip"}
|
106
|
+
)
|
75
107
|
|
76
108
|
headers = {
|
77
109
|
"Authorization": f"Bearer {os.getenv('RAGAAI_CATALYST_TOKEN')}",
|
@@ -84,93 +116,145 @@ def _fetch_presigned_url(project_name, dataset_name, base_url=None, timeout=120)
|
|
84
116
|
start_time = time.time()
|
85
117
|
# Changed to POST from GET
|
86
118
|
endpoint = f"{url_base}/v1/llm/presigned-url"
|
87
|
-
response = requests.request(
|
88
|
-
|
89
|
-
|
90
|
-
data=payload,
|
91
|
-
timeout=timeout)
|
119
|
+
response = requests.request(
|
120
|
+
"POST", endpoint, headers=headers, data=payload, timeout=timeout
|
121
|
+
)
|
92
122
|
elapsed_ms = (time.time() - start_time) * 1000
|
93
123
|
logger.debug(
|
94
|
-
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
124
|
+
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
125
|
+
)
|
95
126
|
|
96
127
|
if response.status_code == 200:
|
97
128
|
presigned_url = response.json()["data"]["presignedUrls"][0]
|
98
|
-
presigned_url = update_presigned_url(presigned_url,url_base)
|
129
|
+
presigned_url = update_presigned_url(presigned_url, url_base)
|
99
130
|
return presigned_url
|
100
131
|
else:
|
101
132
|
# If POST fails, try GET
|
102
|
-
response = requests.request(
|
103
|
-
|
104
|
-
|
105
|
-
data=payload,
|
106
|
-
timeout=timeout)
|
133
|
+
response = requests.request(
|
134
|
+
"POST", endpoint, headers=headers, data=payload, timeout=timeout
|
135
|
+
)
|
107
136
|
elapsed_ms = (time.time() - start_time) * 1000
|
108
137
|
logger.debug(
|
109
|
-
f"API Call: [
|
138
|
+
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
139
|
+
)
|
110
140
|
if response.status_code == 200:
|
111
141
|
presigned_url = response.json()["data"]["presignedUrls"][0]
|
112
|
-
presigned_url = update_presigned_url(presigned_url,url_base)
|
142
|
+
presigned_url = update_presigned_url(presigned_url, url_base)
|
113
143
|
return presigned_url
|
114
|
-
|
115
|
-
|
116
|
-
|
144
|
+
elif response.status_code == 401:
|
145
|
+
logger.warning("Received 401 error. Attempting to refresh token.")
|
146
|
+
RagaAICatalyst.get_token(force_refresh=True)
|
147
|
+
headers = {
|
148
|
+
"Authorization": f"Bearer {os.getenv('RAGAAI_CATALYST_TOKEN')}",
|
149
|
+
"Content-Type": "application/json",
|
150
|
+
"X-Project-Name": project_name,
|
151
|
+
}
|
152
|
+
response = requests.request(
|
153
|
+
"POST", endpoint, headers=headers, data=payload, timeout=timeout
|
154
|
+
)
|
155
|
+
elapsed_ms = (time.time() - start_time) * 1000
|
156
|
+
logger.debug(
|
157
|
+
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
158
|
+
)
|
159
|
+
if response.status_code == 200:
|
160
|
+
presigned_url = response.json()["data"]["presignedUrls"][0]
|
161
|
+
presigned_url = update_presigned_url(presigned_url, url_base)
|
162
|
+
return presigned_url
|
163
|
+
else:
|
164
|
+
logger.error(
|
165
|
+
f"Failed to fetch code hashes: {response.json()['message']}"
|
166
|
+
)
|
167
|
+
raise Exception(
|
168
|
+
f"Failed to fetch code hashes: {response.json()['message']}"
|
169
|
+
)
|
170
|
+
else:
|
171
|
+
logger.error(
|
172
|
+
f"Failed to fetch code hashes: {response.json()['message']}"
|
173
|
+
)
|
174
|
+
raise Exception(
|
175
|
+
f"Failed to fetch code hashes: {response.json()['message']}"
|
176
|
+
)
|
117
177
|
except requests.exceptions.RequestException as e:
|
118
178
|
logger.error(f"Failed to list datasets: {e}")
|
119
179
|
raise
|
120
180
|
|
181
|
+
|
121
182
|
def _put_zip_presigned_url(project_name, presignedUrl, filename, timeout=120):
|
122
183
|
headers = {
|
123
|
-
|
124
|
-
|
125
|
-
|
184
|
+
"X-Project-Name": project_name,
|
185
|
+
"Content-Type": "application/zip",
|
186
|
+
}
|
126
187
|
|
127
188
|
if "blob.core.windows.net" in presignedUrl: # Azure
|
128
189
|
headers["x-ms-blob-type"] = "BlockBlob"
|
129
|
-
print(
|
130
|
-
with open(filename,
|
190
|
+
print("Uploading code...")
|
191
|
+
with open(filename, "rb") as f:
|
131
192
|
payload = f.read()
|
132
193
|
|
133
194
|
start_time = time.time()
|
134
|
-
response = requests.request(
|
135
|
-
|
136
|
-
|
137
|
-
data=payload,
|
138
|
-
timeout=timeout)
|
195
|
+
response = requests.request(
|
196
|
+
"PUT", presignedUrl, headers=headers, data=payload, timeout=timeout
|
197
|
+
)
|
139
198
|
elapsed_ms = (time.time() - start_time) * 1000
|
140
199
|
logger.debug(
|
141
|
-
f"API Call: [PUT] {presignedUrl} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
200
|
+
f"API Call: [PUT] {presignedUrl} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
201
|
+
)
|
142
202
|
if response.status_code != 200 or response.status_code != 201:
|
143
203
|
return response, response.status_code
|
144
204
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
'Content-Type': 'application/json',
|
155
|
-
'Authorization': f'Bearer {os.getenv("RAGAAI_CATALYST_TOKEN")}'
|
205
|
+
|
206
|
+
def _insert_code(
|
207
|
+
dataset_name, hash_id, presigned_url, project_name, base_url=None, timeout=120
|
208
|
+
):
|
209
|
+
payload = json.dumps(
|
210
|
+
{
|
211
|
+
"datasetName": dataset_name,
|
212
|
+
"codeHash": hash_id,
|
213
|
+
"presignedUrl": presigned_url,
|
156
214
|
}
|
157
|
-
|
215
|
+
)
|
216
|
+
|
217
|
+
headers = {
|
218
|
+
"X-Project-Name": project_name,
|
219
|
+
"Content-Type": "application/json",
|
220
|
+
"Authorization": f"Bearer {os.getenv('RAGAAI_CATALYST_TOKEN')}",
|
221
|
+
}
|
222
|
+
|
158
223
|
try:
|
159
224
|
url_base = base_url if base_url is not None else RagaAICatalyst.BASE_URL
|
160
225
|
start_time = time.time()
|
161
226
|
endpoint = f"{url_base}/v2/llm/dataset/code"
|
162
|
-
response = requests.request(
|
163
|
-
|
164
|
-
|
165
|
-
data=payload,
|
166
|
-
timeout=timeout)
|
227
|
+
response = requests.request(
|
228
|
+
"POST", endpoint, headers=headers, data=payload, timeout=timeout
|
229
|
+
)
|
167
230
|
elapsed_ms = (time.time() - start_time) * 1000
|
168
231
|
logger.debug(
|
169
|
-
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
232
|
+
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
233
|
+
)
|
170
234
|
if response.status_code == 200:
|
171
235
|
return response.json()["message"]
|
236
|
+
|
237
|
+
elif response.status_code == 401:
|
238
|
+
logger.warning("Received 401 error. Attempting to refresh token.")
|
239
|
+
token = RagaAICatalyst.get_token(force_refresh=True)
|
240
|
+
headers = {
|
241
|
+
"X-Project-Name": project_name,
|
242
|
+
"Content-Type": "application/json",
|
243
|
+
"Authorization": f"Bearer {token}",
|
244
|
+
}
|
245
|
+
response = requests.request(
|
246
|
+
"POST", endpoint, headers=headers, data=payload, timeout=timeout
|
247
|
+
)
|
248
|
+
elapsed_ms = (time.time() - start_time) * 1000
|
249
|
+
logger.debug(
|
250
|
+
f"API Call: [POST] {endpoint} | Status: {response.status_code} | Time: {elapsed_ms:.2f}ms"
|
251
|
+
)
|
252
|
+
if response.status_code == 200:
|
253
|
+
return response.json()["message"]
|
254
|
+
else:
|
255
|
+
raise Exception(f"Failed to insert code: {response.json()['message']}")
|
172
256
|
else:
|
173
257
|
raise Exception(f"Failed to insert code: {response.json()['message']}")
|
174
258
|
except requests.exceptions.RequestException as e:
|
175
259
|
logger.error(f"Failed to insert code: {e}")
|
176
|
-
raise
|
260
|
+
raise
|