autogen icon indicating copy to clipboard operation
autogen copied to clipboard

added Gemini safety setting and Gemini generation config

Open NikolayTV opened this issue 1 year ago • 5 comments

Why are these changes needed?

  • Added support for safety_setting and generation_config for Gemini client

Hi! I have also taken into account the comments from this PR #2410

Important notes:

1 - If no parameters are passed, then default values for safety and generation config of Gemini will be used, whatever it is under the hood of gemini itself. 2 - Safety settings will work only when api_type is google, if api_type is not passed it will throw an error. 3 - Added mapping for generation params names, so that Autogen users may not bother with different namings for different model types. For examples, I have seen "max_tokens" in Autogen, and Gemini uses "max_output_tokens" 4 - Safety settings works fine when passed as strings (opposed to class definitions). Also it is how it is show in official docs 5 - I did not include parameter "n" (candidate count) because gemini breaks for n > 1 right now

Tests: Additionally to the tests that check that safety setting and generation params work, I tested few scripts that use openai-models, and I believe it will not brake anything.

Example usage:

safety_settings = [
    {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_ONLY_HIGH"},
    {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_ONLY_HIGH"},
    {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_ONLY_HIGH"},
    {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_ONLY_HIGH"},
]

llm_config_gemini = {
  "config_list": [{"model": "gemini-pro", "api_key": os.environ["GEMINI_API_KEY"], "api_type": "google",
    "top_p":0.1, "temperature":1, "top_k":1, "max_tokens":2048,
    "safety_settings":safety_settings}],
  }

TODO

  • Should I update documentation? which one?

Related issue number

Closes #2428, #2409

Checks

  • [ ] I've included any doc changes needed for https://microsoft.github.io/autogen/. See https://microsoft.github.io/autogen/docs/Contribute#documentation to build and test documentation locally.
  • [ ] I've added tests (if relevant) corresponding to the changes introduced in this PR.
  • [ ] I've made sure all auto checks have passed.

NikolayTV avatar Apr 18 '24 08:04 NikolayTV

@microsoft-github-policy-service agree

NikolayTV avatar Apr 18 '24 08:04 NikolayTV

@NikolayTV Very nice PR! Thanks for your contribution~! Let's fix the formatting issues. An easy way to do it is run the following command in the terminal

git add .
pre-commit run --all-files
git add .

BeibinLi avatar Apr 18 '24 17:04 BeibinLi

@NikolayTV Very nice PR! Thanks for your contribution~! Let's fix the formatting issues. An easy way to do it is run the following command in the terminal

git add .
pre-commit run --all-files
git add .

@BeibinLi It froze on mypy step, had to interrupt. But I think it did the job of formatting image

NikolayTV avatar Apr 19 '24 08:04 NikolayTV

@BeibinLi Hi! What do we do next to add to main branch?

NikolayTV avatar Apr 23 '24 14:04 NikolayTV

⚠️ GitGuardian has uncovered 5 secrets following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secrets in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
10404693 Triggered Generic High Entropy Secret 672c54521949c9517a4eb4996ed845442e947d67 test/oai/test_utils.py View secret
10404662 Triggered Generic CLI Secret 85d3d68bad509683af8d05f1ed330763fe76153a .github/workflows/dotnet-release.yml View secret
10404694 Triggered Generic High Entropy Secret 672c54521949c9517a4eb4996ed845442e947d67 test/oai/test_utils.py View secret
10404696 Triggered Generic High Entropy Secret 672c54521949c9517a4eb4996ed845442e947d67 test/oai/test_utils.py View secret
10422482 Triggered Generic High Entropy Secret 672c54521949c9517a4eb4996ed845442e947d67 test/oai/test_utils.py View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secrets safely. Learn here the best practices.
  3. Revoke and rotate these secrets.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

gitguardian[bot] avatar Apr 23 '24 21:04 gitguardian[bot]

It is quite impossible to use Autogen with Gemini default security settings (medium) on. Group chat collapses to absolutely benign security violations.

I managed to work it around following the modifications from this branch. Would be good to have in main.

Codecov Report

Attention: Patch coverage is 0% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 19.49%. Comparing base (11d9336) to head (85d3d68). Report is 6 commits behind head on main.

Files Patch % Lines
autogen/oai/gemini.py 0.00% 5 Missing :warning:
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2429       +/-   ##
===========================================
- Coverage   33.60%   19.49%   -14.11%     
===========================================
  Files          87       87               
  Lines        9336     9336               
  Branches     1987     1988        +1     
===========================================
- Hits         3137     1820     -1317     
- Misses       5933     7396     +1463     
+ Partials      266      120      -146     
Flag Coverage Δ
unittests 19.48% <0.00%> (-14.12%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov-commenter avatar May 21 '24 23:05 codecov-commenter