Google Gemini API 错误 - 内容生成停止。理由:安全

问题描述 投票:0回答:1

我正在使用 Google Gemini API 构建一个 Android 应用程序。

我正在提示

what is 58+78
,它给出了正确的输出。但是当我提示
what is 2+2
时,我的应用程序崩溃了,并且在 Logcat 中显示:
Content generation stopped. Reason: SAFETY

完整的 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]
    

我不明白这里的安全问题是什么。

android logcat google-gemini
1个回答
0
投票

在使用Gemini API时,我们需要注意安全设置。这些设置用于避免损坏/不适当的传递。您可以像下面这样添加这些

从 google.generativeai.types 导入 HarmCategory、HarmBlockThreshold

model = genai.GenerativeModel(model_name='gemini-pro-vision') 响应 = model.generate_content( [“这些看起来是商店买的还是自制的?”,img], 安全设置={ HarmCategory.HARM_CATEGORY_HATE_SPEECH:HarmBlockThreshold.BLOCK_LOW_AND_ABOVE, HarmCategory.HARM_CATEGORY_HARASSMENT:HarmBlockThreshold.BLOCK_LOW_AND_ABOVE, } )

参考:要查找所有安全设置,请查看文档 https://ai.google.dev/docs/safety_setting_gemini

© www.soinside.com 2019 - 2024. All rights reserved.