Search code examples
javaspring-bootgoogle-cloud-platformgoogle-cloud-vertex-aigoogle-gemini

create a GenerationConfig for Google Gemini - 'Builder()' has private access


I would like to create a new GenerationConfig for Google Gemini, having this in my code:

public static void main(String[] args) throws Exception {

   
    GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
    configBuilder.temperature = 0.9f;
    configBuilder.topK = 16;
    configBuilder.topP = 0.1f;
    configBuilder.maxOutputTokens = 200;
    configBuilder.stopSequences = Arrays.asList("red");

    'Builder()' has private access in 'com.google.cloud.vertexai.api.GenerationConfig.Builder'

    GenerationConfig generationConfig = configBuilder.build();

    GenerativeModel gm = new GenerativeModel(
            "MODEL_NAME",
            BuildConfig.apiKey,
            generationConfig
    );

    GenerativeModelFutures model = GenerativeModelFutures.from(gm);

but I have this compilation error:

 'Builder()' has private access in 'com.google.cloud.vertexai.api.GenerationConfig.Builder'

my maven :

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-vertexai</artifactId>
    <version>0.1.0</version>
</dependency>

Solution

  • I think you are trying following the example provided in this page in the Android Google Documentation.

    According to that documentation, you need to configure the following dependencies in your project, assuming you are using Gradle:

    dependencies {
        // ... other androidx dependencies
    
        // add the dependency for the Google AI client SDK for Android
        implementation("com.google.ai.client.generativeai:generativeai:0.1.1")
    
        // Required for one-shot operations (to use `ListenableFuture` from Reactive Streams)
        implementation("com.google.guava:guava:31.0.1-android")
    
        // Required for streaming operations (to use `Publisher` from Guava Android)
        implementation("org.reactivestreams:reactive-streams:1.0.4")
    }
    

    Please, note that the dependency com.google.ai.client.generativeai:generativeai:0.1.1 is hosted in the Google Maven Repository, not in Maven Central.

    If you want to keep using Maven you can try using the following dependency:

    <dependency>
        <groupId>com.google.ai.client.generativeai</groupId>
        <artifactId>generativeai</artifactId>
        <version>0.1.1</version>
    </dependency>
    

    instead of the one you provided:

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-vertexai</artifactId>
        <version>0.1.0</version>
    </dependency>
    

    As indicated, you probably will need to include a reference to the Google Maven Repository in your pom.xml:

    <repositories>
        <repository>
            <id>google</id>
            <url>https://maven.google.com/</url>
        </repository>
    </repositories>
    

    In any case, you can achieve similar results using the Vertex AI you already configured in your pom.xml using a code similar to the following one (based in the official documentation):

    import com.google.cloud.vertexai.VertexAI;
    import com.google.cloud.vertexai.api.GenerateContentResponse;
    import com.google.cloud.vertexai.api.GenerationConfig;
    import com.google.cloud.vertexai.generativeai.preview.GenerativeModel;
    
    public class GeminiAIExample {
      public static void main(String[] args) throws Exception {
        // Provide the required information about your Vertex AI GCP project
        String projectId = "your-google-cloud-project-id";
        String location = "us-central1";
        String modelName = "gemini-pro-vision";
        String textPrompt = "your-text-here";
    
        try (VertexAI vertexAI = new VertexAI(projectId, location)) {
          GenerationConfig generationConfig =
              GenerationConfig.newBuilder()
                  .setTemperature(0.9F)
                  .setTopK(16)
                  .setTopP(0.1f)
                  .setMaxOutputTokens(200)
                  .setStopSequences(0, "red")
                  .build();
    
          GenerativeModel model = new GenerativeModel(modelName, generationConfig, vertexAI);
    
          GenerateContentResponse response = model.generateContent(textPrompt);
          
          // Please, include the safety checks about the number of candidates, etc, you consider appropriate
          System.out.println(response.getCandidates(0).getContent());
        }
      }
    }
    

    Please, consider review this or this other blog entries, I think they could be of help as well.