Source code for airflow.providers.google.cloud.hooks.vertex_ai.prediction_service
# Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.from__future__importannotationsfromcollections.abcimportSequencefromtypingimportTYPE_CHECKINGfromgoogle.api_core.client_optionsimportClientOptionsfromgoogle.api_core.gapic_v1.methodimportDEFAULT,_MethodDefaultfromgoogle.cloud.aiplatform_v1importPredictionServiceClientfromairflow.providers.google.common.constsimportCLIENT_INFOfromairflow.providers.google.common.hooks.base_googleimportPROVIDE_PROJECT_ID,GoogleBaseHookifTYPE_CHECKING:fromgoogle.api_core.retryimportRetryfromgoogle.cloud.aiplatform_v1.typesimportPredictResponse
[docs]classPredictionServiceHook(GoogleBaseHook):"""Hook for Google Cloud Vertex AI Prediction API."""
[docs]defget_prediction_service_client(self,region:str|None=None)->PredictionServiceClient:""" Return PredictionServiceClient object. :param region: The ID of the Google Cloud region that the service belongs to. Default is None. :return: `google.cloud.aiplatform_v1.services.prediction_service.client.PredictionServiceClient` instance. """ifregionandregion!="global":client_options=ClientOptions(api_endpoint=f"{region}-aiplatform.googleapis.com:443")else:client_options=ClientOptions()returnPredictionServiceClient(credentials=self.get_credentials(),client_info=CLIENT_INFO,client_options=client_options)
@GoogleBaseHook.fallback_to_default_project_id
[docs]defpredict(self,endpoint_id:str,instances:list[str],location:str,project_id:str=PROVIDE_PROJECT_ID,parameters:dict[str,str]|None=None,retry:Retry|_MethodDefault=DEFAULT,timeout:float|None=None,metadata:Sequence[tuple[str,str]]=(),)->PredictResponse:""" Perform an online prediction and returns the prediction result in the response. :param endpoint_id: Name of the endpoint_id requested to serve the prediction. :param instances: Required. The instances that are the input to the prediction call. A DeployedModel may have an upper limit on the number of instances it supports per request, and when it is exceeded the prediction call errors in case of AutoML Models, or, in case of customer created Models, the behaviour is as documented by that Model. :param parameters: Additional domain-specific parameters, any string must be up to 25000 characters long. :param project_id: ID of the Google Cloud project where model is located if None then default project_id is used. :param location: The location of the project. :param retry: A retry object used to retry requests. If `None` is specified, requests will not be retried. :param timeout: The amount of time, in seconds, to wait for the request to complete. Note that if `retry` is specified, the timeout applies to each individual attempt. :param metadata: Additional metadata that is provided to the method. """client=self.get_prediction_service_client(location)endpoint=f"projects/{project_id}/locations/{location}/endpoints/{endpoint_id}"returnclient.predict(request={"endpoint":endpoint,"instances":instances,"parameters":parameters},retry=retry,timeout=timeout,metadata=metadata,)