858 lines
38 KiB
Plaintext
858 lines
38 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "ur8xi4C7S06n"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Copyright 2024 Google LLC\n",
|
|
"#\n",
|
|
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
|
|
"# you may not use this file except in compliance with the License.\n",
|
|
"# You may obtain a copy of the License at\n",
|
|
"#\n",
|
|
"# https://www.apache.org/licenses/LICENSE-2.0\n",
|
|
"#\n",
|
|
"# Unless required by applicable law or agreed to in writing, software\n",
|
|
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
|
|
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
|
|
"# See the License for the specific language governing permissions and\n",
|
|
"# limitations under the License."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "JAPoU8Sm5E6e"
|
|
},
|
|
"source": [
|
|
"# Forced Function Calling with Tool Configurations in Gemini\n",
|
|
"\n",
|
|
"<table align=\"left\">\n",
|
|
" <td style=\"text-align: center\">\n",
|
|
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/generative-ai/blob/main/gemini/function-calling/forced_function_calling.ipynb\">\n",
|
|
" <img width=\"32px\" src=\"https://www.gstatic.com/pantheon/images/bigquery/welcome_page/colab-logo.svg\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td style=\"text-align: center\">\n",
|
|
" <a href=\"https://console.cloud.google.com/agent-platform/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Ffunction-calling%2Fforced_function_calling.ipynb\">\n",
|
|
" <img width=\"32px\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td style=\"text-align: center\">\n",
|
|
" <a href=\"https://console.cloud.google.com/agent-platform/workbench/instances?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/generative-ai/main/gemini/function-calling/forced_function_calling.ipynb\">\n",
|
|
" <img width=\"32px\" src=\"https://storage.googleapis.com/github-repo/workbench-icon.svg\" alt=\"Workbench logo\"><br> Open in Workbench\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td style=\"text-align: center\">\n",
|
|
" <a href=\"https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/function-calling/forced_function_calling.ipynb\">\n",
|
|
" <img width=\"32px\" src=\"https://raw.githubusercontent.com/primer/octicons/refs/heads/main/icons/mark-github-24.svg\" alt=\"GitHub logo\"><br> View on GitHub\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
"</table>\n",
|
|
"\n",
|
|
"<div style=\"clear: both;\"></div>\n",
|
|
"\n",
|
|
"<p>\n",
|
|
"<b>Share to:</b>\n",
|
|
"\n",
|
|
"<a href=\"https://www.linkedin.com/sharing/share-offsite/?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/function-calling/forced_function_calling.ipynb\" target=\"_blank\">\n",
|
|
" <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/8/81/LinkedIn_icon.svg\" alt=\"LinkedIn logo\">\n",
|
|
"</a>\n",
|
|
"\n",
|
|
"<a href=\"https://bsky.app/intent/compose?text=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/function-calling/forced_function_calling.ipynb\" target=\"_blank\">\n",
|
|
" <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/7/7a/Bluesky_Logo.svg\" alt=\"Bluesky logo\">\n",
|
|
"</a>\n",
|
|
"\n",
|
|
"<a href=\"https://twitter.com/intent/tweet?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/function-calling/forced_function_calling.ipynb\" target=\"_blank\">\n",
|
|
" <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/5a/X_icon_2.svg\" alt=\"X logo\">\n",
|
|
"</a>\n",
|
|
"\n",
|
|
"<a href=\"https://reddit.com/submit?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/function-calling/forced_function_calling.ipynb\" target=\"_blank\">\n",
|
|
" <img width=\"20px\" src=\"https://redditinc.com/hubfs/Reddit%20Inc/Brand/Reddit_Logo.png\" alt=\"Reddit logo\">\n",
|
|
"</a>\n",
|
|
"\n",
|
|
"<a href=\"https://www.facebook.com/sharer/sharer.php?u=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/function-calling/forced_function_calling.ipynb\" target=\"_blank\">\n",
|
|
" <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/51/Facebook_f_logo_%282019%29.svg\" alt=\"Facebook logo\">\n",
|
|
"</a>\n",
|
|
"</p>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "84f0f73a0f76"
|
|
},
|
|
"source": [
|
|
"| Author(s) |\n",
|
|
"| --- |\n",
|
|
"| [Kristopher Overholt](https://github.com/koverholt) |"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "tvgnzT1CKxrO"
|
|
},
|
|
"source": [
|
|
"## Overview\n",
|
|
"\n",
|
|
"This notebook demonstrates the use of forced Function Calling in the Gemini model.\n",
|
|
"\n",
|
|
"### Gemini\n",
|
|
"\n",
|
|
"Gemini is a family of generative AI models developed by Google DeepMind that is designed for multimodal use cases.\n",
|
|
"\n",
|
|
"### Function Calling in Gemini\n",
|
|
"\n",
|
|
"[Function Calling in Gemini](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling) lets developers create a description of a function in their code, then pass that description to a language model in a request. The response from the model includes the name of a function that matches the description and the arguments to call it with.\n",
|
|
"\n",
|
|
"### Forced Function Calling\n",
|
|
"\n",
|
|
"[Forced Function Calling in Gemini](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling#tool-config) allows you to place constraints on how the model should use the function declarations that you provide it with. Using tool configurations, you can force the Gemini model to only predict function calls. You can also choose to provide the model with a full set of function declarations, but restrict its responses to a subset of these functions.\n",
|
|
"\n",
|
|
"## Objectives\n",
|
|
"\n",
|
|
"In this tutorial, you will learn how to use the Vertex AI SDK for Python to use different function calling modes, including forced function calling, via the Gemini model.\n",
|
|
"\n",
|
|
"You will complete the following tasks:\n",
|
|
"\n",
|
|
"- Read through an overview of forced function calling and when to use it\n",
|
|
"- Use the default function calling behavior in `AUTO` mode\n",
|
|
"- Enable forced function calling using the `ANY` mode\n",
|
|
"- Disable function calling using the `NONE` mode"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "61RBz8LLbxCR"
|
|
},
|
|
"source": [
|
|
"## Getting Started"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "No17Cw5hgx12"
|
|
},
|
|
"source": [
|
|
"### Install Google Gen AI SDK"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"id": "tFy3H3aPgx12"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"%pip install --upgrade --quiet google-genai arxiv"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "dmWOrTJ3gx13"
|
|
},
|
|
"source": [
|
|
"### Authenticate your notebook environment (Colab only)\n",
|
|
"\n",
|
|
"If you are running this notebook on Google Colab, run the cell below to authenticate your environment."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {
|
|
"id": "NyKGtVQjgx13"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import sys\n",
|
|
"\n",
|
|
"if \"google.colab\" in sys.modules:\n",
|
|
" from google.colab import auth\n",
|
|
"\n",
|
|
" auth.authenticate_user()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "DF4l8DTdWgPY"
|
|
},
|
|
"source": [
|
|
"### Set Google Cloud project information and create client\n",
|
|
"\n",
|
|
"To get started using Vertex AI, you must have an existing Google Cloud project and [enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
|
|
"\n",
|
|
"Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"id": "Nqwi-5ufWp_B"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"\n",
|
|
"# fmt: off\n",
|
|
"PROJECT_ID = \"[your-project-id]\" # @param {type: \"string\", placeholder: \"[your-project-id]\", isTemplate: true}\n",
|
|
"# fmt: on\n",
|
|
"if not PROJECT_ID or PROJECT_ID == \"[your-project-id]\":\n",
|
|
" PROJECT_ID = str(os.environ.get(\"GOOGLE_CLOUD_PROJECT\"))\n",
|
|
"\n",
|
|
"LOCATION = \"global\"\n",
|
|
"\n",
|
|
"from google import genai\n",
|
|
"\n",
|
|
"client = genai.Client(enterprise=True, project=PROJECT_ID, location=LOCATION)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "23a24049e443"
|
|
},
|
|
"source": [
|
|
"## Import libraries"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {
|
|
"id": "cc6278ff6e55"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import arxiv\n",
|
|
"from IPython.display import Markdown, display\n",
|
|
"from google.genai.types import (\n",
|
|
" Content,\n",
|
|
" FunctionCallingConfig,\n",
|
|
" FunctionCallingConfigMode,\n",
|
|
" FunctionDeclaration,\n",
|
|
" GenerateContentConfig,\n",
|
|
" Part,\n",
|
|
" Schema,\n",
|
|
" Tool,\n",
|
|
" ToolConfig,\n",
|
|
" Type,\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "d091edc81048"
|
|
},
|
|
"source": [
|
|
"### Choose a model\n",
|
|
"\n",
|
|
"For more information about all AI models and APIs on Vertex AI, see [Google Models](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models#gemini-models) and [Model Garden](https://cloud.google.com/vertex-ai/generative-ai/docs/model-garden/explore-models).\n",
|
|
"\n",
|
|
"Refer to the [Gemini Function Calling documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling) for more information on which models and model versions support forced function calling and tool configurations."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {
|
|
"id": "c8922eacdb2d"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"MODEL_ID = \"gemini-3.5-flash\" # @param {type: \"string\"}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "0505b45fa754"
|
|
},
|
|
"source": [
|
|
"## Define a function to search for scientific papers in arXiv\n",
|
|
"\n",
|
|
"Since this notebook focuses on using different tool configurations and modes in Gemini Function Calling, you'll define a function declaration to use throughout the examples.\n",
|
|
"\n",
|
|
"The purpose of this function is to extract a parameter to send as a query to search for relevant papers in [arXiv](https://arxiv.org/).\n",
|
|
"\n",
|
|
"arXiv is an open-access repository of electronic preprints and postprints that consists of scientific papers in various fields."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {
|
|
"id": "72440503b923"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"search_arxiv = FunctionDeclaration(\n",
|
|
" name=\"search_arxiv\",\n",
|
|
" description=\"Search for articles and publications in arXiv\",\n",
|
|
" parameters=Schema(\n",
|
|
" type=Type.OBJECT,\n",
|
|
" properties={\n",
|
|
" \"query\": Schema(\n",
|
|
" type=Type.STRING, description=\"Query to search for in arXiv\"\n",
|
|
" )\n",
|
|
" },\n",
|
|
" ),\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "17f31eb30c46"
|
|
},
|
|
"source": [
|
|
"Define a tool that wraps the above function:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {
|
|
"id": "e4d7150e5b03"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"search_tool = Tool(\n",
|
|
" function_declarations=[\n",
|
|
" search_arxiv,\n",
|
|
" ],\n",
|
|
")\n",
|
|
"config = GenerateContentConfig(temperature=0, tools=[search_tool])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "6a12e1e637c7"
|
|
},
|
|
"source": [
|
|
"You'll use this function declaration and tool throughout the next few sections of the notebook."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "3a043e118735"
|
|
},
|
|
"source": [
|
|
"## Overview of Forced Function Calling in Gemini"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "0d17a45fcea5"
|
|
},
|
|
"source": [
|
|
"The default behavior for Function Calling allows the Gemini model to decide whether to predict a function call or a natural language response. This is because the default Function Calling mode in Gemini is set to `AUTO`.\n",
|
|
"\n",
|
|
"In most cases this is the desired behavior when you want the Gemini model to use information from the prompt to determine if it should call a function, and which function it should call. However, you might have specific use cases where you want to **force** the Gemini model to call a function (or a set of functions) in a given model generation request.\n",
|
|
"\n",
|
|
"Tool configurations in the Gemini API allow you to specify different Function Calling modes in Gemini. Refer to the [Gemini Function Calling documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling) for more information on forced function calling and tool configurations.\n",
|
|
"\n",
|
|
"The following code example for `tool_config` shows various modes that you can set and pass to the Gemini model either globally when you initialize the model or for a given model generation request:\n",
|
|
"\n",
|
|
"```py\n",
|
|
"tool_config = ToolConfig(\n",
|
|
" function_calling_config=ToolConfig.FunctionCallingConfig(\n",
|
|
" mode=ToolConfig.FunctionCallingConfig.Mode.AUTO, # The default model behavior. The model decides whether to predict a function call or a natural language response.\n",
|
|
" mode=ToolConfig.FunctionCallingConfig.Mode.ANY, # ANY mode forces the model to predict a function call from a subset of function names.\n",
|
|
" mode=ToolConfig.FunctionCallingConfig.Mode.NONE, # NONE mode instructs the model to not predict function calls. Equivalent to a model request without any function declarations.\n",
|
|
" allowed_function_names=[\n",
|
|
" \"function_to_call\"\n",
|
|
" ], # Allowed functions to call when mode is ANY, if empty any one of the provided functions will be called.\n",
|
|
" )\n",
|
|
")\n",
|
|
"```\n",
|
|
"\n",
|
|
"Using these Function Calling modes, you can configure the model to behave in one of the following ways:\n",
|
|
"\n",
|
|
"- Allow the model to choose whether to predict a function call or natural language response (`AUTO` mode)\n",
|
|
"- Force the model to predict a function call on one function or a set of functions (`ANY` mode)\n",
|
|
"- Disable function calling and return a natural language response as if no functions or tools were defined (`NONE` mode)\n",
|
|
"\n",
|
|
"In the following sections, you'll walk through examples and sample code for each Function Calling mode."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "5ab06d57a134"
|
|
},
|
|
"source": [
|
|
"## Example: Default Function Calling mode (`AUTO`)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "3a3b8124f0fc"
|
|
},
|
|
"source": [
|
|
"In this example, you'll specify the function calling mode as `AUTO`. Note that `AUTO` mode is the default model behavior, therefore the Gemini model will also use this mode when there is no `tool_config` specified:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {
|
|
"id": "50b1009e342b"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"config.tool_config = ToolConfig(\n",
|
|
" function_calling_config=FunctionCallingConfig(\n",
|
|
" mode=FunctionCallingConfigMode.AUTO, # The default model behavior. The model decides whether to predict a function call or a natural language response.\n",
|
|
" )\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "a1c7e020ee38"
|
|
},
|
|
"source": [
|
|
"Ask a question about a topic related to publications in arXiv and include the `tool_config` kwarg. Note that you can also set the `tool_config` kwarg globally in the model rather than with every request to generate content:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {
|
|
"id": "0c22b8e9a99a"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/markdown": [
|
|
"**Reinforcement Learning (RL)** is a type of machine learning where an \"agent\" learns to make decisions by interacting with an environment. Instead of being told the correct answer, the agent performs actions and receives feedback in the form of rewards or penalties. Its goal is to develop a strategy, called a policy, that maximizes the total cumulative reward over time through trial and error.\n",
|
|
"\n",
|
|
"Here are four essential papers from arXiv to help you learn more, ranging from introductory overviews to foundational algorithms:\n",
|
|
"\n",
|
|
"### 1. The Best Starting Point (Survey)\n",
|
|
"**[Deep Reinforcement Learning: An Overview](https://arxiv.org/abs/1701.07274)**\n",
|
|
"* **Authors:** Yuxi Li\n",
|
|
"* **Why read it:** This is a comprehensive technical summary of the field. It covers the core concepts, the history of RL, and the various sub-fields like Deep Q-Networks (DQN), Policy Gradients, and Multi-Agent RL. It is an excellent \"map\" of the landscape.\n",
|
|
"\n",
|
|
"### 2. The Modern Foundation\n",
|
|
"**[Playing Atari with Deep Reinforcement Learning](https://arxiv.org/abs/1312.5602)**\n",
|
|
"* **Authors:** Volodymyr Mnih, et al. (DeepMind)\n",
|
|
"* **Why read it:** This is the landmark paper that started the \"Deep RL\" revolution. It describes how DeepMind trained an AI to play Atari games using only the pixels on the screen as input, outperforming humans in several games. It introduces the **Deep Q-Network (DQN)**.\n",
|
|
"\n",
|
|
"### 3. A Practical Deep RL Review\n",
|
|
"**[A Brief Survey of Deep Reinforcement Learning](https://arxiv.org/abs/1708.05866)**\n",
|
|
"* **Authors:** Kai Arulkumaran, et al.\n",
|
|
"* **Why read it:** This paper is slightly more concise than the first survey and focuses specifically on the \"Deep\" aspect of RL. It explains the transition from \"classical\" RL to using neural networks to solve complex problems.\n",
|
|
"\n",
|
|
"### 4. The Industry Standard Algorithm\n",
|
|
"**[Proximal Policy Optimization Algorithms (PPO)](https://arxiv.org/abs/1707.06347)**\n",
|
|
"* **Authors:** John Schulman, et al. (OpenAI)\n",
|
|
"* **Why read it:** PPO is currently one of the most popular and widely used RL algorithms because it is stable and relatively easy to tune. If you want to understand how modern RL agents (like those used in robotics or game AI) are actually trained today, this is the paper to read.\n",
|
|
"\n",
|
|
"***\n",
|
|
"\n",
|
|
"**Pro Tip:** If you are a complete beginner, you should also look into the textbook **\"Reinforcement Learning: An Introduction\"** by Richard Sutton and Andrew Barto. While not an arXiv paper, it is considered the \"Bible\" of the field and is available for free online."
|
|
],
|
|
"text/plain": [
|
|
"<IPython.core.display.Markdown object>"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
}
|
|
],
|
|
"source": [
|
|
"prompt = \"Explain reinforcement learning in a few sentences and give me papers from arXiv to learn more\"\n",
|
|
"\n",
|
|
"response = client.models.generate_content(\n",
|
|
" model=MODEL_ID,\n",
|
|
" contents=prompt,\n",
|
|
" config=config,\n",
|
|
")\n",
|
|
"\n",
|
|
"display(Markdown(response.candidates[0].content.parts[0].text))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "63ee50104549"
|
|
},
|
|
"source": [
|
|
"The response includes a natural language summary to the prompt. However, you were probably hoping to make a function call along the way to search for actual papers in arXiv and return them to the end user!\n",
|
|
"\n",
|
|
"We'll make that happen in the next section by using the forced function calling mode."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "ec647eed5130"
|
|
},
|
|
"source": [
|
|
"## Example: Using Forced Function Calling mode (`ANY`)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "f76d1bb4b4c9"
|
|
},
|
|
"source": [
|
|
"In this example, you'll set the tool configuration to `ANY`, and specify one or more `allowed_function_names` that will force Gemini to make a function call against a function or subset of functions:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {
|
|
"id": "992c70f9a9b8"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"config.tool_config = ToolConfig(\n",
|
|
" function_calling_config=FunctionCallingConfig(\n",
|
|
" mode=FunctionCallingConfigMode.ANY, # ANY mode forces the model to predict a function call from a subset of function names.\n",
|
|
" allowed_function_names=[\n",
|
|
" \"search_arxiv\"\n",
|
|
" ], # Allowed functions to call when mode is ANY, if empty any one of the provided functions will be called.\n",
|
|
" )\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "60a33d7fff56"
|
|
},
|
|
"source": [
|
|
"Now you can ask the same question publications in arXiv with our newly defined `tool_config` that is set to `ANY` function calling mode, which will force the Gemini model to call our search function."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {
|
|
"id": "753d7d208f77"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"FunctionCall(\n",
|
|
" args={\n",
|
|
" 'query': 'Deep Reinforcement Learning survey overview introduction'\n",
|
|
" },\n",
|
|
" name='search_arxiv'\n",
|
|
")"
|
|
]
|
|
},
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"prompt = \"Explain reinforcement learning in a few sentences and give me papers from arXiv to learn more\"\n",
|
|
"\n",
|
|
"response = client.models.generate_content(\n",
|
|
" model=MODEL_ID,\n",
|
|
" contents=prompt,\n",
|
|
" config=config,\n",
|
|
")\n",
|
|
"\n",
|
|
"model_response_content = response.candidates[0].content # Save for thought signature\n",
|
|
"response.function_calls[0]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "4f4715146eca"
|
|
},
|
|
"source": [
|
|
"You can extract the parameters from the model response so that we can use them to make an API call to search papers in arXiv:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {
|
|
"id": "10754b0a94ba"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'query': 'Deep Reinforcement Learning survey overview introduction'}"
|
|
]
|
|
},
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"params = {}\n",
|
|
"for key, value in response.function_calls[0].args.items():\n",
|
|
" params[key] = value\n",
|
|
"params"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {
|
|
"id": "ffc6cc2033f3"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"if response.function_calls[0].name == \"search_arxiv\":\n",
|
|
" arxiv_client = arxiv.Client()\n",
|
|
"\n",
|
|
" search = arxiv.Search(\n",
|
|
" query=params[\"query\"], max_results=3, sort_by=arxiv.SortCriterion.Relevance\n",
|
|
" )\n",
|
|
"\n",
|
|
" results = arxiv_client.results(search)\n",
|
|
" results = str(list(results))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "1f4e8482dcc2"
|
|
},
|
|
"source": [
|
|
"Print a sample of the API response from arXiv:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"metadata": {
|
|
"id": "d9ef5bebc0eb"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"application/vnd.google.colaboratory.intrinsic+json": {
|
|
"type": "string"
|
|
},
|
|
"text/plain": [
|
|
"\"[arxiv.Result(entry_id='http://arxiv.org/abs/1701.07274v6', updated=datetime.datetime(2018, 11, 26, 4, 56, 31, tzinfo=datetime.timezone.utc), published=datetime.datetime(2017, 1, 25, 11, 52, 11, tzinfo=datetime.timezone.utc), title='Deep Reinforcement Learning: An Overview', authors=[arxiv.Result.Author('Yuxi Li')], summary='We give an overview of recent exciting achievements of deep reinforcement learning (RL). We discuss six core elements, six important mechanisms, and twelve applications. We start with background of machine learning, deep learning and reinforcement learning. Next we discuss core RL elements, including value function, in particular, Deep Q-Network (DQN), policy, reward, model, planning, and exploration. After that, we discuss important mechanisms for RL, including attention and memory, unsupervised learning, transfer learning, multi-agent RL, hierarchical RL, and learning to learn. Then we discuss various applications of RL, including games, in particular, AlphaGo, r\""
|
|
]
|
|
},
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"results[:1000]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"metadata": {
|
|
"id": "fae1aaee9fe3"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/markdown": [
|
|
"**Reinforcement Learning (RL)** is a field of machine learning where an autonomous agent learns to make decisions by interacting with an environment. Instead of being told the \"correct\" answer, the agent receives feedback in the form of **rewards or penalties** based on its actions. Its ultimate goal is to develop a strategy, called a policy, that maximizes the total cumulative reward it receives over time.\n",
|
|
"\n",
|
|
"Here are four essential papers from arXiv to help you learn more, ranging from broad overviews to foundational algorithms:\n",
|
|
"\n",
|
|
"### 1. The Comprehensive Overview\n",
|
|
"* **Title:** [Deep Reinforcement Learning: An Overview](https://arxiv.org/abs/1701.07274)\n",
|
|
"* **Author:** Yuxi Li\n",
|
|
"* **Why read it:** This is an excellent starting point. It provides a massive bird's-eye view of the field, covering core elements (value functions, policies, rewards), key mechanisms (attention, memory, transfer learning), and real-world applications like robotics and games.\n",
|
|
"\n",
|
|
"### 2. The Modern Survey\n",
|
|
"* **Title:** [A Brief Survey of Deep Reinforcement Learning](https://arxiv.org/abs/1708.05866)\n",
|
|
"* **Authors:** Kai Arulkumaran, Marc Peter Deisenroth, Miles Brundage, and Anil Anthony Bharath\n",
|
|
"* **Why read it:** This paper bridges the gap between traditional RL and \"Deep\" RL (using neural networks). It explains central algorithms like Deep Q-Networks (DQN) and Actor-Critic methods in a way that is accessible to those familiar with general machine learning.\n",
|
|
"\n",
|
|
"### 3. The Breakthrough (DQN)\n",
|
|
"* **Title:** [Playing Atari with Deep Reinforcement Learning](https://arxiv.org/abs/1312.5602)\n",
|
|
"* **Authors:** Volodymyr Mnih, et al. (DeepMind)\n",
|
|
"* **Why read it:** This is a historic paper. It describes the first deep learning model to successfully learn control policies directly from high-dimensional sensory input (pixels) using reinforcement learning, famously outperforming humans at several Atari 2600 games.\n",
|
|
"\n",
|
|
"### 4. The Industry Standard (PPO)\n",
|
|
"* **Title:** [Proximal Policy Optimization Algorithms](https://arxiv.org/abs/1707.06347)\n",
|
|
"* **Authors:** John Schulman, et al. (OpenAI)\n",
|
|
"* **Why read it:** PPO has become one of the most popular \"default\" algorithms in RL because it is easier to tune and more stable than previous methods. If you want to understand how modern AI (like ChatGPT's fine-tuning process) is trained, this is a key algorithm to know.\n",
|
|
"\n",
|
|
"### Bonus Tip for Learning\n",
|
|
"If you are new to the math behind RL, the most recommended textbook is **\"Reinforcement Learning: An Introduction\"** by Richard Sutton and Andrew Barto. While not an arXiv paper, it is the \"bible\" of the field and is [available for free online](http://incompleteideas.net/book/the-book-2nd.html)."
|
|
],
|
|
"text/plain": [
|
|
"<IPython.core.display.Markdown object>"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
}
|
|
],
|
|
"source": [
|
|
"config.tool_config = None\n",
|
|
"response = client.models.generate_content(\n",
|
|
" model=MODEL_ID,\n",
|
|
" contents=[\n",
|
|
" prompt,\n",
|
|
" model_response_content, # Function call with thought signature\n",
|
|
" Content(\n",
|
|
" role=\"tool\",\n",
|
|
" parts=[\n",
|
|
" Part.from_function_response(\n",
|
|
" name=\"search_arxiv\",\n",
|
|
" response={\n",
|
|
" \"content\": results,\n",
|
|
" },\n",
|
|
" ),\n",
|
|
" ],\n",
|
|
" ),\n",
|
|
" ],\n",
|
|
" config=config,\n",
|
|
")\n",
|
|
"display(Markdown(response.text))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "a97b13c2a45b"
|
|
},
|
|
"source": [
|
|
"In this case, the natural language response contains information about relevant papers based on our function call to the arXiv API."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "a64301e40297"
|
|
},
|
|
"source": [
|
|
"## Example: Disabling Function Calling (`NONE`)\n",
|
|
"\n",
|
|
"In this example, you'll set the tool configuration to `NONE`, which will instruct the Gemini model to behave as if no tools or functions were defined."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"metadata": {
|
|
"id": "cc0c43b9a696"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"config.tool_config = ToolConfig(\n",
|
|
" function_calling_config=FunctionCallingConfig(\n",
|
|
" mode=FunctionCallingConfigMode.NONE, # NONE mode instructs the model to not predict function calls. Equivalent to a model request without any function declarations.\n",
|
|
" )\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"metadata": {
|
|
"id": "7678f426d195"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/markdown": [
|
|
"**Reinforcement Learning (RL)** is a branch of machine learning where an autonomous agent learns to make decisions by interacting with an environment. Instead of being told the \"correct\" answer, the agent performs actions and receives feedback in the form of rewards or penalties. Its ultimate goal is to develop a strategy, called a policy, that maximizes the total cumulative reward over time.\n",
|
|
"\n",
|
|
"Here are four essential papers from arXiv to help you learn more, ranging from foundational overviews to modern breakthroughs:\n",
|
|
"\n",
|
|
"### 1. The Comprehensive Overview\n",
|
|
"**[Deep Reinforcement Learning: An Overview](https://arxiv.org/abs/1701.07274)**\n",
|
|
"* **Author:** Yuxi Li\n",
|
|
"* **Why read it:** This is an excellent starting point. It provides a broad survey of the field, explaining the transition from \"classical\" RL to \"Deep\" RL, and covers major algorithms and applications like games, robotics, and healthcare.\n",
|
|
"\n",
|
|
"### 2. The Deep RL Breakthrough\n",
|
|
"**[Playing Atari with Deep Reinforcement Learning](https://arxiv.org/abs/1312.5602)**\n",
|
|
"* **Authors:** Volodymyr Mnih et al. (DeepMind)\n",
|
|
"* **Why read it:** This is the seminal paper that introduced the **Deep Q-Network (DQN)**. It demonstrated for the first time that an AI could learn to play complex video games directly from raw pixels, outperforming human experts in many cases.\n",
|
|
"\n",
|
|
"### 3. The Modern Standard\n",
|
|
"**[Proximal Policy Optimization Algorithms (PPO)](https://arxiv.org/abs/1707.06347)**\n",
|
|
"* **Authors:** John Schulman et al. (OpenAI)\n",
|
|
"* **Why read it:** PPO has become one of the most popular \"default\" algorithms in RL because it is more stable and easier to tune than previous methods. If you want to understand how modern agents (like those used in robotics or even fine-tuning LLMs) are trained, this is a key paper.\n",
|
|
"\n",
|
|
"### 4. The Future: Learning from Data\n",
|
|
"**[Offline Reinforcement Learning: Tutorial, Review, and Perspectives on Open Problems](https://arxiv.org/abs/2005.00451)**\n",
|
|
"* **Authors:** Sergey Levine et al.\n",
|
|
"* **Why read it:** Traditional RL requires constant interaction with an environment, which can be dangerous or expensive (e.g., in self-driving cars). This paper explains \"Offline RL,\" where agents learn from pre-existing datasets without needing to interact with the world in real-time.\n",
|
|
"\n",
|
|
"***\n",
|
|
"\n",
|
|
"**Pro Tip:** If you are a complete beginner, you should also look for the book *Reinforcement Learning: An Introduction* by **Sutton and Barto**. While not an arXiv paper, it is considered the \"Bible\" of the field and is available for free online."
|
|
],
|
|
"text/plain": [
|
|
"<IPython.core.display.Markdown object>"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
}
|
|
],
|
|
"source": [
|
|
"prompt = \"Explain reinforcement learning in a few sentences and give me papers from arXiv to learn more\"\n",
|
|
"\n",
|
|
"response = client.models.generate_content(\n",
|
|
" model=MODEL_ID,\n",
|
|
" contents=prompt,\n",
|
|
" config=config,\n",
|
|
")\n",
|
|
"\n",
|
|
"display(Markdown(response.text))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "7dc4ee73d814"
|
|
},
|
|
"source": [
|
|
"Note that the model returned a natural language response without making a function call, even though the prompt asked for arXiv papers. In `NONE` mode, the model generates its response entirely from its training data rather than calling the `search_arxiv` function to retrieve real-time results."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"name": "forced_function_calling.ipynb",
|
|
"toc_visible": true
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"name": "python3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|