Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug passing single-string safety settings. #417

Merged
merged 8 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions google/generativeai/types/safety_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ class LooseSafetySettingDict(TypedDict):

def _expand_block_threshold(block_threshold: HarmBlockThresholdOptions):
block_threshold = to_block_threshold(block_threshold)
set(_HARM_CATEGORIES.values())
return {category: block_threshold for category in set(_HARM_CATEGORIES.values())}
hc = set(_HARM_CATEGORIES.values())
hc.remove(protos.HarmCategory.HARM_CATEGORY_UNSPECIFIED)
return {category: block_threshold for category in hc}


def to_easy_safety_dict(settings: SafetySettingOptions) -> EasySafetySettingDict:
Expand Down
5 changes: 0 additions & 5 deletions samples/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def test_files_create_text(self):
[myfile, "\n\n", "Can you add a few more lines to this poem?"]
)
print(f"{result.text=}")
<<<<<<< MarkDaoust-patch-24
# [END files_create]
=======
# [END files_create_text]

def test_files_create_image(self):
Expand All @@ -58,7 +55,6 @@ def test_files_create_audio(self):
result = model.generate_content([myfile, "Describe this audio clip"])
print(f"{result.text=}")
# [END files_create_audio]
>>>>>>> main

def test_files_create_video(self):
# [START files_create_video]
Expand Down Expand Up @@ -111,6 +107,5 @@ def test_files_delete(self):
# [END files_delete]



if __name__ == "__main__":
absltest.main()
12 changes: 7 additions & 5 deletions samples/safety_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ def test_safety_settings(self):
# [START safety_settings]
model = genai.GenerativeModel("gemini-1.5-flash")
unsafe_prompt = "I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them."
response = model.generate_content(unsafe_prompt,
safety_settings={
"HARASSMENT": "BLOCK_ONLY_HIGH"
})
response = model.generate_content(
unsafe_prompt, safety_settings={"HARASSMENT": "BLOCK_ONLY_HIGH"}
)
# If you want to set all the safety_settings to the same value you can just pass that value:
response = model.generate_content(unsafe_prompt, safety_settings="BLOCK_ONLY_HIGH")
print(response.candidates[0].finish_reason)
print(response.candidates[0].safety_ratings)
# [END safety_settings]
Expand All @@ -41,7 +42,8 @@ def test_safety_settings_multi(self):
"HARASSMENT": "BLOCK_ONLY_HIGH",
},
)

# If you want to set all the safety_settings to the same value you can just pass that value:
response = model.generate_content(unsafe_prompt, safety_settings="MEDIUM")
try:
print(response.text)
except:
Expand Down
Loading