Google Gemini API error - Content generation stopped. Reason: SAFETY

100 Views Asked by At

I'm building an android app by using Google Gemini API.

I'm prompting what is 58+78 which is giving correct output. But when I'm prompting what is 2+2, then my app crashes and in Logcat it says : Content generation stopped. Reason: SAFETY.

Complete Logcat :

FATAL EXCEPTION: main
Process: com.example.gemniapi, PID: 22751
com.google.ai.client.generativeai.type.ResponseStoppedException: Content generation stopped. Reason: SAFETY
 at com.google.ai.client.generativeai.GenerativeModel.validate(GenerativeModel.kt:193)
 at com.google.ai.client.generativeai.GenerativeModel.generateContent(GenerativeModel.kt:86)
 at com.google.ai.client.generativeai.GenerativeModel$generateContent$1.invokeSuspend(Unknown Source:15)
 at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
 at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
 at kotlinx.coroutines.EventLoop.processUnconfinedEvent(EventLoop.common.kt:68)
 at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:135)
 at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:109)
 at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:11)
 at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(SuspendFunctionGun.kt:59)
 at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
 at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
 at android.os.Handler.handleCallback(Handler.java:983)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loopOnce(Looper.java:226)
 at android.os.Looper.loop(Looper.java:328)
 at android.app.ActivityThread.main(ActivityThread.java:9155)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:586)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
 Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: 
  [StandaloneCoroutine{Cancelling}@e794c8a, Dispatchers.Main.immediate]
    

I can't understand what's the SAFETY concern here.

1

There are 1 best solutions below

0
Viswanath Kumar Sandu On

While using Gemini API, we need to take care of SAFETY Settings. These settings are used to avoid dagenrous/inappropriate being delivered. You can add these like below

from google.generativeai.types import HarmCategory, HarmBlockThreshold

model = genai.GenerativeModel(model_name='gemini-pro-vision')
response = model.generate_content(
    ['Do these look store-bought or homemade?', img],
    safety_settings={
        HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
        HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
    }
)

Ref: To find all the safety settings, please check the documentation https://ai.google.dev/docs/safety_setting_gemini