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

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

我有这样一段代码:

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

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

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


即使我在设置中有HarmBlockThreshold.BLOCK_NONE

ecfsfe2w

ecfsfe2w1#

问题

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

解决方案

把这个换了...

SafetySetting.newBuilder()
  .setThreshold(SafetySetting.HarmBlockThreshold.BLOCK_NONE)
  .build()

字符串
...这个

SafetySetting.newBuilder()
  .setCategory(HarmCategory.HARM_CATEGORY_YOUR_CATEGORY)
  .setThreshold(SafetySetting.HarmBlockThreshold.BLOCK_NONE)
  .build()


所有伤害类别列表:

  • 第一个月
  • HARM_CATEGORY_HATE_SPEECH
  • HARM_CATEGORY_HARASSMENT
  • HARM_CATEGORY_DANGEROUS_CONTENT

相关问题