|
| 1 | +"""AI Model resource client for DataSpace SDK.""" |
| 2 | + |
| 3 | +from typing import Any, Dict, List, Optional |
| 4 | + |
| 5 | +from dataspace_sdk.base import BaseAPIClient |
| 6 | + |
| 7 | + |
| 8 | +class AIModelClient(BaseAPIClient): |
| 9 | + """Client for interacting with AI Model resources.""" |
| 10 | + |
| 11 | + def search( |
| 12 | + self, |
| 13 | + query: Optional[str] = None, |
| 14 | + tags: Optional[List[str]] = None, |
| 15 | + sectors: Optional[List[str]] = None, |
| 16 | + geographies: Optional[List[str]] = None, |
| 17 | + status: Optional[str] = None, |
| 18 | + model_type: Optional[str] = None, |
| 19 | + provider: Optional[str] = None, |
| 20 | + sort: Optional[str] = None, |
| 21 | + page: int = 1, |
| 22 | + page_size: int = 10, |
| 23 | + ) -> Dict[str, Any]: |
| 24 | + """ |
| 25 | + Search for AI models using Elasticsearch. |
| 26 | +
|
| 27 | + Args: |
| 28 | + query: Search query string |
| 29 | + tags: Filter by tags |
| 30 | + sectors: Filter by sectors |
| 31 | + geographies: Filter by geographies |
| 32 | + status: Filter by status (ACTIVE, INACTIVE, etc.) |
| 33 | + model_type: Filter by model type (LLM, VISION, etc.) |
| 34 | + provider: Filter by provider (OPENAI, ANTHROPIC, etc.) |
| 35 | + sort: Sort order (recent, alphabetical) |
| 36 | + page: Page number (1-indexed) |
| 37 | + page_size: Number of results per page |
| 38 | +
|
| 39 | + Returns: |
| 40 | + Dictionary containing search results and metadata |
| 41 | + """ |
| 42 | + params: Dict[str, Any] = { |
| 43 | + "page": page, |
| 44 | + "page_size": page_size, |
| 45 | + } |
| 46 | + |
| 47 | + if query: |
| 48 | + params["q"] = query |
| 49 | + if tags: |
| 50 | + params["tags"] = ",".join(tags) |
| 51 | + if sectors: |
| 52 | + params["sectors"] = ",".join(sectors) |
| 53 | + if geographies: |
| 54 | + params["geographies"] = ",".join(geographies) |
| 55 | + if status: |
| 56 | + params["status"] = status |
| 57 | + if model_type: |
| 58 | + params["model_type"] = model_type |
| 59 | + if provider: |
| 60 | + params["provider"] = provider |
| 61 | + if sort: |
| 62 | + params["sort"] = sort |
| 63 | + |
| 64 | + return self.get("/api/search/aimodel/", params=params) |
| 65 | + |
| 66 | + def get_by_id(self, model_id: str) -> Dict[str, Any]: |
| 67 | + """ |
| 68 | + Get an AI model by ID. |
| 69 | +
|
| 70 | + Args: |
| 71 | + model_id: UUID of the AI model |
| 72 | +
|
| 73 | + Returns: |
| 74 | + Dictionary containing AI model information |
| 75 | + """ |
| 76 | + return self.get(f"/api/aimodels/{model_id}/") |
| 77 | + |
| 78 | + def get_by_id_graphql(self, model_id: str) -> Dict[str, Any]: |
| 79 | + """ |
| 80 | + Get an AI model by ID using GraphQL. |
| 81 | +
|
| 82 | + Args: |
| 83 | + model_id: UUID of the AI model |
| 84 | +
|
| 85 | + Returns: |
| 86 | + Dictionary containing AI model information |
| 87 | + """ |
| 88 | + query = """ |
| 89 | + query GetAIModel($id: UUID!) { |
| 90 | + aiModel(id: $id) { |
| 91 | + id |
| 92 | + name |
| 93 | + displayName |
| 94 | + description |
| 95 | + modelType |
| 96 | + provider |
| 97 | + version |
| 98 | + providerModelId |
| 99 | + supportsStreaming |
| 100 | + maxTokens |
| 101 | + supportedLanguages |
| 102 | + inputSchema |
| 103 | + outputSchema |
| 104 | + status |
| 105 | + isPublic |
| 106 | + createdAt |
| 107 | + updatedAt |
| 108 | + organization { |
| 109 | + id |
| 110 | + name |
| 111 | + } |
| 112 | + tags { |
| 113 | + id |
| 114 | + value |
| 115 | + } |
| 116 | + sectors { |
| 117 | + id |
| 118 | + name |
| 119 | + } |
| 120 | + geographies { |
| 121 | + id |
| 122 | + name |
| 123 | + } |
| 124 | + endpoints { |
| 125 | + id |
| 126 | + name |
| 127 | + url |
| 128 | + httpMethod |
| 129 | + authType |
| 130 | + isActive |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + """ |
| 135 | + |
| 136 | + response = self.post( |
| 137 | + "/api/graphql", |
| 138 | + json_data={ |
| 139 | + "query": query, |
| 140 | + "variables": {"id": model_id}, |
| 141 | + }, |
| 142 | + ) |
| 143 | + |
| 144 | + if "errors" in response: |
| 145 | + from dataspace_sdk.exceptions import DataSpaceAPIError |
| 146 | + |
| 147 | + raise DataSpaceAPIError(f"GraphQL error: {response['errors']}") |
| 148 | + |
| 149 | + result: Dict[str, Any] = response.get("data", {}).get("aiModel", {}) |
| 150 | + return result |
| 151 | + |
| 152 | + def list_all( |
| 153 | + self, |
| 154 | + status: Optional[str] = None, |
| 155 | + organization_id: Optional[str] = None, |
| 156 | + model_type: Optional[str] = None, |
| 157 | + limit: int = 10, |
| 158 | + offset: int = 0, |
| 159 | + ) -> Dict[str, Any]: |
| 160 | + """ |
| 161 | + List all AI models with pagination using GraphQL. |
| 162 | +
|
| 163 | + Args: |
| 164 | + status: Filter by status |
| 165 | + organization_id: Filter by organization |
| 166 | + model_type: Filter by model type |
| 167 | + limit: Number of results to return |
| 168 | + offset: Number of results to skip |
| 169 | +
|
| 170 | + Returns: |
| 171 | + Dictionary containing list of AI models |
| 172 | + """ |
| 173 | + query = """ |
| 174 | + query ListAIModels($filters: AIModelFilter, $pagination: OffsetPaginationInput) { |
| 175 | + aiModels(filters: $filters, pagination: $pagination) { |
| 176 | + id |
| 177 | + name |
| 178 | + displayName |
| 179 | + description |
| 180 | + modelType |
| 181 | + provider |
| 182 | + version |
| 183 | + status |
| 184 | + isPublic |
| 185 | + createdAt |
| 186 | + updatedAt |
| 187 | + organization { |
| 188 | + id |
| 189 | + name |
| 190 | + } |
| 191 | + tags { |
| 192 | + id |
| 193 | + value |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + """ |
| 198 | + |
| 199 | + filters: Dict[str, Any] = {} |
| 200 | + if status: |
| 201 | + filters["status"] = status |
| 202 | + if organization_id: |
| 203 | + filters["organization"] = {"id": {"exact": organization_id}} |
| 204 | + if model_type: |
| 205 | + filters["modelType"] = model_type |
| 206 | + |
| 207 | + variables: Dict[str, Any] = { |
| 208 | + "pagination": {"limit": limit, "offset": offset}, |
| 209 | + } |
| 210 | + if filters: |
| 211 | + variables["filters"] = filters |
| 212 | + |
| 213 | + response = self.post( |
| 214 | + "/api/graphql", |
| 215 | + json_data={ |
| 216 | + "query": query, |
| 217 | + "variables": variables, |
| 218 | + }, |
| 219 | + ) |
| 220 | + |
| 221 | + if "errors" in response: |
| 222 | + from dataspace_sdk.exceptions import DataSpaceAPIError |
| 223 | + |
| 224 | + raise DataSpaceAPIError(f"GraphQL error: {response['errors']}") |
| 225 | + |
| 226 | + data = response.get("data", {}) |
| 227 | + models_result: Dict[str, Any] = data.get("aiModels", []) if isinstance(data, dict) else [] |
| 228 | + return models_result |
| 229 | + |
| 230 | + def get_organization_models( |
| 231 | + self, |
| 232 | + organization_id: str, |
| 233 | + limit: int = 10, |
| 234 | + offset: int = 0, |
| 235 | + ) -> Dict[str, Any]: |
| 236 | + """ |
| 237 | + Get AI models for a specific organization. |
| 238 | +
|
| 239 | + Args: |
| 240 | + organization_id: UUID of the organization |
| 241 | + limit: Number of results to return |
| 242 | + offset: Number of results to skip |
| 243 | +
|
| 244 | + Returns: |
| 245 | + Dictionary containing organization's AI models |
| 246 | + """ |
| 247 | + return self.list_all( |
| 248 | + organization_id=organization_id, |
| 249 | + limit=limit, |
| 250 | + offset=offset, |
| 251 | + ) |
0 commit comments