Google Gemini API error: "The response is blocked due to safety reason." although I set the harm block threshold to BLOCK_NONE
Asked Answered
O

1

7

I have this piece of code:

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

and sometimes I get this error;

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)

even I have HarmBlockThreshold.BLOCK_NONE in the settings

Outbreak answered 26/12, 2023 at 11:43 Comment(0)
D
7

Problem

You need to set the harm block threshold for a specific harm category.

Solution

Change this...

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

...to this.

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

List of all harm categories:

  • HARM_CATEGORY_SEXUALLY_EXPLICIT
  • HARM_CATEGORY_HATE_SPEECH
  • HARM_CATEGORY_HARASSMENT
  • HARM_CATEGORY_DANGEROUS_CONTENT
Dupaix answered 27/12, 2023 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.