Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Gemini API: Safety Settings doesn't work

I work on translator, using gemini. I use multi turn mode, cause history and context are important. Sometimes it breaks due to safety reason. I tried to disable safety settings, but it doesn't work or I do something wrong.

Here is my current safety settings (but I tried different variants).

const safe = [
    {
        "category": "HARM_CATEGORY_HARASSMENT",
        "threshold": "BLOCK_NONE",
    },
    {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "threshold": "BLOCK_NONE",
    },
    {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "threshold": "BLOCK_NONE",
    },
    {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "threshold": "BLOCK_NONE",
    },
]

Here is code, which I use for translations.

async function run() {
    // For text-only input, use the gemini-pro model
    const model = genAI.getGenerativeModel( { model: "gemini-pro", safe } );

    chat = model.startChat({
    });
    
    console.log(model);
    
    const msg = readFile("./description.txt");

    const result = await chat.sendMessage(msg);
    const response = await result.response;
    const text = response.text();
    console.log(response.text());
    
}

Here is response:

{
  finishReason: 'SAFETY',
  index: 0,
  safetyRatings: [
    {
      category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
      probability: 'NEGLIGIBLE'
    },
    {
      category: 'HARM_CATEGORY_HATE_SPEECH',
      probability: 'NEGLIGIBLE'
    },
    { category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },
    {
      category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
      probability: 'MEDIUM'
    }
  ]
}

What's wrong with this code?

I tried different safety setting, but every time I get an error, with the safety level lower than in the config.

like image 540
Egor Vasilev Avatar asked Aug 12 '26 06:08

Egor Vasilev


1 Answers

The issue seems to be with the format of your safe dictionary.

Here's how you should define it:

const safe = {
    "HARM_CATEGORY_HARASSMENT": "BLOCK_NONE",
    "HARM_CATEGORY_HATE_SPEECH": "BLOCK_NONE",
    "HARM_CATEGORY_SEXUALLY_EXPLICIT": "BLOCK_NONE",
    "HARM_CATEGORY_DANGEROUS_CONTENT": "BLOCK_NONE",
}

In your code, you're using a list of dictionaries, each containing a category and threshold key. However, the safe dictionary should be a single dictionary where the keys are the harm categories and the values are the thresholds.

In Python, this code works for me:

safety_settings={
    HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
    HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
    HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: 4,
    HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: 4,
}

Also, for people running into similar problems, note that Gemini only supports these 4 harm categories, and not others defined in the API such as HARM_CATEGORY_MEDICAL for example. This caused me an error too.

like image 92
Fernando Ó Avatar answered Aug 16 '26 06:08

Fernando Ó



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!