-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaimodels.py
More file actions
343 lines (297 loc) · 9.69 KB
/
aimodels.py
File metadata and controls
343 lines (297 loc) · 9.69 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""AI Model resource client for DataSpace SDK."""
from typing import Any, Dict, List, Optional
from dataspace_sdk.base import BaseAPIClient
class AIModelClient(BaseAPIClient):
"""Client for interacting with AI Model resources."""
def search(
self,
query: Optional[str] = None,
tags: Optional[List[str]] = None,
sectors: Optional[List[str]] = None,
geographies: Optional[List[str]] = None,
status: Optional[str] = None,
model_type: Optional[str] = None,
provider: Optional[str] = None,
sort: Optional[str] = None,
page: int = 1,
page_size: int = 10,
) -> Dict[str, Any]:
"""
Search for AI models using Elasticsearch.
Args:
query: Search query string
tags: Filter by tags
sectors: Filter by sectors
geographies: Filter by geographies
status: Filter by status (ACTIVE, INACTIVE, etc.)
model_type: Filter by model type (LLM, VISION, etc.)
provider: Filter by provider (OPENAI, ANTHROPIC, etc.)
sort: Sort order (recent, alphabetical)
page: Page number (1-indexed)
page_size: Number of results per page
Returns:
Dictionary containing search results and metadata
"""
params: Dict[str, Any] = {
"page": page,
"page_size": page_size,
}
if query:
params["q"] = query
if tags:
params["tags"] = ",".join(tags)
if sectors:
params["sectors"] = ",".join(sectors)
if geographies:
params["geographies"] = ",".join(geographies)
if status:
params["status"] = status
if model_type:
params["model_type"] = model_type
if provider:
params["provider"] = provider
if sort:
params["sort"] = sort
return self.get("/api/search/aimodel/", params=params)
def get_by_id(self, model_id: str) -> Dict[str, Any]:
"""
Get an AI model by ID.
Args:
model_id: UUID of the AI model
Returns:
Dictionary containing AI model information
"""
return self.get(f"/api/aimodels/{model_id}/")
def get_by_id_graphql(self, model_id: str) -> Dict[str, Any]:
"""
Get an AI model by ID using GraphQL.
Args:
model_id: UUID of the AI model
Returns:
Dictionary containing AI model information
"""
query = """
query GetAIModel($id: Int!) {
getAiModel(modelId: $id) {
id
name
displayName
description
modelType
provider
version
providerModelId
hfUsePipeline
hfAuthToken
hfModelClass
hfAttnImplementation
framework
supportsStreaming
maxTokens
supportedLanguages
inputSchema
outputSchema
status
isPublic
createdAt
updatedAt
organization {
id
name
}
tags {
id
value
}
sectors {
id
name
}
geographies {
id
name
}
endpoints {
id
url
httpMethod
authType
isActive
}
}
}
"""
response = self.post(
"/api/graphql",
json_data={
"query": query,
"variables": {"id": model_id},
},
)
if "errors" in response:
from dataspace_sdk.exceptions import DataSpaceAPIError
raise DataSpaceAPIError(f"GraphQL error: {response['errors']}")
result: Dict[str, Any] = response.get("data", {}).get("aiModel", {})
return result
def list_all(
self,
status: Optional[str] = None,
organization_id: Optional[str] = None,
model_type: Optional[str] = None,
limit: int = 10,
offset: int = 0,
) -> Any:
"""
List all AI models with pagination using GraphQL.
Args:
status: Filter by status
organization_id: Filter by organization
model_type: Filter by model type
limit: Number of results to return
offset: Number of results to skip
Returns:
Dictionary containing list of AI models
"""
query = """
query ListAIModels($filters: AIModelFilter, $pagination: OffsetPaginationInput) {
aiModels(filters: $filters, pagination: $pagination) {
id
name
displayName
description
modelType
provider
version
status
isPublic
createdAt
updatedAt
organization {
id
name
}
tags {
id
value
}
}
}
"""
filters: Dict[str, Any] = {}
if status:
filters["status"] = status
if organization_id:
filters["organization"] = {"id": {"exact": organization_id}}
if model_type:
filters["modelType"] = model_type
variables: Dict[str, Any] = {
"pagination": {"limit": limit, "offset": offset},
}
if filters:
variables["filters"] = filters
response = self.post(
"/api/graphql",
json_data={
"query": query,
"variables": variables,
},
)
if "errors" in response:
from dataspace_sdk.exceptions import DataSpaceAPIError
raise DataSpaceAPIError(f"GraphQL error: {response['errors']}")
data = response.get("data", {})
models_result: Any = data.get("aiModels", []) if isinstance(data, dict) else []
return models_result
def get_organization_models(
self,
organization_id: str,
limit: int = 10,
offset: int = 0,
) -> Any:
"""
Get AI models for a specific organization.
Args:
organization_id: UUID of the organization
limit: Number of results to return
offset: Number of results to skip
Returns:
Dictionary containing organization's AI models
"""
return self.list_all(
organization_id=organization_id,
limit=limit,
offset=offset,
)
def create(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""
Create a new AI model.
Args:
data: Dictionary containing AI model data
Returns:
Dictionary containing created AI model information
"""
return self.post("/api/aimodels/", json_data=data)
def update(self, model_id: str, data: Dict[str, Any]) -> Dict[str, Any]:
"""
Update an existing AI model.
Args:
model_id: UUID of the AI model
data: Dictionary containing updated AI model data
Returns:
Dictionary containing updated AI model information
"""
return self.patch(f"/api/aimodels/{model_id}/", json_data=data)
def delete_model(self, model_id: str) -> Dict[str, Any]:
"""
Delete an AI model.
Args:
model_id: UUID of the AI model
Returns:
Dictionary containing deletion response
"""
return self.delete(f"/api/aimodels/{model_id}/")
def call_model(
self, model_id: str, input_text: str, parameters: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
"""
Call an AI model with input text using the appropriate client (API or HuggingFace).
Args:
model_id: UUID of the AI model
input_text: Input text to process
parameters: Optional parameters for the model call (temperature, max_tokens, etc.)
Returns:
Dictionary containing model response:
{
"success": bool,
"output": str (if successful),
"error": str (if failed),
"latency_ms": float,
"provider": str,
...
}
"""
return self.post(
f"/api/aimodels/{model_id}/call/",
json_data={"input_text": input_text, "parameters": parameters or {}},
)
def call_model_async(
self, model_id: str, input_text: str, parameters: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
"""
Call an AI model asynchronously (returns task ID for long-running operations).
Args:
model_id: UUID of the AI model
input_text: Input text to process
parameters: Optional parameters for the model call
Returns:
Dictionary containing task information:
{
"task_id": str,
"status": str,
"model_id": str
}
"""
return self.post(
f"/api/aimodels/{model_id}/call-async/",
json_data={"input_text": input_text, "parameters": parameters or {}},
)