Spring Boot 为Google Gemini创建一个GenerationConfig- 'Builder()'具有私有访问权限

frebpwbc  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(108)

我想为Google Gemini创建一个新的GenerationConfig,在我的代码中有:

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);

字符串
但我有这个编译错误:

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


我的Maven:

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

bmp9r5qi

bmp9r5qi1#

我认为你正在尝试遵循Android Google文档中此页面提供的示例。
根据该文档,假设您使用的是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")
}

字符串
请注意,依赖项com.google.ai.client.generativeai:generativeai:0.1.1托管在Google Maven Repository中,而不是Maven Central中。
如果你想继续使用Maven,你可以尝试使用following dependency

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


而不是你提供的那个

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


如上所述,您可能需要在pom.xml中包含对Google Maven Repository的引用:

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


在任何情况下,您都可以使用已经在pom.xml中配置的Vertex AI实现类似的结果,使用类似于以下代码(基于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());
    }
  }
}


请考虑审查thisthis other博客条目,我认为他们也可以帮助。

相关问题