ilang-ai commited on
Commit
4abec07
·
1 Parent(s): efbc2bc

fix: pass BLOCK_NONE safety_settings on every API call, not just constructor

Browse files

Constructor-level safety_settings may not propagate to all calls in
google-generativeai 0.8.4. Now explicitly passed on all 9 generate_content_async calls.

Files changed (1) hide show
  1. modules/chat.py +7 -7
modules/chat.py CHANGED
@@ -128,7 +128,7 @@ async def ai_text(text, history=None, context_info=""):
128
  try:
129
  c = _ctx(history, context_info)
130
  prompt = c + "\nuser: " + text if c else "user: " + text
131
- r = await model.generate_content_async(prompt)
132
  raw = _safe_text(r)
133
  if not raw:
134
  feedback = ""
@@ -155,7 +155,7 @@ async def ai_vision(image_bytes, caption="", history=None, context_info=""):
155
  prompt = VISION_PROMPT + "\n" + c
156
  if caption:
157
  prompt += "\nuser: " + caption
158
- r = await vision_model.generate_content_async([prompt, {"mime_type": "image/jpeg", "data": image_bytes}])
159
  return _parse(_safe_text(r))
160
  except Exception as e:
161
  logger.warning("AI vision: " + str(e))
@@ -166,7 +166,7 @@ async def ai_voice(audio_bytes, mime_type="audio/ogg", history=None, context_inf
166
  try:
167
  c = _ctx(history, context_info)
168
  prompt = SYSTEM_PROMPT + "\n" + c + "\nUser sent a voice message:"
169
- r = await vision_model.generate_content_async([prompt, {"mime_type": mime_type, "data": audio_bytes}])
170
  return _parse(_safe_text(r))
171
  except Exception as e:
172
  logger.warning("AI voice: " + str(e))
@@ -176,7 +176,7 @@ async def ai_voice(audio_bytes, mime_type="audio/ogg", history=None, context_inf
176
  async def ai_judge_group_message(text):
177
  try:
178
  prompt = ANTISPAM_TEXT_PROMPT + "\n\nMessage content: " + text[:1000]
179
- r = await vision_model.generate_content_async(prompt)
180
  result = (_safe_text(r) or "ok").lower()
181
  return "spam" in result
182
  except Exception:
@@ -188,7 +188,7 @@ async def ai_judge_group_image(image_bytes, caption=""):
188
  prompt = ANTISPAM_TEXT_PROMPT + "\n\nJudge this image. Reply ONLY: spam or ok."
189
  if caption:
190
  prompt += "\nCaption: " + caption[:500]
191
- r = await vision_model.generate_content_async([prompt, {"mime_type": "image/jpeg", "data": image_bytes}])
192
  result = (_safe_text(r) or "ok").lower()
193
  return "spam" in result
194
  except Exception:
@@ -203,7 +203,7 @@ async def ai_group_vision(image_bytes, caption="", history=None):
203
  prompt += "\nuser: " + caption
204
  else:
205
  prompt += "\nuser: [shared an image]"
206
- r = await vision_model.generate_content_async([prompt, {"mime_type": "image/jpeg", "data": image_bytes}])
207
  raw = _safe_text(r)
208
  if not raw:
209
  return _deflect()
@@ -219,7 +219,7 @@ async def ai_group_reply(text, history=None):
219
  try:
220
  ctx = _ctx(history, "GROUP_CHAT: You were @mentioned in a group. Reply directly, 1-2 sentences.")
221
  prompt = ctx + "\nuser: " + text
222
- r = await model.generate_content_async(prompt)
223
  raw = _safe_text(r)
224
  if not raw:
225
  return _deflect()
 
128
  try:
129
  c = _ctx(history, context_info)
130
  prompt = c + "\nuser: " + text if c else "user: " + text
131
+ r = await model.generate_content_async(prompt, safety_settings=SAFETY_SETTINGS)
132
  raw = _safe_text(r)
133
  if not raw:
134
  feedback = ""
 
155
  prompt = VISION_PROMPT + "\n" + c
156
  if caption:
157
  prompt += "\nuser: " + caption
158
+ r = await vision_model.generate_content_async([prompt, {"mime_type": "image/jpeg", "data": image_bytes}], safety_settings=SAFETY_SETTINGS)
159
  return _parse(_safe_text(r))
160
  except Exception as e:
161
  logger.warning("AI vision: " + str(e))
 
166
  try:
167
  c = _ctx(history, context_info)
168
  prompt = SYSTEM_PROMPT + "\n" + c + "\nUser sent a voice message:"
169
+ r = await vision_model.generate_content_async([prompt, {"mime_type": mime_type, "data": audio_bytes}], safety_settings=SAFETY_SETTINGS)
170
  return _parse(_safe_text(r))
171
  except Exception as e:
172
  logger.warning("AI voice: " + str(e))
 
176
  async def ai_judge_group_message(text):
177
  try:
178
  prompt = ANTISPAM_TEXT_PROMPT + "\n\nMessage content: " + text[:1000]
179
+ r = await vision_model.generate_content_async(prompt, safety_settings=SAFETY_SETTINGS)
180
  result = (_safe_text(r) or "ok").lower()
181
  return "spam" in result
182
  except Exception:
 
188
  prompt = ANTISPAM_TEXT_PROMPT + "\n\nJudge this image. Reply ONLY: spam or ok."
189
  if caption:
190
  prompt += "\nCaption: " + caption[:500]
191
+ r = await vision_model.generate_content_async([prompt, {"mime_type": "image/jpeg", "data": image_bytes}], safety_settings=SAFETY_SETTINGS)
192
  result = (_safe_text(r) or "ok").lower()
193
  return "spam" in result
194
  except Exception:
 
203
  prompt += "\nuser: " + caption
204
  else:
205
  prompt += "\nuser: [shared an image]"
206
+ r = await vision_model.generate_content_async([prompt, {"mime_type": "image/jpeg", "data": image_bytes}], safety_settings=SAFETY_SETTINGS)
207
  raw = _safe_text(r)
208
  if not raw:
209
  return _deflect()
 
219
  try:
220
  ctx = _ctx(history, "GROUP_CHAT: You were @mentioned in a group. Reply directly, 1-2 sentences.")
221
  prompt = ctx + "\nuser: " + text
222
+ r = await model.generate_content_async(prompt, safety_settings=SAFETY_SETTINGS)
223
  raw = _safe_text(r)
224
  if not raw:
225
  return _deflect()