Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.cloud.aiplatform.v1.PredictResponse;
import com.google.cloud.aiplatform.v1.PredictionServiceClient;
import com.google.cloud.aiplatform.v1.PredictionServiceSettings;
import com.google.gson.JsonObject;
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
Expand Down Expand Up @@ -52,13 +53,16 @@ static void predictTextSentimentAnalysis(String project, String content, String
try (PredictionServiceClient predictionServiceClient =
PredictionServiceClient.create(predictionServiceSettings)) {
String location = "us-central1";
String jsonString = "{\"content\": \"" + content + "\"}";

// Use JsonObject to ensure safe serialization of the content; handles characters like `"`.
JsonObject contentJsonObject = new JsonObject();
contentJsonObject.addProperty("content", content);

EndpointName endpointName = EndpointName.of(project, location, endpointId);

Value parameter = Value.newBuilder().setNumberValue(0).setNumberValue(5).build();
Value.Builder instance = Value.newBuilder();
JsonFormat.parser().merge(jsonString, instance);
JsonFormat.parser().merge(contentJsonObject.toString(), instance);

List<Value> instances = new ArrayList<>();
instances.add(instance.build());
Expand Down