ilang-ai commited on
Commit ·
7c1cf27
1
Parent(s): c376974
fix: relax Gemini safety filters — BLOCK_NONE for all categories
Browse filesWithout this, Gemini blocks any message containing slang/adult terms
from the 黑话词典, returning empty response and triggering deflect.
Added better logging for blocked responses.
- modules/chat.py +20 -2
modules/chat.py
CHANGED
|
@@ -9,6 +9,14 @@ logger = logging.getLogger(__name__)
|
|
| 9 |
|
| 10 |
genai.configure(api_key=config.GEMINI_API_KEY)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Load prompts from .ilang files (prompts/ if exists, else prompts_demo/)
|
| 13 |
def _load_prompt(name):
|
| 14 |
for d in ("prompts", "prompts_demo"):
|
|
@@ -29,8 +37,15 @@ GROUP_WELCOME = (
|
|
| 29 |
"给我管理员权限(删消息+封人)就行, 其他不用管"
|
| 30 |
)
|
| 31 |
|
| 32 |
-
model = genai.GenerativeModel(
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
def _parse(raw):
|
|
@@ -91,6 +106,9 @@ async def ai_text(text, history=None, context_info=""):
|
|
| 91 |
r = await model.generate_content_async(prompt)
|
| 92 |
raw = r.text.strip() if r.text else ""
|
| 93 |
if not raw:
|
|
|
|
|
|
|
|
|
|
| 94 |
return ("chat", None, _deflect())
|
| 95 |
return _parse(raw)
|
| 96 |
except Exception as e:
|
|
|
|
| 9 |
|
| 10 |
genai.configure(api_key=config.GEMINI_API_KEY)
|
| 11 |
|
| 12 |
+
# Relax Gemini safety filters — bot needs to understand slang and adult humor
|
| 13 |
+
SAFETY_SETTINGS = [
|
| 14 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 15 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
| 16 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
| 17 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
# Load prompts from .ilang files (prompts/ if exists, else prompts_demo/)
|
| 21 |
def _load_prompt(name):
|
| 22 |
for d in ("prompts", "prompts_demo"):
|
|
|
|
| 37 |
"给我管理员权限(删消息+封人)就行, 其他不用管"
|
| 38 |
)
|
| 39 |
|
| 40 |
+
model = genai.GenerativeModel(
|
| 41 |
+
config.GEMINI_MODEL,
|
| 42 |
+
system_instruction=SYSTEM_PROMPT,
|
| 43 |
+
safety_settings=SAFETY_SETTINGS
|
| 44 |
+
)
|
| 45 |
+
vision_model = genai.GenerativeModel(
|
| 46 |
+
config.GEMINI_MODEL,
|
| 47 |
+
safety_settings=SAFETY_SETTINGS
|
| 48 |
+
)
|
| 49 |
|
| 50 |
|
| 51 |
def _parse(raw):
|
|
|
|
| 106 |
r = await model.generate_content_async(prompt)
|
| 107 |
raw = r.text.strip() if r.text else ""
|
| 108 |
if not raw:
|
| 109 |
+
# Log the block reason if available
|
| 110 |
+
if hasattr(r, 'prompt_feedback') and r.prompt_feedback:
|
| 111 |
+
logger.warning("AI blocked: " + str(r.prompt_feedback))
|
| 112 |
return ("chat", None, _deflect())
|
| 113 |
return _parse(raw)
|
| 114 |
except Exception as e:
|