Spring Boot Google Gemini API错误:“由于安全原因,响应被阻止,”尽管我将危害阻止阈值设置为BLOCK_NONE

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

我有这样一段代码:

  1. public static String simpleQuestion(String projectId, String location, String modelName) throws Exception {
  2. // Initialize client that will be used to send requests.
  3. // This client only needs to be created once, and can be reused for multiple requests.
  4. try (VertexAI vertexAI = new VertexAI(projectId, location)) {
  5. String output;
  6. GenerativeModel model = new GenerativeModel(modelName, vertexAI);
  7. model.setGenerationConfig(GenerationConfig.newBuilder().build());
  8. model.setSafetySettings(Collections.singletonList(
  9. SafetySetting.newBuilder()
  10. .setThreshold(SafetySetting
  11. .HarmBlockThreshold.BLOCK_NONE).build()));
  12. GenerateContentResponse response = model.generateContent("What can you tell me about Pythagorean theorem");
  13. output = ResponseHandler.getText(response);
  14. return output;
  15. }
  16. }

字符串
有时我会犯这样的错误

  1. context/6.1.1/spring-context-6.1.1.jar com.mysticriver.service.GoogleGeminiService
  2. Exception in thread "main" java.lang.IllegalArgumentException: The response is blocked due to safety reason.
  3. at com.google.cloud.vertexai.generativeai.preview.ResponseHandler.getText(ResponseHandler.java:46)


即使我在设置中有HarmBlockThreshold.BLOCK_NONE

ecfsfe2w

ecfsfe2w1#

问题

您需要为特定伤害类别设置伤害阻止阈值。

解决方案

把这个换了...

  1. SafetySetting.newBuilder()
  2. .setThreshold(SafetySetting.HarmBlockThreshold.BLOCK_NONE)
  3. .build()

字符串
...这个

  1. SafetySetting.newBuilder()
  2. .setCategory(HarmCategory.HARM_CATEGORY_YOUR_CATEGORY)
  3. .setThreshold(SafetySetting.HarmBlockThreshold.BLOCK_NONE)
  4. .build()


所有伤害类别列表:

  • 第一个月
  • HARM_CATEGORY_HATE_SPEECH
  • HARM_CATEGORY_HARASSMENT
  • HARM_CATEGORY_DANGEROUS_CONTENT
展开查看全部

相关问题