Skip to content

Commit

Permalink
fix: update gemini's safety_settings (#1057)
Browse files Browse the repository at this point in the history
Support gemini's new safety settings
  • Loading branch information
alxpez authored Oct 12, 2024
1 parent 8ee9c5d commit 8b8f48a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion instructor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,20 @@ def update_gemini_kwargs(kwargs: dict[str, Any]) -> dict[str, Any]:
# minimize gemini safety related errors - model is highly prone to false alarms
from google.generativeai.types import HarmCategory, HarmBlockThreshold # type: ignore

kwargs["safety_settings"] = kwargs.get("safety_settings", {}) | {
kwargs["safety_settings"] = kwargs.get("safety_settings", {})

fallback_safety_settings = {
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_ONLY_HIGH,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_ONLY_HIGH,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_ONLY_HIGH,
}

# Update or add fallback settings, respecting stricter existing ones
for category, fallback_threshold in fallback_safety_settings.items():
current_threshold = kwargs["safety_settings"].get(category)
if current_threshold is None or current_threshold < fallback_threshold:
kwargs["safety_settings"][category] = fallback_threshold

return kwargs


Expand Down

0 comments on commit 8b8f48a

Please sign in to comment.