-
Notifications
You must be signed in to change notification settings - Fork 458
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
Add Ability to Specify Gemini Safety Settings #790
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fantastic, @dAIsySHEng1! Great work.
In your PR description, you can add "fix #373", and when this PR is merged, it'll automatically close that issue. If you could add a bit more of a detailed description (just a few sentences), that would be great as well.
The test looks great. Given that we'll probably want to test this sort of pattern often, we should generalize this further (given the similar pattern in the above test). That lies outside of the scope of this PR, though.
Ah one other thing - the example you sent me on slack is hilarious, let's add this to the gemini model docs :) |
Will this also work for |
See <https://ai.google.dev/gemini-api/docs/safety-settings> for API docs. | ||
""" | ||
|
||
category: Literal[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it perhaps be better to make these StrEnum
? e.g.
class HarmCategory(StrEnum):
UNSPECIFIED = auto()
DEROGATORY = auto()
....
This actually already exists in the SDK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And perhaps even make GeminiSafetySettings
a dataclass
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Literal
is fine here - it's consistent with other patterns we're using in this file. If we were using the sdk, I definitely think we should pull from there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why make GeminiSafetySettings
a dataclass? Does it need any methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enums provide better developer experience with stronger type safety, IDE autocomplete, enumeration, avoiding typos etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And dataclasses often don't have any methods, that's not their purpose. It's for having a date structure similar to Pydantic models but without overhead of validation. Again provides better developer experience of attribute instead of key access, with IDE autocomplete, stronger type safety, defaults, etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer using enums & dataclasses over str literals and typeddicts for more explicit typing, But I realise that opinion is somewhat subjective
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The style guide flagged several spelling errors that seemed like false positives. We skipped posting inline suggestions for the following words:
- Chinoy
- Hussain
docs/agents.md
Outdated
@@ -226,6 +226,29 @@ print(result_sync.data) | |||
#> Rome | |||
``` | |||
|
|||
For Gemini models, safety settings for requests can be specified via [`GeminiModelSettings`][pydantic_ai.models.gemini.GeminiModelSettings]. For example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove my previous example, this one is better.
Just preface with:
If you wish to further customize model behavior, you can use a subclass of [`ModelSettings`][pydantic_ai.settings.ModelSettings], like [`GeminiModelSettings`][pydantic_ai.models.gemini.GeminiModelSettings], associated with your model of choice.
For example:
docs/agents.md
Outdated
], | ||
), | ||
) | ||
print(result.data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe wrap this in a try/except block and print the error response?
if 'content' not in response['candidates'][0]: | ||
if response['candidates'][0].get('finish_reason') == 'SAFETY': | ||
raise UnexpectedModelBehavior('Safety settings triggered', str(response)) | ||
else: | ||
raise UnexpectedModelBehavior('Content field missing from Gemini response', str(response)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to chat with the team tomorrow morning to see if this is the right way to go about this. I think we might just want to catch the validation issue earlier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok confirmed, we can move forward with this approach for now 👍
fix #373
Add ability to specify Gemini safety settings when doing an agent run. This addition allows you to specify safety categories and thresholds via the
model_settings
field. Valid settings are based on the Gemini API docs. This should hopefully add flexibility to deal with Gemini's safety guardrails as needed, aligning with what one is able to currently do in the vertexai package.Example Code:
To Do:
_GeminiSafetyRating
finish_reason
is'SAFETY'
Figure out way to deal with potential duplicates (deferred)