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

Merged examples/function_calling_config.ipynb into examples/function_calling.ipynb notebook #631

Open
wants to merge 26 commits into
base: main
Choose a base branch
from

Conversation

andycandy
Copy link
Contributor

@andycandy andycandy commented Apr 1, 2025

Much stuff has changed, so I'm unsure what precisely to print in each cell.
So I took some stuff from ../quickstarts/Function_calling.ipynb route, as it's pretty well formatted, to print all the to and fro that went in the background for performing every user request

  • Updated SDK imports
  • Merged function_calling_config into function_calling notebook
  • lint checked
  • nbfmt

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @andycandy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request migrates the function calling configuration in the quickstarts/Function_calling_config.ipynb notebook. It updates the notebook to use the new google-genai library (version 1.0.0 or higher) instead of google-generativeai. The changes include updating the import statements, the way the client is configured, and how tool configurations are created and used. Additionally, a function print_history is added to analyze the chat history between the model and user functions, and the notebook cells are updated to reflect these changes.

Highlights

  • Library Update: The pull request updates the google-generativeai library to google-genai (version >=1.0.0).
  • Configuration Changes: The configuration of the API key and model initialization has been updated to use the new google-genai library.
  • Tool Configuration: The way tool configurations are created has been updated to use types.ToolConfig.
  • Chat History Analysis: A new function print_history is added to analyze the chat history between the model and user functions.
  • Function Calling: The way function calling is invoked has been updated to use the new send_message API.

Changelog

Click here to see the changelog
  • quickstarts/Function_calling_config.ipynb
    • Updated copyright year to 2025.
    • Updated the google-generativeai library to google-genai (version >=1.0.0).
    • Updated import statements to use from google import genai instead of import google.generativeai as genai.
    • Updated the client configuration to use client = genai.Client(api_key=userdata.get("GOOGLE_API_KEY")) instead of genai.configure.
    • Renamed model_name to MODEL_ID.
    • Updated the way chat sessions are created to use client.chats.create.
    • Updated the tool configuration to use types.ToolConfig instead of content_types.to_tool_config.
    • Added a new function print_history to analyze the chat history.
    • Updated the way messages are sent to use the config parameter in chat.send_message to pass system_instruction, tools, and tool_config.
    • Updated the tool_config mode to "NONE" instead of "none".
    • Removed chat.rewind() after sending a message.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


The functions now called,
In configs, standing tall,
A new library's grace,
In the notebook's space,
Genai's changes enthrall.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions bot added status:awaiting review PR awaiting review from a maintainer component:quickstarts Issues/PR referencing quickstarts folder labels Apr 1, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The code changes migrate the function calling configuration in the notebook. The changes include updating the google-generativeai package to google-genai, updating the model initialization, and modifying the tool configuration. Additional cells were added to define a function for analyzing chat history and demonstrate different function calling modes. Overall, the changes aim to improve the function calling capabilities and provide more control over the model's behavior.

Summary of Findings

  • Package Update: The google-generativeai package is updated to google-genai, which may introduce breaking changes or require updates in other parts of the codebase. Ensure compatibility and test thoroughly.
  • Model Initialization: The model initialization is updated to use client.chats.create instead of genai.GenerativeModel and model.start_chat. Verify that the new initialization method is correct and that all parameters are properly configured.
  • Tool Configuration: The tool configuration is updated to use types.ToolConfig instead of content_types.to_tool_config. Ensure that the new tool configuration method is correct and that all parameters are properly configured.
  • Function Calling Mode: The function calling mode is updated to use NONE instead of none. Ensure that the new function calling mode is correct and that all parameters are properly configured.

Merge Readiness

The pull request introduces significant changes to the function calling configuration. While the changes seem to improve the function calling capabilities and provide more control over the model's behavior, thorough testing is required to ensure compatibility and correctness. I am unable to directly approve this pull request, and recommend that the pull request not be merged until the above points are addressed and other reviewers have approved this code.

@andycandy andycandy marked this pull request as draft April 1, 2025 18:21
@andycandy andycandy marked this pull request as ready for review April 1, 2025 18:51
@andycandy andycandy changed the title Migrating fn calling config Migrating the function_calling_config notebook Apr 1, 2025
@Giom-V Giom-V dismissed gemini-code-assist[bot]’s stale review April 2, 2025 14:36

The bot is not behaving as expected

@Giom-V
Copy link
Collaborator

Giom-V commented Apr 2, 2025

@andycandy I'm sorry since you already worked on that notebook, but I think the best would be to move those explanations to the function calling notebook as I don't think it really makes sense to have 2 separate ones. I'll still add some comments in the current notebook so that you can do it right the first time :)

@Giom-V Giom-V self-assigned this Apr 2, 2025
@andycandy
Copy link
Contributor Author

andycandy commented Apr 2, 2025

@andycandy I'm sorry since you already worked on that notebook, but I think the best would be to move those explanations to the function calling notebook as I don't think it really makes sense to have 2 separate ones. I'll still add some comments in the current notebook so that you can do it right the first time :)

I have merged both these notebooks into one @Giom-V

@andycandy andycandy requested a review from Giom-V April 2, 2025 19:04
@andycandy andycandy changed the title Migrating the function_calling_config notebook Merged examples/function_calling_config.ipynb into examples/function_calling.ipynb notebook Apr 3, 2025
@Giom-V
Copy link
Collaborator

Giom-V commented Apr 3, 2025

Thanks a lot @andycandy. I was not expecting you to start refactoring the notebook that much (I was expecting a copy-pasting of the other notebook at the end of this one).

That wasn't an easy review to do (at work I'd have jumped into a call to explain live), so I'm not sure if my comments are really clear, but in the end it all comes down to the order in which we want to introduce and explain stuff.

I'd say we need to start simple, with a simple example just showing the final output, that can be used to introduce what FC is, why using chat, how to define the functions, etc... Then we should explain that with the SDK it has Automatic FC, then show manual FC if auto-FC is not available, then I guess the order is up to you between advanced use-cases (parallel/Compositional) and the modes. The whole goal is to take the reader by the hand and explain the concepts in order. In doubt you can just refer to the documentation does it as it was reworked last week.

@andycandy
Copy link
Contributor Author

@Giom-V, Thank you for giving such an in-depth review. I made notes for all the changes you wanted, restructured the whole notebook, and checked everything to ensure they are as you asked them to be. Please skim through them once.

@andycandy andycandy requested a review from Giom-V April 3, 2025 20:02
@@ -11,7 +11,7 @@
},
Copy link
Collaborator

@Giom-V Giom-V Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this paragraph belongs to a later section.


Reply via ReviewNB

@@ -11,7 +11,7 @@
},
Copy link
Collaborator

@Giom-V Giom-V Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rerun this one, or find a better prompt? It shouldn't be executing the same function that many times...


Reply via ReviewNB

Copy link
Contributor Author

@andycandy andycandy Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I tried multiple prompts but I think the main issue is arising from

"tool_config":types.ToolConfig(
            function_calling_config=types.FunctionCallingConfig(
                mode: "any",
                allowed_function_names=["set_light_color"]
            )
)

Maybe you didn't notice the same setting was applied in that, turning this place into a disco example. and similarly to this the model started executing functions multiple times. I am not really sure what's wrong but these any mode examples are messing with the outputs somehow, I'll try changing the prompts and model, if the results stay the same, I'll raise an issue in the repo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think I found the cause:
#674 (comment)

'any' mode is supposed to be run with a set number of reruns that cap its re-invocation. And in our case I didn't set any cap, that's why it kept repeating for 10 (default) times

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I capped the automatic function reinvocation to 1 and reran all the cells:

"tool_config" : {
            "function_calling_config": {
                "mode": "any"
            }
        },
        "automatic_function_calling": {
            "maximum_remote_calls" : 1
        }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's interesting, I didn't know about that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andycandy I think you should add a line explaining that behavior before the code then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I'll refer to the official documentation for this function call as well

@Giom-V
Copy link
Collaborator

Giom-V commented Apr 4, 2025

@andycandy I made a couple of comments but it's shaping well. Now the progression of the guide makes sense. We're getting there :)

@andycandy
Copy link
Contributor Author

@Giom-V Tried to implement all your suggestions, however, I'd request you to look into that 'any' mode function_calling_config parameters issue

@Giom-V
Copy link
Collaborator

Giom-V commented Apr 7, 2025

@Giom-V Tried to implement all your suggestions, however, I'd request you to look into that 'any' mode function_calling_config parameters issue

@andycandy I think we're good, except the little addition to explain what maximum_remote_calls does and why you need to set it when using ANY.

@Giom-V
Copy link
Collaborator

Giom-V commented Apr 7, 2025

Ignore the format and lint errors, they are as expected.

@andycandy
Copy link
Contributor Author

@Giom-V Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:quickstarts Issues/PR referencing quickstarts folder status:awaiting review PR awaiting review from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants