|
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 |
21 | 22 | import shutil |
| 23 | +import string |
22 | 24 | import tempfile |
23 | 25 | import requests |
24 | 26 | from typing import ( |
@@ -626,6 +628,7 @@ def __init__( |
626 | 628 | self._gca_resource = gca_endpoint_compat.Endpoint(name=endpoint_name) |
627 | 629 |
|
628 | 630 | self.authorized_session = None |
| 631 | + self.httpx_async_client = None |
629 | 632 | self.raw_predict_request_url = None |
630 | 633 | self.stream_raw_predict_request_url = None |
631 | 634 |
|
@@ -717,6 +720,18 @@ def private_service_connect_config( |
717 | 720 | self._assert_gca_resource_is_available() |
718 | 721 | return self._gca_resource.private_service_connect_config |
719 | 722 |
|
| 723 | + @property |
| 724 | + def dedicated_endpoint_dns(self) -> string: |
| 725 | + """The dedicated DNS for this Endpoint.""" |
| 726 | + self._assert_gca_resource_is_available() |
| 727 | + return self._gca_resource.dedicated_endpoint_dns |
| 728 | + |
| 729 | + @property |
| 730 | + def dedicated_endpoint_enabled(self) -> bool: |
| 731 | + """Whether this endpoint is a dedicated endpoint.""" |
| 732 | + self._assert_gca_resource_is_available() |
| 733 | + return self._gca_resource.dedicated_endpoint_enabled |
| 734 | + |
720 | 735 | @classmethod |
721 | 736 | def create( |
722 | 737 | cls, |
@@ -734,6 +749,7 @@ def create( |
734 | 749 | enable_request_response_logging=False, |
735 | 750 | request_response_logging_sampling_rate: Optional[float] = None, |
736 | 751 | request_response_logging_bq_destination_table: Optional[str] = None, |
| 752 | + dedicated_endpoint_enabled=False, |
737 | 753 | ) -> "Endpoint": |
738 | 754 | """Creates a new endpoint. |
739 | 755 |
|
@@ -801,6 +817,10 @@ def create( |
801 | 817 | request_response_logging_bq_destination_table (str): |
802 | 818 | Optional. The request response logging bigquery destination. If not set, will create a table with name: |
803 | 819 | ``bq://{project_id}.logging_{endpoint_display_name}_{endpoint_id}.request_response_logging``. |
| 820 | + dedicated_endpoint_enabled (bool): |
| 821 | + Optional. If enabled, a dedicated dns will be created and your |
| 822 | + traffic will be fully isolated from other customers' traffic and |
| 823 | + latency will be significantly reduced. |
804 | 824 |
|
805 | 825 | Returns: |
806 | 826 | endpoint (aiplatform.Endpoint): |
@@ -845,6 +865,7 @@ def create( |
845 | 865 | create_request_timeout=create_request_timeout, |
846 | 866 | endpoint_id=endpoint_id, |
847 | 867 | predict_request_response_logging_config=predict_request_response_logging_config, |
| 868 | + dedicated_endpoint_enabled=dedicated_endpoint_enabled, |
848 | 869 | ) |
849 | 870 |
|
850 | 871 | @classmethod |
@@ -870,6 +891,7 @@ def _create( |
870 | 891 | private_service_connect_config: Optional[ |
871 | 892 | gca_service_networking.PrivateServiceConnectConfig |
872 | 893 | ] = None, |
| 894 | + dedicated_endpoint_enabled=False, |
873 | 895 | ) -> "Endpoint": |
874 | 896 | """Creates a new endpoint by calling the API client. |
875 | 897 |
|
@@ -936,6 +958,10 @@ def _create( |
936 | 958 | private_service_connect_config (aiplatform.service_network.PrivateServiceConnectConfig): |
937 | 959 | If enabled, the endpoint can be accessible via [Private Service Connect](https://cloud.google.com/vpc/docs/private-service-connect). |
938 | 960 | Cannot be enabled when network is specified. |
| 961 | + dedicated_endpoint_enabled (bool): |
| 962 | + Optional. If enabled, a dedicated dns will be created and your |
| 963 | + traffic will be fully isolated from other customers' traffic and |
| 964 | + latency will be significantly reduced. |
939 | 965 |
|
940 | 966 | Returns: |
941 | 967 | endpoint (aiplatform.Endpoint): |
@@ -954,6 +980,7 @@ def _create( |
954 | 980 | network=network, |
955 | 981 | predict_request_response_logging_config=predict_request_response_logging_config, |
956 | 982 | private_service_connect_config=private_service_connect_config, |
| 983 | + dedicated_endpoint_enabled=dedicated_endpoint_enabled, |
957 | 984 | ) |
958 | 985 |
|
959 | 986 | operation_future = api_client.create_endpoint( |
@@ -1012,6 +1039,7 @@ def _construct_sdk_resource_from_gapic( |
1012 | 1039 | credentials=credentials, |
1013 | 1040 | ) |
1014 | 1041 | endpoint.authorized_session = None |
| 1042 | + endpoint.httpx_async_client = None |
1015 | 1043 | endpoint.raw_predict_request_url = None |
1016 | 1044 | endpoint.stream_raw_predict_request_url = None |
1017 | 1045 |
|
@@ -2101,6 +2129,42 @@ def predict( |
2101 | 2129 | _RAW_PREDICT_MODEL_VERSION_ID_KEY, None |
2102 | 2130 | ), |
2103 | 2131 | ) |
| 2132 | + if self.dedicated_endpoint_enabled: |
| 2133 | + if self.dedicated_endpoint_dns is None: |
| 2134 | + raise ValueError( |
| 2135 | + "Dedicated endpoint DNS is empty. Please make sure endpoint" |
| 2136 | + " and model are ready before making a prediction." |
| 2137 | + ) |
| 2138 | + |
| 2139 | + if not self.authorized_session: |
| 2140 | + self.credentials._scopes = constants.base.DEFAULT_AUTHED_SCOPES |
| 2141 | + self.authorized_session = google_auth_requests.AuthorizedSession( |
| 2142 | + self.credentials |
| 2143 | + ) |
| 2144 | + |
| 2145 | + headers = { |
| 2146 | + "Content-Type": "application/json", |
| 2147 | + } |
| 2148 | + |
| 2149 | + url = f"https://{self.dedicated_endpoint_dns}/v1/{self.resource_name}:predict" |
| 2150 | + response = self.authorized_session.post( |
| 2151 | + url=url, |
| 2152 | + data=json.dumps({ |
| 2153 | + "instances": instances, |
| 2154 | + "parameters": parameters, |
| 2155 | + }), |
| 2156 | + headers=headers, |
| 2157 | + ) |
| 2158 | + |
| 2159 | + prediction_response = json.loads(response.data) |
| 2160 | + |
| 2161 | + return Prediction( |
| 2162 | + predictions=prediction_response.get("predictions"), |
| 2163 | + metadata=prediction_response.get("metadata"), |
| 2164 | + deployed_model_id=prediction_response.get("deployedModelId"), |
| 2165 | + model_resource_name=prediction_response.get("model"), |
| 2166 | + model_version_id=prediction_response.get("modelVersionId"), |
| 2167 | + ) |
2104 | 2168 | else: |
2105 | 2169 | prediction_response = self._prediction_client.predict( |
2106 | 2170 | endpoint=self._gca_resource.name, |
@@ -2166,27 +2230,65 @@ async def predict_async( |
2166 | 2230 | """ |
2167 | 2231 | self.wait() |
2168 | 2232 |
|
2169 | | - prediction_response = await self._prediction_async_client.predict( |
2170 | | - endpoint=self._gca_resource.name, |
2171 | | - instances=instances, |
2172 | | - parameters=parameters, |
2173 | | - timeout=timeout, |
2174 | | - ) |
2175 | | - if prediction_response._pb.metadata: |
2176 | | - metadata = json_format.MessageToDict(prediction_response._pb.metadata) |
| 2233 | + if self.dedicated_endpoint_enabled: |
| 2234 | + if self.dedicated_endpoint_dns is None: |
| 2235 | + raise ValueError( |
| 2236 | + "Dedicated endpoint DNS is empty. Please make sure endpoint" |
| 2237 | + " and model are ready before making a prediction." |
| 2238 | + ) |
| 2239 | + |
| 2240 | + if not self.httpx_async_client: |
| 2241 | + self.credentials._scopes = constants.base.DEFAULT_AUTHED_SCOPES |
| 2242 | + self.httpx_async_client = httpx.AsyncClient( |
| 2243 | + auth=self.credentials, |
| 2244 | + ) |
| 2245 | + |
| 2246 | + headers = { |
| 2247 | + "Content-Type": "application/json", |
| 2248 | + } |
| 2249 | + |
| 2250 | + url = f"https://{self.dedicated_endpoint_dns}/v1/{self.resource_name}:predict" |
| 2251 | + async with self.httpx_async_client as client: |
| 2252 | + response = (await client.post( |
| 2253 | + url=url, |
| 2254 | + data=json.dumps({ |
| 2255 | + "instances": instances, |
| 2256 | + "parameters": parameters, |
| 2257 | + }), |
| 2258 | + headers=headers |
| 2259 | + )).json() |
| 2260 | + |
| 2261 | + prediction_response = json.loads(response.data) |
| 2262 | + |
| 2263 | + return Prediction( |
| 2264 | + predictions=prediction_response.get("predictions"), |
| 2265 | + metadata=prediction_response.get("metadata"), |
| 2266 | + deployed_model_id=prediction_response.get("deployedModelId"), |
| 2267 | + model_resource_name=prediction_response.get("model"), |
| 2268 | + model_version_id=prediction_response.get("modelVersionId"), |
| 2269 | + ) |
2177 | 2270 | else: |
2178 | | - metadata = None |
| 2271 | + prediction_response = await self._prediction_async_client.predict( |
| 2272 | + endpoint=self._gca_resource.name, |
| 2273 | + instances=instances, |
| 2274 | + parameters=parameters, |
| 2275 | + timeout=timeout, |
| 2276 | + ) |
| 2277 | + if prediction_response._pb.metadata: |
| 2278 | + metadata = json_format.MessageToDict(prediction_response._pb.metadata) |
| 2279 | + else: |
| 2280 | + metadata = None |
2179 | 2281 |
|
2180 | | - return Prediction( |
2181 | | - predictions=[ |
2182 | | - json_format.MessageToDict(item) |
2183 | | - for item in prediction_response.predictions.pb |
2184 | | - ], |
2185 | | - metadata=metadata, |
2186 | | - deployed_model_id=prediction_response.deployed_model_id, |
2187 | | - model_version_id=prediction_response.model_version_id, |
2188 | | - model_resource_name=prediction_response.model, |
2189 | | - ) |
| 2282 | + return Prediction( |
| 2283 | + predictions=[ |
| 2284 | + json_format.MessageToDict(item) |
| 2285 | + for item in prediction_response.predictions.pb |
| 2286 | + ], |
| 2287 | + metadata=metadata, |
| 2288 | + deployed_model_id=prediction_response.deployed_model_id, |
| 2289 | + model_version_id=prediction_response.model_version_id, |
| 2290 | + model_resource_name=prediction_response.model, |
| 2291 | + ) |
2190 | 2292 |
|
2191 | 2293 | def raw_predict( |
2192 | 2294 | self, body: bytes, headers: Dict[str, str] |
@@ -2220,9 +2322,17 @@ def raw_predict( |
2220 | 2322 | if self.raw_predict_request_url is None: |
2221 | 2323 | 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" |
2222 | 2324 |
|
2223 | | - return self.authorized_session.post( |
2224 | | - url=self.raw_predict_request_url, data=body, headers=headers |
2225 | | - ) |
| 2325 | + url = self.raw_predict_request_url |
| 2326 | + |
| 2327 | + if self.dedicated_endpoint_enabled: |
| 2328 | + if self.dedicated_endpoint_dns is None: |
| 2329 | + raise ValueError( |
| 2330 | + "Dedicated endpoint DNS is empty. Please make sure endpoint" |
| 2331 | + " and model are ready before making a prediction." |
| 2332 | + ) |
| 2333 | + url = f"https://{self.dedicated_endpoint_dns}/v1/{self.resource_name}:rawPredict" |
| 2334 | + |
| 2335 | + return self.authorized_session.post(url=url, data=body, headers=headers) |
2226 | 2336 |
|
2227 | 2337 | def stream_raw_predict( |
2228 | 2338 | self, body: bytes, headers: Dict[str, str] |
@@ -2261,8 +2371,18 @@ def stream_raw_predict( |
2261 | 2371 | if self.stream_raw_predict_request_url is None: |
2262 | 2372 | 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" |
2263 | 2373 |
|
| 2374 | + url = self.raw_predict_request_url |
| 2375 | + |
| 2376 | + if self.dedicated_endpoint_enabled: |
| 2377 | + if self.dedicated_endpoint_dns is None: |
| 2378 | + raise ValueError( |
| 2379 | + "Dedicated endpoint DNS is empty. Please make sure endpoint" |
| 2380 | + " and model are ready before making a prediction." |
| 2381 | + ) |
| 2382 | + url = f"https://{self.dedicated_endpoint_dns}/v1/{self.resource_name}:rawPredict" |
| 2383 | + |
2264 | 2384 | with self.authorized_session.post( |
2265 | | - url=self.stream_raw_predict_request_url, |
| 2385 | + url=url, |
2266 | 2386 | data=body, |
2267 | 2387 | headers=headers, |
2268 | 2388 | stream=True, |
@@ -2306,6 +2426,9 @@ def direct_predict( |
2306 | 2426 | """ |
2307 | 2427 | self.wait() |
2308 | 2428 |
|
| 2429 | + if self.dedicated_endpoint_enabled: |
| 2430 | + raise ValueError( |
| 2431 | + "This method is not supported for dedicated endpoints.") |
2309 | 2432 | prediction_response = self._prediction_client.direct_predict( |
2310 | 2433 | request={ |
2311 | 2434 | "endpoint": self._gca_resource.name, |
@@ -2369,6 +2492,9 @@ async def direct_predict_async( |
2369 | 2492 | """ |
2370 | 2493 | self.wait() |
2371 | 2494 |
|
| 2495 | + if self.dedicated_endpoint_enabled: |
| 2496 | + raise ValueError( |
| 2497 | + "This method is not supported for dedicated endpoints.") |
2372 | 2498 | prediction_response = await self._prediction_async_client.direct_predict( |
2373 | 2499 | request={ |
2374 | 2500 | "endpoint": self._gca_resource.name, |
@@ -2424,6 +2550,9 @@ def stream_direct_predict( |
2424 | 2550 | The resulting streamed predictions. |
2425 | 2551 | """ |
2426 | 2552 | self.wait() |
| 2553 | + if self.dedicated_endpoint_enabled: |
| 2554 | + raise ValueError( |
| 2555 | + "This method is not supported for dedicated endpoints.") |
2427 | 2556 | for resp in self._prediction_client.stream_direct_predict( |
2428 | 2557 | requests=( |
2429 | 2558 | { |
@@ -2474,6 +2603,9 @@ def direct_raw_predict( |
2474 | 2603 | """ |
2475 | 2604 | self.wait() |
2476 | 2605 |
|
| 2606 | + if self.dedicated_endpoint_enabled: |
| 2607 | + raise ValueError( |
| 2608 | + "This method is not supported for dedicated endpoints.") |
2477 | 2609 | prediction_response = self._prediction_client.direct_raw_predict( |
2478 | 2610 | request={ |
2479 | 2611 | "endpoint": self._gca_resource.name, |
@@ -2520,6 +2652,9 @@ async def direct_raw_predict_async( |
2520 | 2652 | """ |
2521 | 2653 | self.wait() |
2522 | 2654 |
|
| 2655 | + if self.dedicated_endpoint_enabled: |
| 2656 | + raise ValueError( |
| 2657 | + "This method is not supported for dedicated endpoints.") |
2523 | 2658 | prediction_response = await self._prediction_async_client.direct_raw_predict( |
2524 | 2659 | request={ |
2525 | 2660 | "endpoint": self._gca_resource.name, |
@@ -2569,6 +2704,9 @@ def stream_direct_raw_predict( |
2569 | 2704 | """ |
2570 | 2705 | self.wait() |
2571 | 2706 |
|
| 2707 | + if self.dedicated_endpoint_enabled: |
| 2708 | + raise ValueError( |
| 2709 | + "This method is not supported for dedicated endpoints.") |
2572 | 2710 | for resp in self._prediction_client.stream_direct_raw_predict( |
2573 | 2711 | requests=( |
2574 | 2712 | { |
@@ -2632,6 +2770,9 @@ def explain( |
2632 | 2770 | """ |
2633 | 2771 | self.wait() |
2634 | 2772 |
|
| 2773 | + if self.dedicated_endpoint_enabled: |
| 2774 | + raise ValueError( |
| 2775 | + "This method is not supported for dedicated endpoints.") |
2635 | 2776 | explain_response = self._prediction_client.explain( |
2636 | 2777 | endpoint=self.resource_name, |
2637 | 2778 | instances=instances, |
@@ -2696,6 +2837,9 @@ async def explain_async( |
2696 | 2837 | """ |
2697 | 2838 | self.wait() |
2698 | 2839 |
|
| 2840 | + if self.dedicated_endpoint_enabled: |
| 2841 | + raise ValueError( |
| 2842 | + "This method is not supported for dedicated endpoints.") |
2699 | 2843 | explain_response = await self._prediction_async_client.explain( |
2700 | 2844 | endpoint=self.resource_name, |
2701 | 2845 | instances=instances, |
@@ -3403,19 +3547,12 @@ def raw_predict( |
3403 | 3547 | "Invalid endpoint override provided. Please only use IP" |
3404 | 3548 | "address or DNS." |
3405 | 3549 | ) |
3406 | | - if not self.credentials.valid: |
3407 | | - self.credentials.refresh(google_auth_requests.Request()) |
3408 | | - |
3409 | | - token = self.credentials.token |
3410 | | - headers_with_token = dict(headers) |
3411 | | - headers_with_token["Authorization"] = f"Bearer {token}" |
3412 | | - |
3413 | 3550 | url = f"https://{endpoint_override}/v1/projects/{self.project}/locations/{self.location}/endpoints/{self.name}:rawPredict" |
3414 | 3551 | return self._http_request( |
3415 | 3552 | method="POST", |
3416 | 3553 | url=url, |
3417 | 3554 | body=body, |
3418 | | - headers=headers_with_token, |
| 3555 | + headers=headers, |
3419 | 3556 | ) |
3420 | 3557 |
|
3421 | 3558 | def explain(self): |
|