|
15 | 15 | # limitations under the License. |
16 | 16 | # |
17 | 17 | import itertools |
| 18 | +import httpx |
18 | 19 | import json |
19 | 20 | import pathlib |
20 | 21 | import re |
@@ -674,6 +675,7 @@ def __init__( |
674 | 675 | self._gca_resource = gca_endpoint_compat.Endpoint(name=endpoint_name) |
675 | 676 |
|
676 | 677 | self.authorized_session = None |
| 678 | + self.httpx_async_client = None |
677 | 679 | self.raw_predict_request_url = None |
678 | 680 | self.stream_raw_predict_request_url = None |
679 | 681 |
|
@@ -782,6 +784,7 @@ def create( |
782 | 784 | enable_request_response_logging=False, |
783 | 785 | request_response_logging_sampling_rate: Optional[float] = None, |
784 | 786 | request_response_logging_bq_destination_table: Optional[str] = None, |
| 787 | + dedicated_endpoint_enabled=False, |
785 | 788 | ) -> "Endpoint": |
786 | 789 | """Creates a new endpoint. |
787 | 790 |
|
@@ -849,6 +852,10 @@ def create( |
849 | 852 | request_response_logging_bq_destination_table (str): |
850 | 853 | Optional. The request response logging bigquery destination. If not set, will create a table with name: |
851 | 854 | ``bq://{project_id}.logging_{endpoint_display_name}_{endpoint_id}.request_response_logging``. |
| 855 | + dedicated_endpoint_enabled (bool): |
| 856 | + Optional. If enabled, a dedicated dns will be created and your |
| 857 | + traffic will be fully isolated from other customers' traffic and |
| 858 | + latency will be significantly reduced. |
852 | 859 |
|
853 | 860 | Returns: |
854 | 861 | endpoint (aiplatform.Endpoint): |
@@ -893,6 +900,7 @@ def create( |
893 | 900 | create_request_timeout=create_request_timeout, |
894 | 901 | endpoint_id=endpoint_id, |
895 | 902 | predict_request_response_logging_config=predict_request_response_logging_config, |
| 903 | + dedicated_endpoint_enabled=dedicated_endpoint_enabled, |
896 | 904 | ) |
897 | 905 |
|
898 | 906 | @classmethod |
@@ -918,6 +926,7 @@ def _create( |
918 | 926 | private_service_connect_config: Optional[ |
919 | 927 | gca_service_networking.PrivateServiceConnectConfig |
920 | 928 | ] = None, |
| 929 | + dedicated_endpoint_enabled=False, |
921 | 930 | ) -> "Endpoint": |
922 | 931 | """Creates a new endpoint by calling the API client. |
923 | 932 |
|
@@ -984,6 +993,10 @@ def _create( |
984 | 993 | private_service_connect_config (aiplatform.service_network.PrivateServiceConnectConfig): |
985 | 994 | If enabled, the endpoint can be accessible via [Private Service Connect](https://cloud.google.com/vpc/docs/private-service-connect). |
986 | 995 | Cannot be enabled when network is specified. |
| 996 | + dedicated_endpoint_enabled (bool): |
| 997 | + Optional. If enabled, a dedicated dns will be created and your |
| 998 | + traffic will be fully isolated from other customers' traffic and |
| 999 | + latency will be significantly reduced. |
987 | 1000 |
|
988 | 1001 | Returns: |
989 | 1002 | endpoint (aiplatform.Endpoint): |
@@ -1002,6 +1015,7 @@ def _create( |
1002 | 1015 | network=network, |
1003 | 1016 | predict_request_response_logging_config=predict_request_response_logging_config, |
1004 | 1017 | private_service_connect_config=private_service_connect_config, |
| 1018 | + dedicated_endpoint_enabled=dedicated_endpoint_enabled, |
1005 | 1019 | ) |
1006 | 1020 |
|
1007 | 1021 | operation_future = api_client.create_endpoint( |
@@ -1060,6 +1074,7 @@ def _construct_sdk_resource_from_gapic( |
1060 | 1074 | credentials=credentials, |
1061 | 1075 | ) |
1062 | 1076 | endpoint.authorized_session = None |
| 1077 | + endpoint.httpx_async_client = None |
1063 | 1078 | endpoint.raw_predict_request_url = None |
1064 | 1079 | endpoint.stream_raw_predict_request_url = None |
1065 | 1080 |
|
@@ -2167,6 +2182,7 @@ def predict( |
2167 | 2182 | parameters: Optional[Dict] = None, |
2168 | 2183 | timeout: Optional[float] = None, |
2169 | 2184 | use_raw_predict: Optional[bool] = False, |
| 2185 | + use_dedicated_endpoint: Optional[bool] = False, |
2170 | 2186 | ) -> Prediction: |
2171 | 2187 | """Make a prediction against this Endpoint. |
2172 | 2188 |
|
@@ -2194,6 +2210,9 @@ def predict( |
2194 | 2210 | use_raw_predict (bool): |
2195 | 2211 | Optional. Default value is False. If set to True, the underlying prediction call will be made |
2196 | 2212 | against Endpoint.raw_predict(). |
| 2213 | + use_dedicated_endpoint (bool): |
| 2214 | + Optional. Default value is False. If set to True, the underlying prediction call will be made |
| 2215 | + using the dedicated endpoint dns. |
2197 | 2216 |
|
2198 | 2217 | Returns: |
2199 | 2218 | prediction (aiplatform.Prediction): |
@@ -2219,6 +2238,51 @@ def predict( |
2219 | 2238 | _RAW_PREDICT_MODEL_VERSION_ID_KEY, None |
2220 | 2239 | ), |
2221 | 2240 | ) |
| 2241 | + |
| 2242 | + if use_dedicated_endpoint: |
| 2243 | + self._sync_gca_resource_if_skipped() |
| 2244 | + if ( |
| 2245 | + not self._gca_resource.dedicated_endpoint_enabled |
| 2246 | + or self._gca_resource.dedicated_endpoint_dns is None |
| 2247 | + ): |
| 2248 | + raise ValueError( |
| 2249 | + "Dedicated endpoint is not enabled or DNS is empty." |
| 2250 | + "Please make sure endpoint has dedicated endpoint enabled" |
| 2251 | + "and model are ready before making a prediction." |
| 2252 | + ) |
| 2253 | + |
| 2254 | + if not self.authorized_session: |
| 2255 | + self.credentials._scopes = constants.base.DEFAULT_AUTHED_SCOPES |
| 2256 | + self.authorized_session = google_auth_requests.AuthorizedSession( |
| 2257 | + self.credentials |
| 2258 | + ) |
| 2259 | + |
| 2260 | + headers = { |
| 2261 | + "Content-Type": "application/json", |
| 2262 | + } |
| 2263 | + |
| 2264 | + url = f"https://{self._gca_resource.dedicated_endpoint_dns}/v1/{self.resource_name}:predict" |
| 2265 | + response = self.authorized_session.post( |
| 2266 | + url=url, |
| 2267 | + data=json.dumps( |
| 2268 | + { |
| 2269 | + "instances": instances, |
| 2270 | + "parameters": parameters, |
| 2271 | + } |
| 2272 | + ), |
| 2273 | + headers=headers, |
| 2274 | + ) |
| 2275 | + |
| 2276 | + prediction_response = json.loads(response.text) |
| 2277 | + |
| 2278 | + return Prediction( |
| 2279 | + predictions=prediction_response.get("predictions"), |
| 2280 | + metadata=prediction_response.get("metadata"), |
| 2281 | + deployed_model_id=prediction_response.get("deployedModelId"), |
| 2282 | + model_resource_name=prediction_response.get("model"), |
| 2283 | + model_version_id=prediction_response.get("modelVersionId"), |
| 2284 | + ) |
| 2285 | + |
2222 | 2286 | else: |
2223 | 2287 | prediction_response = self._prediction_client.predict( |
2224 | 2288 | endpoint=self._gca_resource.name, |
@@ -2248,6 +2312,7 @@ async def predict_async( |
2248 | 2312 | *, |
2249 | 2313 | parameters: Optional[Dict] = None, |
2250 | 2314 | timeout: Optional[float] = None, |
| 2315 | + use_dedicated_endpoint: Optional[bool] = False, |
2251 | 2316 | ) -> Prediction: |
2252 | 2317 | """Make an asynchronous prediction against this Endpoint. |
2253 | 2318 | Example usage: |
@@ -2277,13 +2342,66 @@ async def predict_async( |
2277 | 2342 | [PredictSchemata's][google.cloud.aiplatform.v1beta1.Model.predict_schemata] |
2278 | 2343 | ``parameters_schema_uri``. |
2279 | 2344 | timeout (float): Optional. The timeout for this request in seconds. |
| 2345 | + use_dedicated_endpoint (bool): |
| 2346 | + Optional. Default value is False. If set to True, the underlying prediction call will be made |
| 2347 | + using the dedicated endpoint dns. |
2280 | 2348 |
|
2281 | 2349 | Returns: |
2282 | 2350 | prediction (aiplatform.Prediction): |
2283 | 2351 | Prediction with returned predictions and Model ID. |
2284 | 2352 | """ |
2285 | 2353 | self.wait() |
2286 | 2354 |
|
| 2355 | + if use_dedicated_endpoint: |
| 2356 | + self._sync_gca_resource_if_skipped() |
| 2357 | + if ( |
| 2358 | + not self._gca_resource.dedicated_endpoint_enabled |
| 2359 | + or self._gca_resource.dedicated_endpoint_dns is None |
| 2360 | + ): |
| 2361 | + raise ValueError( |
| 2362 | + "Dedicated endpoint is not enabled or DNS is empty." |
| 2363 | + "Please make sure endpoint has dedicated endpoint enabled" |
| 2364 | + "and model are ready before making a prediction." |
| 2365 | + ) |
| 2366 | + |
| 2367 | + if not self.httpx_async_client: |
| 2368 | + self.credentials._scopes = constants.base.DEFAULT_AUTHED_SCOPES |
| 2369 | + self.httpx_async_client = httpx.AsyncClient() |
| 2370 | + |
| 2371 | + if not self.credentials.valid: |
| 2372 | + self.credentials.refresh(google_auth_requests.Request()) |
| 2373 | + |
| 2374 | + token = self.credentials.token |
| 2375 | + headers = { |
| 2376 | + "Authorization": f"Bearer {token}", |
| 2377 | + "Content-Type": "application/json", |
| 2378 | + } |
| 2379 | + |
| 2380 | + url = f"https://{self._gca_resource.dedicated_endpoint_dns}/v1/{self.resource_name}:predict" |
| 2381 | + async with self.httpx_async_client as client: |
| 2382 | + response = ( |
| 2383 | + await client.post( |
| 2384 | + url=url, |
| 2385 | + data=json.dumps( |
| 2386 | + { |
| 2387 | + "instances": instances, |
| 2388 | + "parameters": parameters, |
| 2389 | + } |
| 2390 | + ), |
| 2391 | + headers=headers, |
| 2392 | + ) |
| 2393 | + ).json() |
| 2394 | + |
| 2395 | + prediction_response = json.loads(response.text) |
| 2396 | + |
| 2397 | + return Prediction( |
| 2398 | + predictions=prediction_response.get("predictions"), |
| 2399 | + metadata=prediction_response.get("metadata"), |
| 2400 | + deployed_model_id=prediction_response.get("deployedModelId"), |
| 2401 | + model_resource_name=prediction_response.get("model"), |
| 2402 | + model_version_id=prediction_response.get("modelVersionId"), |
| 2403 | + ) |
| 2404 | + |
2287 | 2405 | prediction_response = await self._prediction_async_client.predict( |
2288 | 2406 | endpoint=self._gca_resource.name, |
2289 | 2407 | instances=instances, |
@@ -2307,7 +2425,10 @@ async def predict_async( |
2307 | 2425 | ) |
2308 | 2426 |
|
2309 | 2427 | def raw_predict( |
2310 | | - self, body: bytes, headers: Dict[str, str] |
| 2428 | + self, |
| 2429 | + body: bytes, |
| 2430 | + headers: Dict[str, str], |
| 2431 | + use_dedicated_endpoint: Optional[bool] = False, |
2311 | 2432 | ) -> requests.models.Response: |
2312 | 2433 | """Makes a prediction request using arbitrary headers. |
2313 | 2434 |
|
@@ -2325,6 +2446,9 @@ def raw_predict( |
2325 | 2446 | The body of the prediction request in bytes. This must not exceed 1.5 mb per request. |
2326 | 2447 | headers (Dict[str, str]): |
2327 | 2448 | The header of the request as a dictionary. There are no restrictions on the header. |
| 2449 | + use_dedicated_endpoint (bool): |
| 2450 | + Optional. Default value is False. If set to True, the underlying prediction call will be made |
| 2451 | + using the dedicated endpoint dns. |
2328 | 2452 |
|
2329 | 2453 | Returns: |
2330 | 2454 | A requests.models.Response object containing the status code and prediction results. |
@@ -2338,12 +2462,28 @@ def raw_predict( |
2338 | 2462 | if self.raw_predict_request_url is None: |
2339 | 2463 | self.raw_predict_request_url = f"https://{self.location}-{constants.base.API_BASE_PATH}/v1/projects/{self.project}/locations/{self.location}/endpoints/{self.name}:rawPredict" |
2340 | 2464 |
|
2341 | | - return self.authorized_session.post( |
2342 | | - url=self.raw_predict_request_url, data=body, headers=headers |
2343 | | - ) |
| 2465 | + url = self.raw_predict_request_url |
| 2466 | + |
| 2467 | + if use_dedicated_endpoint: |
| 2468 | + self._sync_gca_resource_if_skipped() |
| 2469 | + if ( |
| 2470 | + not self._gca_resource.dedicated_endpoint_enabled |
| 2471 | + or self._gca_resource.dedicated_endpoint_dns is None |
| 2472 | + ): |
| 2473 | + raise ValueError( |
| 2474 | + "Dedicated endpoint is not enabled or DNS is empty." |
| 2475 | + "Please make sure endpoint has dedicated endpoint enabled" |
| 2476 | + "and model are ready before making a prediction." |
| 2477 | + ) |
| 2478 | + url = f"https://{self._gca_resource.dedicated_endpoint_dns}/v1/{self.resource_name}:rawPredict" |
| 2479 | + |
| 2480 | + return self.authorized_session.post(url=url, data=body, headers=headers) |
2344 | 2481 |
|
2345 | 2482 | def stream_raw_predict( |
2346 | | - self, body: bytes, headers: Dict[str, str] |
| 2483 | + self, |
| 2484 | + body: bytes, |
| 2485 | + headers: Dict[str, str], |
| 2486 | + use_dedicated_endpoint: Optional[bool] = False, |
2347 | 2487 | ) -> Iterator[requests.models.Response]: |
2348 | 2488 | """Makes a streaming prediction request using arbitrary headers. |
2349 | 2489 |
|
@@ -2365,6 +2505,9 @@ def stream_raw_predict( |
2365 | 2505 | headers (Dict[str, str]): |
2366 | 2506 | The header of the request as a dictionary. There are no |
2367 | 2507 | restrictions on the header. |
| 2508 | + use_dedicated_endpoint (bool): |
| 2509 | + Optional. Default value is False. If set to True, the underlying prediction call will be made |
| 2510 | + using the dedicated endpoint dns. |
2368 | 2511 |
|
2369 | 2512 | Yields: |
2370 | 2513 | predictions (Iterator[requests.models.Response]): |
@@ -2379,8 +2522,23 @@ def stream_raw_predict( |
2379 | 2522 | if self.stream_raw_predict_request_url is None: |
2380 | 2523 | self.stream_raw_predict_request_url = f"https://{self.location}-{constants.base.API_BASE_PATH}/v1/projects/{self.project}/locations/{self.location}/endpoints/{self.name}:streamRawPredict" |
2381 | 2524 |
|
| 2525 | + url = self.raw_predict_request_url |
| 2526 | + |
| 2527 | + if use_dedicated_endpoint: |
| 2528 | + self._sync_gca_resource_if_skipped() |
| 2529 | + if ( |
| 2530 | + not self._gca_resource.dedicated_endpoint_enabled |
| 2531 | + or self._gca_resource.dedicated_endpoint_dns is None |
| 2532 | + ): |
| 2533 | + raise ValueError( |
| 2534 | + "Dedicated endpoint is not enabled or DNS is empty." |
| 2535 | + "Please make sure endpoint has dedicated endpoint enabled" |
| 2536 | + "and model are ready before making a prediction." |
| 2537 | + ) |
| 2538 | + url = f"https://{self._gca_resource.dedicated_endpoint_dns}/v1/{self.resource_name}:streamRawPredict" |
| 2539 | + |
2382 | 2540 | with self.authorized_session.post( |
2383 | | - url=self.stream_raw_predict_request_url, |
| 2541 | + url=url, |
2384 | 2542 | data=body, |
2385 | 2543 | headers=headers, |
2386 | 2544 | stream=True, |
|