I am trying to call vertex AI and retrieve the data based on the query using java but getting the error “Exception in thread “main” java.lang.NoClassDefFoundError: com/google/protobuf/MapFieldReflectionAccessor”
I have tried by changing different versions of the dependencies but it is not working.
Also I have observed that MapFieldReflectionAccessor is deprecated and no more is used but I am getting this error only.
package com.multi_turn
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.cloud.aiplatform.v1.EndpointName;
import com.google.cloud.aiplatform.v1.PredictResponse;
import com.google.cloud.aiplatform.v1.PredictionServiceClient;
import com.google.cloud.aiplatform.v1.PredictionServiceSettings;
import com.google.protobuf.Value;
/*import com.google.cloud.aiplatform.v1.Value;*/
import com.google.protobuf.util.JsonFormat;
public class PredictTextPromptSample {
public static void main(String[] args) throws IOException {
String instance = "{ "prompt": " + ""Give me ten interview questions for the role of program manager."}";
System.out.println("Instance: " + instance);
String parameters = "{n" + " "temperature": 0.2,n" + " "maxOutputTokens": 256,n"
+ " "topP": 0.95,n" + " "topK": 40n" + "}";
String project = "repl-template-test";
String location = "us-central1";
String publisher = "google";
String model = "text-bison@001";
predictTextPrompt(instance, parameters, project, location, publisher, model);
}
public static void predictTextPrompt(String instance, String parameters, String project, String location,
String publisher, String model) throws IOException {
String endpoint = String.format("%s-aiplatform.googleapis.com:443", location);
PredictionServiceSettings predictionServiceSettings = PredictionServiceSettings.newBuilder()
.setEndpoint(endpoint).build();
try {
PredictionServiceClient client = PredictionServiceClient.create(predictionServiceSettings);
final EndpointName endPointName = EndpointName.ofProjectLocationPublisherModelName(project, location,
publisher, model);
Value.Builder instanceValue = Value.newBuilder();
JsonFormat.parser().merge(parameters, instanceValue);
List<Value> instances = new ArrayList<Value>();
instances.add(instanceValue.build());
Value.Builder parameterValueBuilder = Value.newBuilder();
JsonFormat.parser().merge(parameters, parameterValueBuilder);
Value parameterValue = parameterValueBuilder.build();
PredictResponse predictResponse = client.predict(endPointName, instances, parameterValue);
System.out.println(predictResponse);
} catch (Exception e) {
// TODO: handle exception
}
}
}````
````<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>multi-turn_replit</groupId>
<artifactId>vertex.ai</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>vertex.ai</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-aiplatform</artifactId>
<version>3.47.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.21.12</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven-central</id>
<name>Maven Central</name>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
</project>````
I should get the response from vertex AI instead of the error Im getting. It seems there is a compatibility issues of the versions of protobuf-java and google-cloud-aiplatform but not sure what to do.
New contributor
Benugopal Rao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.