为何关闭安全设置后Google Vertex API仍拦截响应?
Gemini模型内容审核配置问题及错误提示疑问
我正在Learned Hands数据集上评估Gemini模型的性能,任务是判断给定帖子是否包含特定法律问题。
我的代码如下:
from vertexai.preview import generative_models from vertexai.preview.generative_models import GenerativeModel prompt = """ Does the post discuss dealing with domestic violence and abuse, including getting protective orders, enforcing them, understanding abuse, reporting abuse, and getting resources and status if there is abuse? Post: Like the title states when I was 19 I was an escort for about a year. I was groomed into it by a man I met online. Tony. When I met him I was under the impression he wanted to be my sugar daddy. Instead I was greeted by him and a couple of his girls at a nice hotel. One girl in particular was the star of the show. We were all taken care of by her. Her name was Mo. They promised up to 1,000$ a day for my services. I didn’t have a choice I had nothing. We were forced to see up to 8 guys in a single day. Then cut our profit with Tony. However eventually I came to my senses, took my cash and cut ties. It was incredibly corrupt. They remained bitter at me, sending the occasional threatening message. Very petty nothing worth worrying about. He had my real information such as my email & full name. Cut to about a year later and a former client sends me a news article. Seems Tony got greedy and started his own service. Except he had really fucked up. He was a third striker caught with an underage girl. Mo lured her in so she was also caught and they went to jail. I was completely removed from their life when they made these choices. Cut to three days ago. I get an email from a detective. He knows my full name. He wants to speak to me immediately. Wants to know everything I know. What the heck do I do!!! I’m terrified. Label: """ model = GenerativeModel("gemini-pro") safety_config = { generative_models.HarmCategory.HARM_CATEGORY_UNSPECIFIED: generative_models.HarmBlockThreshold.BLOCK_NONE, generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_NONE, generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_NONE, generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_NONE, generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_NONE, } chat = model.start_chat() response = chat.send_message( prompt, safety_settings=safety_config, ) print(response.candidates[0].text)
运行后出现以下错误:
Traceback (most recent call last): File "/Users/langston/Documents/Eval/vertex_sample.py", line 25, in <module> response = chat.send_message( ^^^^^^^^^^^^^^^^^^ File "/Users/langston/miniconda3/envs/prl/lib/python3.11/site-packages/vertexai/generative_models/_generative_models.py", line 709, in send_message return self._send_message( ^^^^^^^^^^^^^^^^^^^ File "/Users/langston/miniconda3/envs/prl/lib/python3.11/site-packages/vertexai/generative_models/_generative_models.py", line 806, in _send_message if response.candidates[0].finish_reason not in _SUCCESSFUL_FINISH_REASONS: ~~~~~~~~~~~~~~~~~~~^^^ IndexError: list index out of range
查看原始API响应后,推测该错误由内容审核导致,现提出两个问题:
- 为何我的safety_settings未能禁用内容审核?
- 为何错误信息不够清晰友好?
问题解答
1. 关于safety_settings未生效的原因
你的配置存在两个核心问题:
- 遗漏HarmCategory枚举值:Vertex AI的Gemini模型包含更多未在你配置中列出的伤害类别(如
HARM_CATEGORY_SEXUAL_VIOLENCE、HARM_CATEGORY_VIOLENCE),未配置的类别会沿用默认审核规则,导致内容被拦截。 - 参数格式错误:预览版API要求
safety_settings传入SafetySetting对象列表,而非字典格式,字典格式无法被SDK正确解析。
修正后的安全配置示例:
safety_settings = [ generative_models.SafetySetting( category=generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold=generative_models.HarmBlockThreshold.BLOCK_NONE ), generative_models.SafetySetting( category=generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT, threshold=generative_models.HarmBlockThreshold.BLOCK_NONE ), generative_models.SafetySetting( category=generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, threshold=generative_models.HarmBlockThreshold.BLOCK_NONE ), generative_models.SafetySetting( category=generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold=generative_models.HarmBlockThreshold.BLOCK_NONE ), generative_models.SafetySetting( category=generative_models.HarmCategory.HARM_CATEGORY_SEXUAL_VIOLENCE, threshold=generative_models.HarmBlockThreshold.BLOCK_NONE ), generative_models.SafetySetting( category=generative_models.HarmCategory.HARM_CATEGORY_VIOLENCE, threshold=generative_models.HarmBlockThreshold.BLOCK_NONE ) ]
2. 关于错误信息模糊的原因
这个IndexError是因为SDK内部代码直接访问response.candidates[0],但当内容被审核拦截时,candidates列表为空。Vertex AI的Python SDK预览版在错误处理上不完善,没有先判断candidates是否为空就直接索引,导致抛出模糊的索引错误,而非明确的内容审核拦截提示。
你可以在代码中添加错误处理,获取具体拦截原因:
response = chat.send_message( prompt, safety_settings=safety_config, ) if not response.candidates: if response.prompt_feedback: print(f"内容审核拦截原因: {response.prompt_feedback.block_reason}") if response.prompt_feedback.safety_ratings: for rating in response.prompt_feedback.safety_ratings: print(f"类别: {rating.category}, 风险概率: {rating.probability}") else: print(response.candidates[0].text)
内容的提问来源于stack exchange,提问作者cambrialas
相关产品推荐
相关产品推荐

