### 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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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