|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import json |
| 16 | +from typing import Dict, Any |
| 17 | + |
| 18 | +from google.cloud import aiplatform |
| 19 | + |
| 20 | + |
| 21 | +# [START aiplatform_sdk_invoke_direct_deployed_model_inference_tabular_sample] |
| 22 | +def invoke_direct_deployed_model_inference_tabular_sample( |
| 23 | + project: str, |
| 24 | + location: str, |
| 25 | + endpoint_id: str, |
| 26 | + request_path: str, |
| 27 | + http_request_body: Dict[str, Any], |
| 28 | + deployed_model_id: str, |
| 29 | + stream: bool = False, |
| 30 | +): |
| 31 | + aiplatform.init(project=project, location=location) |
| 32 | + |
| 33 | + dedicated_endpoint = aiplatform.Endpoint(endpoint_id) |
| 34 | + if stream: |
| 35 | + for chunk in dedicated_endpoint.invoke( |
| 36 | + request_path=request_path, |
| 37 | + body=json.dumps(http_request_body).encode("utf-8"), |
| 38 | + headers={"Content-Type": "application/json"}, |
| 39 | + deployed_model_id=deployed_model_id, |
| 40 | + stream=True, |
| 41 | + ): |
| 42 | + print(chunk) |
| 43 | + else: |
| 44 | + response = dedicated_endpoint.invoke( |
| 45 | + request_path=request_path, |
| 46 | + body=json.dumps(http_request_body).encode("utf-8"), |
| 47 | + headers={"Content-Type": "application/json"}, |
| 48 | + deployed_model_id=deployed_model_id, |
| 49 | + ) |
| 50 | + print(response) |
| 51 | + |
| 52 | + |
| 53 | +# [END aiplatform_sdk_invoke_direct_deployed_model_inference_tabular_sample] |
0 commit comments