|
| 1 | +/* |
| 2 | + * Copyright 2023 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_sdk_sentiment_analysis] |
| 20 | + |
| 21 | +import com.google.cloud.aiplatform.v1beta1.EndpointName; |
| 22 | +import com.google.cloud.aiplatform.v1beta1.PredictResponse; |
| 23 | +import com.google.cloud.aiplatform.v1beta1.PredictionServiceClient; |
| 24 | +import com.google.cloud.aiplatform.v1beta1.PredictionServiceSettings; |
| 25 | +import com.google.protobuf.Value; |
| 26 | +import com.google.protobuf.util.JsonFormat; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +// Text sentiment analysis with a Large Language Model |
| 32 | +public class PredictTextSentimentSample { |
| 33 | + |
| 34 | + public static void main(String[] args) throws IOException { |
| 35 | + // TODO(developer): Replace these variables before running the sample. |
| 36 | + // The details of designing text prompts for supported large language models: |
| 37 | + // https://cloud.google.com/vertex-ai/docs/generative-ai/text/text-overview |
| 38 | + String instance = |
| 39 | + "{ \"content\": \"I had to compare two versions of Hamlet for my Shakespeare \n" |
| 40 | + + "class and unfortunately I picked this version. Everything from the acting \n" |
| 41 | + + "(the actors deliver most of their lines directly to the camera) to the camera \n" |
| 42 | + + "shots (all medium or close up shots...no scenery shots and very little back \n" |
| 43 | + + "ground in the shots) were absolutely terrible. I watched this over my spring \n" |
| 44 | + + "break and it is very safe to say that I feel that I was gypped out of 114 \n" |
| 45 | + + "minutes of my vacation. Not recommended by any stretch of the imagination.\n" |
| 46 | + + "Classify the sentiment of the message: negative\n" |
| 47 | + + "\n" |
| 48 | + + "Something surprised me about this movie - it was actually original. It was \n" |
| 49 | + + "not the same old recycled crap that comes out of Hollywood every month. I saw \n" |
| 50 | + + "this movie on video because I did not even know about it before I saw it at my \n" |
| 51 | + + "local video store. If you see this movie available - rent it - you will not \n" |
| 52 | + + "regret it.\n" |
| 53 | + + "Classify the sentiment of the message: positive\n" |
| 54 | + + "\n" |
| 55 | + + "My family has watched Arthur Bach stumble and stammer since the movie first \n" |
| 56 | + + "came out. We have most lines memorized. I watched it two weeks ago and still \n" |
| 57 | + + "get tickled at the simple humor and view-at-life that Dudley Moore portrays. \n" |
| 58 | + + "Liza Minelli did a wonderful job as the side kick - though I'm not her \n" |
| 59 | + + "biggest fan. This movie makes me just enjoy watching movies. My favorite scene \n" |
| 60 | + + "is when Arthur is visiting his fiancée's house. His conversation with the \n" |
| 61 | + + "butler and Susan's father is side-spitting. The line from the butler, \n" |
| 62 | + + "\\\"Would you care to wait in the Library\\\" followed by Arthur's reply, \n" |
| 63 | + + "\\\"Yes I would, the bathroom is out of the question\\\", is my NEWMAIL \n" |
| 64 | + + "notification on my computer.\n" |
| 65 | + + "Classify the sentiment of the message: positive\n" |
| 66 | + + "\n" |
| 67 | + + "This Charles outing is decent but this is a pretty low-key performance. Marlon \n" |
| 68 | + + "Brando stands out. There's a subplot with Mira Sorvino and Donald Sutherland \n" |
| 69 | + + "that forgets to develop and it hurts the film a little. I'm still trying to \n" |
| 70 | + + "figure out why Charlie want to change his name.\n" |
| 71 | + + "Classify the sentiment of the message: negative\n" |
| 72 | + + "\n" |
| 73 | + + "Tweet: The Pixel 7 Pro, is too big to fit in my jeans pocket, so I bought new \n" |
| 74 | + + "jeans.\n" |
| 75 | + + "Classify the sentiment of the message: \"}"; |
| 76 | + String parameters = |
| 77 | + "{\n" |
| 78 | + + " \"temperature\": 0,\n" |
| 79 | + + " \"maxDecodeSteps\": 5,\n" |
| 80 | + + " \"topP\": 0,\n" |
| 81 | + + " \"topK\": 1\n" |
| 82 | + + "}"; |
| 83 | + String project = "YOUR_PROJECT_ID"; |
| 84 | + String location = "us-central1"; |
| 85 | + String publisher = "google"; |
| 86 | + String model = "text-bison@001"; |
| 87 | + |
| 88 | + predictTextSentiment(instance, parameters, project, location, publisher, model); |
| 89 | + } |
| 90 | + |
| 91 | + static void predictTextSentiment( |
| 92 | + String instance, |
| 93 | + String parameters, |
| 94 | + String project, |
| 95 | + String location, |
| 96 | + String publisher, |
| 97 | + String model) |
| 98 | + throws IOException { |
| 99 | + String endpoint = String.format("%s-aiplatform.googleapis.com:443", location); |
| 100 | + PredictionServiceSettings predictionServiceSettings = |
| 101 | + PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build(); |
| 102 | + |
| 103 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 104 | + // once, and can be reused for multiple requests. |
| 105 | + try (PredictionServiceClient predictionServiceClient = |
| 106 | + PredictionServiceClient.create(predictionServiceSettings)) { |
| 107 | + final EndpointName endpointName = |
| 108 | + EndpointName.ofProjectLocationPublisherModelName(project, location, publisher, model); |
| 109 | + |
| 110 | + // Use Value.Builder to convert instance to a dynamically typed value that can be |
| 111 | + // processed by the service. |
| 112 | + Value.Builder instanceValue = Value.newBuilder(); |
| 113 | + JsonFormat.parser().merge(instance, instanceValue); |
| 114 | + List<Value> instances = new ArrayList<>(); |
| 115 | + instances.add(instanceValue.build()); |
| 116 | + |
| 117 | + // Use Value.Builder to convert parameter to a dynamically typed value that can be |
| 118 | + // processed by the service. |
| 119 | + Value.Builder parameterValueBuilder = Value.newBuilder(); |
| 120 | + JsonFormat.parser().merge(parameters, parameterValueBuilder); |
| 121 | + Value parameterValue = parameterValueBuilder.build(); |
| 122 | + |
| 123 | + PredictResponse predictResponse = |
| 124 | + predictionServiceClient.predict(endpointName, instances, parameterValue); |
| 125 | + System.out.println("Predict Response"); |
| 126 | + System.out.println(predictResponse); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | +// [END aiplatform_sdk_sentiment_analysis] |
0 commit comments