[Gemma 3]Function Calling With HF Document Summarizer
Copyright 2024 Google LLC.
Gemma - 3 Function Calling: Local File Reader
Google released Gemma-3, a new variant that comes with multimodality, multilingual support for up to 140 global languages, a 128K context window, and agentic capabilities like structured responses and function calling. In this notebook, we will explore how to use function calling with open-source models from Hugging Face via the Gemma 3 4B-it model to read and summarize local text files.
Setup
Select the Colab runtime
To complete this tutorial, you'll need to have a Colab runtime with sufficient resources to run the Gemma model. In this case, you can use a T4 GPU:
- In the upper-right of the Colab window, select ▾ (Additional connection options).
- Select Change runtime type.
- Under Hardware accelerator, select T4 GPU.
Gemma-3 setup
To complete this tutorial, you'll first need to complete the setup instructions at Gemma setup. The Gemma setup instructions show you how to do the following:
- Get access to Gemma on kaggle.com.
- Select a Colab runtime with sufficient resources to run the Gemma 3 4B-it model.
- Generate and configure a Kaggle username and an API key as Colab secrets.
After you've completed the Gemma setup, move on to the next section, where you'll set environment variables for your Colab environment.
Install dependencies
Run the cell below to install all the required dependencies.
- Transformers: Latest version that supports Gemma-3
- Accelerate
- No external APIs required; uses standard Python file I/O
Gemma 3
Google has released Gemma-3, a successor to its earlier Gemma models, focusing on open-source accessibility and multimodal vision capabilities. The model is designed to handle complex tasks with extended context lengths, support for multiple languages, and vision capabilities, making it suitable for a wide range of applications.
Gemma-3 has gained attention for outperforming notable models like DeepSeek V3, o3-mini, and Llama 3 405B, with extended context lengths, multilingual support, and multimodal capabilities.
- Extended context length from 32K to 128K using ROPE scaling (from 10K to 1M) on global self-attention layers.
- Available in 1B, 4B, 12B, and 27B sizes (Vision capability included, except for 1B, with bi-directional attention).
- Supports an extensive range of 140 global languages.
- Additional Features: Supports function calling, where the model can generate code or instructions to call external functions, and structured outputs, enabling it to produce data in specific formats like JSON or tables.
Load Model weights
Function Calling Gemma
Function calling is a feature that allows large language models to interact with external systems, APIs, and tools in a structured way. This feature enables LLMs to not just generate text responses but to invoke specific functions when appropriate based on user queries. In this notebook, we’ll use function calling to read and summarize a local text file uploaded to the Colab environment. For example, a user can ask the model to summarize a file named notes.txt.
Define prompt for function calling
Function Calling follows these steps:
- Your application sends a prompt to the LLM along with function definitions
- The LLM analyzes the prompt and decides whether to respond directly or use defined functions
- If using functions, the LLM generates structured arguments for the function call
- Your application receives the function call details and executes the actual function
- The function results are sent back to the LLM
- The LLM provides a final response incorporating the function results
Okay, I can help with that. I will read the file "notes.txt" and provide a summary of its contents. ```tool_code summary = read_file(filename='notes.txt') print(summary) ```
Execute the function calling code
Saving notes.txt to notes (2).txt
Saving notes.txt to notes (3).txt
'```tool_output\nAdapt AI :A personalized solution to sense your stress, fix your mess, and , boost productivity.\nMaintaining productivity while managing stress is a significant challenge in todays fast paced and multi tasking work environment.\n\nConventional tools are often static in nature, and offer generic solutions that fail to consider the nuances of individual differences in behavior, and stress triggers.\n\nTherefore in this Late breaking work we introduce Adapt AI: a groundbreaking, personalized AI solution designed to boost productivity and enhance well-being through tailored, personalized, interventions.\n\nAdapt AI employs a sophisticated multimodal sensing system, integrating egocentric vision, audio, and physiological data such as heart and motion activities of the individuals.\n\nThis allows Adapt AI to understand and respond to the unique context of each user with the help of Large language models.\n\nBy monitoring real-time data, Adapt AI detects stress levels and activity patterns, dynamically suggesting interventions like micro-breaks or physical activities precisely when needed.\n\nThe core of Adapt AI includes several critical components:\nThe Processing Module: This Captures and processes inputs from cameras, microphones, and physiological sensors and feeds the data to LLMs, to create a comprehensive understanding of the user’s current state.\n\nThen, the, External Task Agents, Automate routine tasks, allowing users to focus on more critical responsibilities.\n\nThe Personalized Well-being Intervention Pipeline Uses insights generated from the data to suggest timely actions that help maintain a healthy work rhythm and reduce stress.\n\nUsing all the above our system is able to sense your stress and fix your mess, but thats not all, we also boost productivity using your own personalized Tone adaptive agent.\n\nThe Tone-Adaptive Conversation Agent is a personalized LLM at your service that answers to your day to day queries while adjusting its communication style based on the user’s emotional state, offering support and suggestions through a user-friendly interface.\n\nFor instance, if the system detects increased stress during a complex project, it might suggest a brief relaxation exercise or a change in task focus to help alleviate stress, while also acting cheerful and motivational in the form of its conversational agent responses.\n\nIn practice, Adapt AI functions as a smart solution that not only manages tasks but also proactively supports the user’s mental and physical health through personalized assistance.\n\nIn a study with 15 participants, Our AdaptAI demonstrated significant improvements in task performance and user satisfaction, validating its effectiveness in creating a more supportive and efficient work environment.\n\nThis intuitive system not only addresses immediate needs but also adapts to changing conditions, ensuring optimal performance and well-being in any professional setting. Thank you!\n\n\n\n```'
user Results from the file read:```tool_output Adapt AI :A personalized solution to sense your stress, fix your mess, and , boost productivity. Maintaining productivity while managing stress is a significant challenge in todays fast paced and multi tasking work environment. Conventional tools are often static in nature, and offer generic solutions that fail to consider the nuances of individual differences in behavior, and stress triggers. Therefore in this Late breaking work we introduce Adapt AI: a groundbreaking, personalized AI solution designed to boost productivity and enhance well-being through tailored, personalized, interventions. Adapt AI employs a sophisticated multimodal sensing system, integrating egocentric vision, audio, and physiological data such as heart and motion activities of the individuals. This allows Adapt AI to understand and respond to the unique context of each user with the help of Large language models. By monitoring real-time data, Adapt AI detects stress levels and activity patterns, dynamically suggesting interventions like micro-breaks or physical activities precisely when needed. The core of Adapt AI includes several critical components: The Processing Module: This Captures and processes inputs from cameras, microphones, and physiological sensors and feeds the data to LLMs, to create a comprehensive understanding of the user’s current state. Then, the, External Task Agents, Automate routine tasks, allowing users to focus on more critical responsibilities. The Personalized Well-being Intervention Pipeline Uses insights generated from the data to suggest timely actions that help maintain a healthy work rhythm and reduce stress. Using all the above our system is able to sense your stress and fix your mess, but thats not all, we also boost productivity using your own personalized Tone adaptive agent. The Tone-Adaptive Conversation Agent is a personalized LLM at your service that answers to your day to day queries while adjusting its communication style based on the user’s emotional state, offering support and suggestions through a user-friendly interface. For instance, if the system detects increased stress during a complex project, it might suggest a brief relaxation exercise or a change in task focus to help alleviate stress, while also acting cheerful and motivational in the form of its conversational agent responses. In practice, Adapt AI functions as a smart solution that not only manages tasks but also proactively supports the user’s mental and physical health through personalized assistance. In a study with 15 participants, Our AdaptAI demonstrated significant improvements in task performance and user satisfaction, validating its effectiveness in creating a more supportive and efficient work environment. This intuitive system not only addresses immediate needs but also adapts to changing conditions, ensuring optimal performance and well-being in any professional setting. Thank you! ``` User_query: Summarize the contents of notes.txt model Okay, here's a summary of the provided text about Adapt AI: **Adapt AI is a personalized AI solution designed to boost productivity and enhance well-being in a fast-paced work environment.** It addresses the limitations of traditional tools by recognizing individual differences and stress triggers. **Here's how it works:** * **Sensing:** It uses a multimodal system – cameras, microphones, and physiological sensors (like heart rate and motion) – to understand the user’s context and emotional state. * **Processing:** The data is fed into Large Language Models (LLMs) to create a comprehensive understanding of the user’s current state. * **Task Automation:** External Task Agents automate routine tasks, freeing up the user’s time. * **Personalized Interventions:** A Well-being Intervention Pipeline suggests timely actions like micro-breaks or physical activities based on detected stress levels. * **Tone-Adaptive Conversation Agent:** A personalized LLM adjusts its communication style to the user’s emotional state, offering support and motivation. For example, it can suggest relaxation exercises during stressful times or provide cheerful encouragement. **Results:** A study with 15 participants showed significant improvements in task performance and user satisfaction, validating Adapt AI's effectiveness. **In essence, Adapt AI proactively monitors the user, adjusts its approach based on their needs, and provides support for both productivity and mental/physical well-being.** --- Do you want me to focus on a specific aspect of the summary, or perhaps extract particular information (like the components of the system)?
Run in Google Colab