def _Run(args, version): """Create a new Vertex AI endpoint.""" validation.ValidateDisplayName(args.display_name) region_ref = args.CONCEPTS.region.Parse() args.region = region_ref.AsDict()['locationsId'] with endpoint_util.AiplatformEndpointOverrides(version, region=args.region): endpoints_client = client.EndpointsClient(version=version) operation_client = operations.OperationsClient() if version == constants.GA_VERSION: op = endpoints_client.Create( region_ref, args.display_name, labels_util.ParseCreateArgs( args, endpoints_client.messages. GoogleCloudAiplatformV1Endpoint.LabelsValue), args.description, args.network, args.endpoint_id) else: op = endpoints_client.CreateBeta( region_ref, args.display_name, labels_util.ParseCreateArgs( args, endpoints_client.messages. GoogleCloudAiplatformV1beta1Endpoint.LabelsValue), args.description, args.network, args.endpoint_id) response_msg = operations_util.WaitForOpMaybe( operation_client, op, endpoints_util.ParseOperation(op.name)) if response_msg is not None: response = encoding.MessageToPyValue(response_msg) if 'name' in response: log.status.Print(('Created Vertex AI endpoint: {}.').format( response['name'])) return response_msg
def _Run(args, version): """Run Vertex AI online prediction.""" endpoint_ref = args.CONCEPTS.endpoint.Parse() args.region = endpoint_ref.AsDict()['locationsId'] with endpoint_util.AiplatformEndpointOverrides(version, region=args.region): if args.request.startswith('@'): request = console_io.ReadFromFileOrStdin(args.request[1:], binary=True) else: request = args.request.encode('utf-8') endpoints_client = client.EndpointsClient(version=version) _, response = endpoints_client.RawPredict(endpoint_ref, args.http_headers, request) # Workaround since gcloud only supports protobufs as JSON objects. Since # raw predict can return anything, write raw bytes to stdout. if not args.IsSpecified('format'): sys.stdout.buffer.write(response) return None # If user asked for formatting, assume it's a JSON object. try: return json.loads(response.decode('utf-8')) except ValueError: raise core_exceptions.Error( 'No JSON object could be decoded from the ' 'HTTP response body:\n' + six.text_type(response))
def _Run(args, version): """Deploy a model to an existing Vertex AI endpoint.""" validation.ValidateDisplayName(args.display_name) if version != constants.GA_VERSION: validation.ValidateAutoscalingMetricSpecs( args.autoscaling_metric_specs) endpoint_ref = args.CONCEPTS.endpoint.Parse() args.region = endpoint_ref.AsDict()['locationsId'] with endpoint_util.AiplatformEndpointOverrides(version, region=args.region): endpoints_client = client.EndpointsClient(version=version) operation_client = operations.OperationsClient() if version == constants.GA_VERSION: op = endpoints_client.DeployModel( endpoint_ref, args.model, args.region, args.display_name, machine_type=args.machine_type, accelerator_dict=args.accelerator, min_replica_count=args.min_replica_count, max_replica_count=args.max_replica_count, enable_access_logging=args.enable_access_logging, disable_container_logging=args.disable_container_logging, service_account=args.service_account, traffic_split=args.traffic_split, deployed_model_id=args.deployed_model_id) else: op = endpoints_client.DeployModelBeta( endpoint_ref, args.model, args.region, args.display_name, machine_type=args.machine_type, accelerator_dict=args.accelerator, min_replica_count=args.min_replica_count, max_replica_count=args.max_replica_count, autoscaling_metric_specs=args.autoscaling_metric_specs, enable_access_logging=args.enable_access_logging, enable_container_logging=args.enable_container_logging, service_account=args.service_account, traffic_split=args.traffic_split, deployed_model_id=args.deployed_model_id) response_msg = operations_util.WaitForOpMaybe( operation_client, op, endpoints_util.ParseOperation(op.name)) if response_msg is not None: response = encoding.MessageToPyValue(response_msg) if 'deployedModel' in response and 'id' in response[ 'deployedModel']: log.status.Print(('Deployed a model to the endpoint {}. ' 'Id of the deployed model: {}.').format( endpoint_ref.AsDict()['endpointsId'], response['deployedModel']['id'])) return response_msg
def _Run(args, version): """Undeploy a model fro man existing AI Platform endpoint.""" endpoint_ref = args.CONCEPTS.endpoint.Parse() args.region = endpoint_ref.AsDict()['locationsId'] with endpoint_util.AiplatformEndpointOverrides(version, region=args.region): endpoints_client = client.EndpointsClient(version=version) operation_client = operations.OperationsClient() op = endpoints_client.UndeployModelBeta(endpoint_ref, args) return operations_util.WaitForOpMaybe( operation_client, op, endpoints_util.ParseOperation(op.name))
def _Run(args, version): """Delete an existing Vertex AI endpoint.""" endpoint_ref = args.CONCEPTS.endpoint.Parse() args.region = endpoint_ref.AsDict()['locationsId'] endpoint_id = endpoint_ref.AsDict()['endpointsId'] with endpoint_util.AiplatformEndpointOverrides(version, region=args.region): endpoints_client = client.EndpointsClient(version=version) operation_client = operations.OperationsClient() console_io.PromptContinue( 'This will delete endpoint [{}]...'.format(endpoint_id), cancel_on_no=True) op = endpoints_client.Delete(endpoint_ref) return operations_util.WaitForOpMaybe( operation_client, op, endpoints_util.ParseOperation(op.name))
def _Run(args, version): """Run AI Platform online prediction.""" endpoint_ref = args.CONCEPTS.endpoint.Parse() args.region = endpoint_ref.AsDict()['locationsId'] with endpoint_util.AiplatformEndpointOverrides( version, region=args.region, is_prediction=True): endpoints_client = client.EndpointsClient(version=version) instances_json = endpoints_util.ReadInstancesFromArgs(args.json_request) results = endpoints_client.PredictBeta(endpoint_ref, instances_json) if not args.IsSpecified('format'): # default format is based on the response. args.format = endpoints_util.GetDefaultFormat(results.predictions) return results
def _Run(args, version): """Update an existing Vertex AI endpoint.""" validation.ValidateDisplayName(args.display_name) endpoint_ref = args.CONCEPTS.endpoint.Parse() args.region = endpoint_ref.AsDict()['locationsId'] with endpoint_util.AiplatformEndpointOverrides(version, region=args.region): endpoints_client = client.EndpointsClient(version=version) def GetLabels(): return endpoints_client.Get(endpoint_ref).labels try: if version == constants.GA_VERSION: op = endpoints_client.Patch( endpoint_ref, labels_util.ProcessUpdateArgsLazy( args, endpoints_client.messages. GoogleCloudAiplatformV1Endpoint.LabelsValue, GetLabels), display_name=args.display_name, description=args.description, traffic_split=args.traffic_split, clear_traffic_split=args.clear_traffic_split) else: op = endpoints_client.PatchBeta( endpoint_ref, labels_util.ProcessUpdateArgsLazy( args, endpoints_client.messages. GoogleCloudAiplatformV1beta1Endpoint.LabelsValue, GetLabels), display_name=args.display_name, description=args.description, traffic_split=args.traffic_split, clear_traffic_split=args.clear_traffic_split) except errors.NoFieldsSpecifiedError: available_update_args = [ 'display_name', 'traffic_split', 'clear_traffic_split', 'update_labels', 'clear_labels', 'remove_labels', 'description' ] if not any(args.IsSpecified(arg) for arg in available_update_args): raise log.status.Print('No update to perform.') return None else: log.UpdatedResource(op.name, kind='Vertex AI endpoint') return op
def _Run(args, version): """Create a new AI Platform endpoint.""" validation.ValidateDisplayName(args.display_name) region_ref = args.CONCEPTS.region.Parse() args.region = region_ref.AsDict()['locationsId'] with endpoint_util.AiplatformEndpointOverrides(version, region=args.region): endpoints_client = client.EndpointsClient(version=version) operation_client = operations.OperationsClient() op = endpoints_client.CreateBeta(region_ref, args) response_msg = operations_util.WaitForOpMaybe( operation_client, op, endpoints_util.ParseOperation(op.name)) if response_msg is not None: response = encoding.MessageToPyValue(response_msg) if 'name' in response: log.status.Print(('Created AI Platform endpoint: {}.').format( response['name'])) return response_msg
def _Run(args, version): """Undeploy a model fro man existing Vertex AI endpoint.""" endpoint_ref = args.CONCEPTS.endpoint.Parse() args.region = endpoint_ref.AsDict()['locationsId'] with endpoint_util.AiplatformEndpointOverrides(version, region=args.region): endpoints_client = client.EndpointsClient(version=version) operation_client = operations.OperationsClient() if version == constants.GA_VERSION: op = endpoints_client.UndeployModel( endpoint_ref, args.deployed_model_id, traffic_split=args.traffic_split) else: op = endpoints_client.UndeployModelBeta( endpoint_ref, args.deployed_model_id, traffic_split=args.traffic_split) return operations_util.WaitForOpMaybe( operation_client, op, endpoints_util.ParseOperation(op.name))
def _Run(args, version): """Run AI Platform online explanation.""" endpoint_ref = args.CONCEPTS.endpoint.Parse() args.region = endpoint_ref.AsDict()['locationsId'] with endpoint_util.AiplatformEndpointOverrides( version, region=args.region, is_prediction=True): endpoints_client = client.EndpointsClient(version=version) instances_json = endpoints_util.ReadInstancesFromArgs(args.json_request) results = endpoints_client.ExplainBeta(endpoint_ref, instances_json, args) if getattr(results, 'deployedModelId') is not None: log.status.Print( 'Deployed model id to be used for explanation: {}'.format( results.deployedModelId)) if not args.IsSpecified('format'): # default format is based on the response. args.format = endpoints_util.GetDefaultFormat( results, key_name='explanations') return results
def _Run(args, version): """Deploy a model to an existing AI Platform endpoint.""" validation.ValidateDisplayName(args.display_name) endpoint_ref = args.CONCEPTS.endpoint.Parse() args.region = endpoint_ref.AsDict()['locationsId'] with endpoint_util.AiplatformEndpointOverrides(version, region=args.region): endpoints_client = client.EndpointsClient(version=version) operation_client = operations.OperationsClient() op = endpoints_client.DeployModelBeta(endpoint_ref, args) response_msg = operations_util.WaitForOpMaybe( operation_client, op, endpoints_util.ParseOperation(op.name)) if response_msg is not None: response = encoding.MessageToPyValue(response_msg) if 'deployedModel' in response and 'id' in response[ 'deployedModel']: log.status.Print(('Deployed a model to the endpoint {}. ' 'Id of the deployed model: {}.').format( endpoint_ref.AsDict()['endpointsId'], response['deployedModel']['id'])) return response_msg
def _Run(args, version): """Update an existing AI Platform endpoint.""" validation.ValidateDisplayName(args.display_name) endpoint_ref = args.CONCEPTS.endpoint.Parse() args.region = endpoint_ref.AsDict()['locationsId'] with endpoint_util.AiplatformEndpointOverrides(version, region=args.region): endpoints_client = client.EndpointsClient(version=version) try: op = endpoints_client.PatchBeta(endpoint_ref, args) except errors.NoFieldsSpecifiedError: available_update_args = [ 'display_name', 'traffic_split', 'clear_traffic_split', 'update_labels', 'clear_labels', 'remove_labels', 'description' ] if not any(args.IsSpecified(arg) for arg in available_update_args): raise log.status.Print('No update to perform.') return None else: log.UpdatedResource(op.name, kind='AI Platform endpoint') return op
def _Run(args, version): region_ref = args.CONCEPTS.region.Parse() args.region = region_ref.AsDict()['locationsId'] with endpoint_util.AiplatformEndpointOverrides(version, region=args.region): return client.EndpointsClient(version=version).List(region_ref)