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

frebpwbc  于 2024-01-06  发布在  Spring
关注(0)|答案(1)|浏览(184)

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

  1. public static void main(String[] args) throws Exception {
  2. GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
  3. configBuilder.temperature = 0.9f;
  4. configBuilder.topK = 16;
  5. configBuilder.topP = 0.1f;
  6. configBuilder.maxOutputTokens = 200;
  7. configBuilder.stopSequences = Arrays.asList("red");
  8. 'Builder()' has private access in 'com.google.cloud.vertexai.api.GenerationConfig.Builder'
  9. GenerationConfig generationConfig = configBuilder.build();
  10. GenerativeModel gm = new GenerativeModel(
  11. "MODEL_NAME",
  12. BuildConfig.apiKey,
  13. generationConfig
  14. );
  15. GenerativeModelFutures model = GenerativeModelFutures.from(gm);

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

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


我的Maven:

  1. <dependency>
  2. <groupId>com.google.cloud</groupId>
  3. <artifactId>google-cloud-vertexai</artifactId>
  4. <version>0.1.0</version>
  5. </dependency>

bmp9r5qi

bmp9r5qi1#

我认为你正在尝试遵循Android Google文档中此页面提供的示例。
根据该文档,假设您使用的是Gradle,则需要在项目中配置以下依赖项:

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

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

  1. <dependency>
  2. <groupId>com.google.ai.client.generativeai</groupId>
  3. <artifactId>generativeai</artifactId>
  4. <version>0.1.1</version>
  5. </dependency>


而不是你提供的那个

  1. <dependency>
  2. <groupId>com.google.cloud</groupId>
  3. <artifactId>google-cloud-vertexai</artifactId>
  4. <version>0.1.0</version>
  5. </dependency>


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

  1. <repositories>
  2. <repository>
  3. <id>google</id>
  4. <url>https://maven.google.com/</url>
  5. </repository>
  6. </repositories>


在任何情况下,您都可以使用已经在pom.xml中配置的Vertex AI实现类似的结果,使用类似于以下代码(基于official documentation):

  1. import com.google.cloud.vertexai.VertexAI;
  2. import com.google.cloud.vertexai.api.GenerateContentResponse;
  3. import com.google.cloud.vertexai.api.GenerationConfig;
  4. import com.google.cloud.vertexai.generativeai.preview.GenerativeModel;
  5. public class GeminiAIExample {
  6. public static void main(String[] args) throws Exception {
  7. // Provide the required information about your Vertex AI GCP project
  8. String projectId = "your-google-cloud-project-id";
  9. String location = "us-central1";
  10. String modelName = "gemini-pro-vision";
  11. String textPrompt = "your-text-here";
  12. try (VertexAI vertexAI = new VertexAI(projectId, location)) {
  13. GenerationConfig generationConfig =
  14. GenerationConfig.newBuilder()
  15. .setTemperature(0.9F)
  16. .setTopK(16)
  17. .setTopP(0.1f)
  18. .setMaxOutputTokens(200)
  19. .setStopSequences(0, "red")
  20. .build();
  21. GenerativeModel model = new GenerativeModel(modelName, generationConfig, vertexAI);
  22. GenerateContentResponse response = model.generateContent(textPrompt);
  23. // Please, include the safety checks about the number of candidates, etc, you consider appropriate
  24. System.out.println(response.getCandidates(0).getContent());
  25. }
  26. }
  27. }


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

展开查看全部

相关问题