8000 feat: text sentiment analysis sample for Vertex LLMs (#8229) · nes-a-cti/java-docs-samples@c00dd42 · GitHub
[go: up one dir, main page]

Skip to content

Commit c00dd42

Browse files
authored
feat: text sentiment analysis sample for Vertex LLMs (GoogleCloudPlatform#8229)
* feat: text sentiment analysis sample for large language models * addressed lint errors * Improved code comments
1 parent 6268add commit c00dd42

File tree

2 files changed

+252
-0
lines changed

2 files changed

+252
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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]
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class PredictTextSentimentSampleTest {
31+
32+
private static final String PROJECT = System.getenv("UCAIP_PROJECT_ID");
33+
private static final String INSTANCE =
34+
"{ \"content\": \"I had to compare two versions of Hamlet for my Shakespeare \n"
35+
+ "class and unfortunately I picked this version. Everything from the acting \n"
36+
+ "(the actors deliver most of their lines directly to the camera) to the camera \n"
37+
+ "shots (all medium or close up shots...no scenery shots and very little back \n"
38+
+ "ground in the shots) were absolutely terrible. I watched this over my spring \n"
39+
+ "break and it is very safe to say that I feel that I was gypped out of 114 \n"
40+
+ "minutes of my vacation. Not recommended by any stretch of the imagination.\n"
41+
+ "Classify the sentiment of the message: negative\n"
42+
+ "\n"
43+
+ "Something surprised me about this movie - it was actually original. It was \n"
44+
+ "not the same old recycled crap that comes out of Hollywood every month. I saw \n"
45+
+ "this movie on video because I did not even know about it before I saw it at my \n"
46+
+ "local video store. If you see this movie available - rent it - you will not \n"
47+
+ "regret it.\n"
48+
+ "Classify the sentiment of the message: positive\n"
49+
+ "\n"
50+
+ "My family has watched Arthur Bach stumble and stammer since the movie first \n"
51+
+ "came out. We have most lines memorized. I watched it two weeks ago and still \n"
52+
+ "get tickled at the simple humor and view-at-life that Dudley Moore portrays. \n"
53+
+ "Liza Minelli did a wonderful job as the side kick - though I'm not her \n"
54+
+ "biggest fan. This movie makes me just enjoy watching movies. My favorite scene \n"
55+
+ "is when Arthur is visiting his fiancée's house. His conversation with the \n"
56+
+ "butler and Susan's father is side-spitting. The line from the butler, \n"
57+
+ "\\\"Would you care to wait in the Library\\\" followed by Arthur's reply, \n"
58+
+ "\\\"Yes I would, the bathroom is out of the question\\\", is my NEWMAIL \n"
59+
+ "notification on my computer.\n"
60+
+ "Classify the sentiment of the message: positive\n"
61+
+ "\n"
62+
+ "This Charles outing is decent but this is a pretty low-key performance. Marlon \n"
63+
+ "Brando stands out. There's a subplot with Mira Sorvino and Donald Sutherland \n"
64+
+ "that forgets to develop and it hurts the film a little. I'm still trying to \n"
65+
+ "figure out why Charlie want to change his name.\n"
66+
+ "Classify the sentiment of the message: negative\n"
67+
+ "\n"
68+
+ "Tweet: The Pixel 7 Pro, is too big to fit in my jeans pocket, so I bought new \n"
69+
+ "jeans.\n"
70+
+ "Classify the sentiment of the message: \"}";
71+
private static final String PARAMETERS =
72+
"{\n"
73+
+ " \"temperature\": 0,\n"
74+
+ " \"maxDecodeSteps\": 5,\n"
75+
+ " \"topP\": 0,\n"
76+
+ " \"topK\": 1\n"
77+
+ "}";
78+
private static final String PUBLISHER = "google";
79+
private static final String LOCATION = "us-central1";
80+
private static final String MODEL = "text-bison@001";
81+
82+
private ByteArrayOutputStream bout;
83+
private PrintStream out;
84+
private PrintStream originalPrintStream;
85+
86+
private static void requireEnvVar(String varName) {
87+
String errorMessage =
88+
String.format("Environment variable '%s' is required to perform these tests.", varName);
89+
assertNotNull(errorMessage, System.getenv(varName));
90+
}
91+
92+
@BeforeClass
93+
public static void checkRequirements() {
94+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
95+
requireEnvVar("UCAIP_PROJECT_ID");
96+
}
97+
98+
@Before
99+
public void setUp() {
100+
bout = new ByteArrayOutputStream();
101+
out = new PrintStream(bout);
102+
originalPrintStream = System.out;
103+
System.setOut(out);
104+
}
105+
106+
@After
107+
public void tearDown() {
108+
System.out.flush();
109+
System.setOut(originalPrintStream);
110+
}
111+
112+
@Test
113+
public void testPredictTextSentiment() throws IOException {
114+
// Act
115+
PredictTextSentimentSample.predictTextSentiment(
116+
INSTANCE, PARAMETERS, PROJECT, LOCATION, PUBLISHER, MODEL);
117+
118+
// Assert
119+
String got = bout.toString();
120+
assertThat(got).contains("Predict Response");
121+
}
122+
}

0 commit comments

Comments
 (0)
0