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

我有以下代码:

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.GoogleGeminiServiceException 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


回答:

问题

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

解决方案

将这个…

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

…改为这个。

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

所有伤害类别的列表:

  • HARM_CATEGORY_SEXUALLY_EXPLICIT
  • HARM_CATEGORY_HATE_SPEECH
  • HARM_CATEGORY_HARASSMENT
  • HARM_CATEGORY_DANGEROUS_CONTENT

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注