|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package aiplatform; |
| 18 | + |
| 19 | +// [START aiplatform_batch_text_predict] |
| 20 | + |
| 21 | +import com.google.cloud.aiplatform.v1.BatchPredictionJob; |
| 22 | +import com.google.cloud.aiplatform.v1.BatchPredictionJob.InputConfig; |
| 23 | +import com.google.cloud.aiplatform.v1.BatchPredictionJob.OutputConfig; |
| 24 | +import com.google.cloud.aiplatform.v1.GcsDestination; |
| 25 | +import com.google.cloud.aiplatform.v1.GcsSource; |
| 26 | +import com.google.cloud.aiplatform.v1.JobServiceClient; |
| 27 | +import com.google.cloud.aiplatform.v1.JobServiceSettings; |
| 28 | +import com.google.cloud.aiplatform.v1.LocationName; |
| 29 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 30 | +import com.google.protobuf.Value; |
| 31 | +import com.google.protobuf.util.JsonFormat; |
| 32 | +import java.io.IOException; |
| 33 | + |
| 34 | + |
| 35 | +public class BatchTextPredictionSample { |
| 36 | + |
| 37 | + public static void main(String[] args) throws IOException { |
| 38 | + // TODO (Developer): Replace the input_uri and output_uri with your own GCS paths |
| 39 | + String project = "YOUR_PROJECT_ID"; |
| 40 | + String location = "us-central1"; |
| 41 | + // inputUri (str, optional): URI of the input dataset. |
| 42 | + // Could be a BigQuery table or a Google Cloud Storage file. |
| 43 | + // E.g. "gs://[BUCKET]/[DATASET].jsonl" OR "bq://[PROJECT].[DATASET].[TABLE]" |
| 44 | + String inputUri = "gs://cloud-samples-data/batch/prompt_for_batch_text_predict.jsonl"; |
| 45 | + // outputUri (str, optional): URI where the output will be stored. |
| 46 | + // Could be a BigQuery table or a Google Cloud Storage file. |
| 47 | + // E.g. "gs://[BUCKET]/[OUTPUT].jsonl" OR "bq://[PROJECT].[DATASET].[TABLE]" |
| 48 | + String outputUri = "gs://batch-bucket-testing/batch_text_predict_output"; |
| 49 | + String codeModel = "text-bison"; |
| 50 | + |
| 51 | + batchTextPrediction(project, location, inputUri, outputUri, codeModel); |
| 52 | + } |
| 53 | + |
| 54 | + // Perform batch text prediction using a pre-trained text generation model. |
| 55 | + // Example of using Google Cloud Storage bucket as the input and output data source |
| 56 | + public static void batchTextPrediction( |
| 57 | + String project, String location, String inputUri, |
| 58 | + String outputUri, String codeModel) throws IOException { |
| 59 | + String endpoint = String.format("%s-aiplatform.googleapis.com:443", location); |
| 60 | + JobServiceSettings jobServiceSettings = |
| 61 | + JobServiceSettings.newBuilder().setEndpoint(endpoint).build(); |
| 62 | + // Construct your modelParameters |
| 63 | + String parameters = |
| 64 | + "{\n" + " \"temperature\": 0.2,\n" + " \"maxOutputTokens\": 200\n" + "}"; |
| 65 | + Value parameterValue = stringToValue(parameters); |
| 66 | + String modelName = String.format( |
| 67 | + "projects/%s/locations/%s/publishers/google/models/%s", project, location, codeModel); |
| 68 | + |
| 69 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 70 | + // once, and can be reused for multiple requests. |
| 71 | + try (JobServiceClient jobServiceClient = JobServiceClient.create(jobServiceSettings)) { |
| 72 | + |
| 73 | + GcsSource.Builder gcsSource = GcsSource.newBuilder(); |
| 74 | + gcsSource.addUris(inputUri); |
| 75 | + InputConfig inputConfig = |
| 76 | + InputConfig.newBuilder() |
| 77 | + .setGcsSource(gcsSource) |
| 78 | + .setInstancesFormat("jsonl") |
| 79 | + .build(); |
| 80 | + |
| 81 | + GcsDestination.Builder gcsDestination = GcsDestination.newBuilder(); |
| 82 | + gcsDestination.setOutputUriPrefix(outputUri); |
| 83 | + OutputConfig outputConfig = |
| 84 | + OutputConfig.newBuilder() |
| 85 | + .setGcsDestination(gcsDestination) |
| 86 | + .setPredictionsFormat("jsonl") |
| 87 | + .build(); |
| 88 | + |
| 89 | + BatchPredictionJob.Builder batchPredictionJob = |
| 90 | + BatchPredictionJob.newBuilder() |
| 91 | + .setDisplayName("my batch text prediction job " + System.currentTimeMillis()) |
| 92 | + .setModel(modelName) |
| 93 | + .setInputConfig(inputConfig) |
| 94 | + .setOutputConfig(outputConfig) |
| 95 | + .setModelParameters(parameterValue); |
| 96 | + |
| 97 | + LocationName parent = LocationName.of(project, location); |
| 98 | + BatchPredictionJob response = |
| 99 | + jobServiceClient.createBatchPredictionJob(parent, batchPredictionJob.build()); |
| 100 | + |
| 101 | + System.out.format("response: %s\n", response); |
| 102 | + System.out.format("\tName: %s\n", response.getName()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Convert a Json string to a protobuf.Value |
| 107 | + static Value stringToValue(String value) throws InvalidProtocolBufferException { |
| 108 | + Value.Builder builder = Value.newBuilder(); |
| 109 | + JsonFormat.parser().merge(value, builder); |
| 110 | + return builder.build(); |
| 111 | + } |
| 112 | +} |
| 113 | +// [END aiplatform_batch_text_predict] |
0 commit comments