From 7c6ce2f1038b5bcb8724fd65e23885425915d9b1 Mon Sep 17 00:00:00 2001 From: Giom-V Date: Fri, 7 Feb 2025 14:29:58 +0100 Subject: [PATCH 1/3] Ignoring windows links --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f0e93fe6c..a24d05cb5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ **/.ipynb_checkpoints **/venv **/.python-version +**.lnk From e7828af3f4df3d7d65729195a272f22698600785 Mon Sep 17 00:00:00 2001 From: Giom-V Date: Mon, 24 Feb 2025 10:45:30 +0100 Subject: [PATCH 2/3] Improving Code execution notebook * Dataset source and description * Moving helper function out of the setup part as it is quite an important one * Improving readability * Adding code execution example in get started * Renaming PIL Image that was causing issues in case of rerunning cells --- quickstarts/Code_Execution.ipynb | 821 ++++++++++++++++++++----------- quickstarts/Get_started.ipynb | 527 ++++++++------------ 2 files changed, 724 insertions(+), 624 deletions(-) diff --git a/quickstarts/Code_Execution.ipynb b/quickstarts/Code_Execution.ipynb index a7942441b..2297d2a1d 100644 --- a/quickstarts/Code_Execution.ipynb +++ b/quickstarts/Code_Execution.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "cellView": "form", "id": "tuOe1ymfHZPu" @@ -43,7 +43,7 @@ "\n", "\n", " \n", "
\n", - " Run in Google Colab\n", + " Run in Google Colab\n", "
\n" ] @@ -86,7 +86,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": { "id": "46zEFO2a9FFd" }, @@ -95,13 +95,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/130.7 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━\u001b[0m \u001b[32m122.9/130.7 kB\u001b[0m \u001b[31m13.0 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m130.7/130.7 kB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/130.7 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━\u001b[0m \u001b[32m122.9/130.7 kB\u001b[0m \u001b[31m63.6 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m130.7/130.7 kB\u001b[0m \u001b[31m3.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h" ] } ], "source": [ - "!pip install -U -q 'google-genai'" + "!pip install -U -q 'google-genai>=1.2.0'" ] }, { @@ -117,7 +117,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "id": "GC9Cf1BzcYFK" }, @@ -131,91 +131,89 @@ { "cell_type": "markdown", "metadata": { - "id": "ftpY2zPndyh9" + "id": "Me70UWSeZxXG" }, "source": [ - "### Helper functions\n", - "\n", - "When using code execution as a tool, the model returns a list of parts including `text`, `executable_code`, `execution_result`, and `inline_data` parts. Use the function below to help you visualize and better display the code execution results. Here are a few details about the different fields of the results:\n", + "### Initialize SDK client\n", "\n", - "* `text`: Inline text generated by the model.\n", - "* `executable_code`: Code generated by the model that is meant to be executed.\n", - "* `code_execution_result`: Result of the `executable_code`.\n", - "* `inline_data`: Inline media generated by the model." + "With the new SDK you now only need to initialize a client with you API key (or OAuth if using [Vertex AI](https://cloud.google.com/vertex-ai)). The model is now set in each call." ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { - "id": "cCdljxFnx6ab" + "id": "MXR2EZyAaDFB" }, "outputs": [], "source": [ - "from IPython.display import Image, Markdown, Code, HTML\n", - "\n", + "from google import genai\n", "\n", - "def display_code_execution_result(response):\n", - " for part in response.candidates[0].content.parts:\n", - " if part.text is not None:\n", - " display(Markdown(part.text))\n", - " if part.executable_code is not None:\n", - " code_html = f'
{part.executable_code.code}
' # Change code color\n", - " display(HTML(code_html))\n", - " if part.code_execution_result is not None:\n", - " display(Markdown(part.code_execution_result.output))\n", - " if part.inline_data is not None:\n", - " display(Image(data=part.inline_data.data, width=800, format=\"png\"))\n", - " display(Markdown(\"---\"))" + "client = genai.Client(api_key=GOOGLE_API_KEY)" ] }, { "cell_type": "markdown", "metadata": { - "id": "Me70UWSeZxXG" + "id": "h0mM3Axsc2SQ" }, "source": [ - "### Initialize SDK client\n", + "### Choose a model\n", "\n", - "With the new SDK you now only need to initialize a client with you API key (or OAuth if using [Vertex AI](https://cloud.google.com/vertex-ai)). The model is now set in each call." + "This notebook will teach you how to use the [Gemini 2.0 Flash](https://ai.google.dev/gemini-api/docs/models/gemini-v2) model.\n", + "\n", + "For more information about all Gemini models, check the [documentation](https://ai.google.dev/gemini-api/docs/models/gemini) for extended information on each of them." ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { - "id": "MXR2EZyAaDFB" + "id": "9LSf53LPc2qZ" }, "outputs": [], "source": [ - "from google import genai\n", - "\n", - "\n", - "client = genai.Client(api_key=GOOGLE_API_KEY)" + "MODEL_ID = \"gemini-2.0-flash\" # @param [\"gemini-1.5-flash-8b\",\"gemini-1.5-flash-002\",\"gemini-2.0-flash-lite-exp-02-05\",\"gemini-2.0-flash\", \"gemini-2.0-pro-exp-02-05\"] {\"allow-input\":true}" ] }, { "cell_type": "markdown", "metadata": { - "id": "h0mM3Axsc2SQ" + "id": "ftpY2zPndyh9" }, "source": [ - "### Choose a model\n", + "## Helper function\n", "\n", - "This notebook will teach you how to use the [Gemini 2.0 Flash](https://ai.google.dev/gemini-api/docs/models/gemini-v2) model.\n", + "When using code execution as a tool, the model returns a list of parts including `text`, `executable_code`, `execution_result`, and `inline_data` parts. Use the function below to help you visualize and better display the code execution results. Here are a few details about the different fields of the results:\n", "\n", - "For more information about all Gemini models, check the [documentation](https://ai.google.dev/gemini-api/docs/models/gemini) for extended information on each of them." + "* `text`: Inline text generated by the model.\n", + "* `executable_code`: Code generated by the model that is meant to be executed.\n", + "* `code_execution_result`: Result of the `executable_code`.\n", + "* `inline_data`: Inline media generated by the model." ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 49, "metadata": { - "id": "9LSf53LPc2qZ" + "id": "cCdljxFnx6ab" }, "outputs": [], "source": [ - "MODEL_ID = \"gemini-2.0-flash\" # @param [\"gemini-1.5-flash-8b\",\"gemini-1.5-flash-002\",\"gemini-2.0-flash-lite-exp-02-05\",\"gemini-2.0-flash\", \"gemini-2.0-pro-exp-02-05\"] {\"allow-input\":true}" + "from IPython.display import Image, Markdown, Code, HTML\n", + "\n", + "def display_code_execution_result(response):\n", + " for part in response.candidates[0].content.parts:\n", + " if part.text is not None:\n", + " display(Markdown(part.text))\n", + " if part.executable_code is not None:\n", + " code_html = f'
{part.executable_code.code}
' # Change code color\n", + " display(HTML(code_html))\n", + " if part.code_execution_result is not None:\n", + " display(Markdown(part.code_execution_result.output))\n", + " if part.inline_data is not None:\n", + " display(Image(data=part.inline_data.data, width=800, format=\"png\"))\n", + " display(Markdown(\"---\"))" ] }, { @@ -231,14 +229,14 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 50, "metadata": { "id": "o2YY6QnQcyw0" }, "outputs": [ { "data": { - "text/markdown": "Okay, I can calculate the sum of the first 50 prime numbers. I'll use a Python script to generate the prime numbers and then sum them.\n\n", + "text/markdown": "Okay, I need to calculate the sum of the first 50 prime numbers. I will use a Python script to generate the first 50 prime numbers and then sum them.\n\n", "text/plain": [ "" ] @@ -260,29 +258,23 @@ "data": { "text/html": [ "
def is_prime(n):\n",
-              "    if n <= 1:\n",
-              "        return False\n",
-              "    if n <= 3:\n",
-              "        return True\n",
-              "    if n % 2 == 0 or n % 3 == 0:\n",
-              "        return False\n",
-              "    i = 5\n",
-              "    while i * i <= n:\n",
-              "        if n % i == 0 or n % (i + 2) == 0:\n",
-              "            return False\n",
-              "        i = i + 6\n",
-              "    return True\n",
+              "  \"\"\"Returns True if n is a prime number, False otherwise.\"\"\"\n",
+              "  if n <= 1:\n",
+              "    return False\n",
+              "  for i in range(2, int(n**0.5) + 1):\n",
+              "    if n % i == 0:\n",
+              "      return False\n",
+              "  return True\n",
               "\n",
               "primes = []\n",
               "num = 2\n",
               "while len(primes) < 50:\n",
-              "    if is_prime(num):\n",
-              "        primes.append(num)\n",
-              "    num += 1\n",
+              "  if is_prime(num):\n",
+              "    primes.append(num)\n",
+              "  num += 1\n",
               "\n",
               "print(f'{primes=}')\n",
               "print(f'{sum(primes)=}')\n",
-              "\n",
               "
" ], "text/plain": [ @@ -347,8 +339,8 @@ "from google.genai import types\n", "\n", "prompt = \"\"\"\n", - "What is the sum of the first 50 prime numbers?\n", - "Generate and run code for the calculation, and make sure you get all 50.\n", + " What is the sum of the first 50 prime numbers?\n", + " Generate and run code for the calculation, and make sure you get all 50.\n", "\"\"\"\n", "\n", "response = client.models.generate_content(\n", @@ -372,55 +364,42 @@ "source": [ "## Code execution with File I/O\n", "\n", - "The dataset you will use in this guide provides information on various scuba diving experiences, including the location, duration, depth, air consumption, and water temperature. It allows for analysis of diving patterns across different locations and conditions. Here's a breakdown of the columns and what they likely represent:\n", - "\n", - "* Dive No.: A unique identifier for each individual dive.\n", + "The dataset you will use in this guide comes from the [StatLib](http://lib.stat.cmu.edu/datasets/) from the [Department of Statistics](https://www.cmu.edu/dietrich/statistics-datascience/index.html) at [Carnegie Mellon University](http://www.cmu.edu/). It is made available by the [`scikit-learn`](https://scikit-learn.org) under the 3-Clause BSD license.\n", "\n", - "* Country: The country where the dive took place.\n", + "It provides 20k information on various blocks in Californina, including the location (longitute/lattitude), average income,\n", + "housing average age, average rooms, average bedrooms, population,\n", + "average occupation.\n", "\n", - "* Duration: The length of the dive, likely in minutes.\n", - "\n", - "* Max Depth (feet): The maximum depth reached during the dive, measured in feet.\n", - "\n", - "* PSI Remaining: The remaining air pressure in the scuba tank at the end of the dive, measured in pounds per square inch (PSI). This is a measure of how much air was consumed during the dive.\n", - "\n", - "* Water Temperature (F): The water temperature at the dive location, measured in degrees Fahrenheit.\n", + "Here's a breakdown of the columns and what the attributes represent:\n", + "* MedInc: median income in block group\n", + "* HouseAge: median house age in block group\n", + "* AveRooms: average number of rooms per household\n", + "* AveBedrms: average number of bedrooms per household\n", + "* Population: block group population\n", + "* AveOccup: average number of household members\n", + "* Latitude: block group latitude\n", + "* Longitude: block group longitude\n", "\n", "**Note**: Code execution functionality works best with a `.csv` or `.txt` file.\n" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 52, "metadata": { - "id": "M1SctXCP3R9a" + "id": "mk45626-RYxW" }, "outputs": [], "source": [ - "import pathlib\n", - "import requests\n", "import pandas as pd\n", + "from sklearn.datasets import fetch_california_housing\n", "\n", - "\n", - "# Prepare the file to be uploaded\n", - "URL = \"https://storage.googleapis.com/cloud-samples-data/vertex-ai/structured_data/california_housing/california-housing-data.csv\" # @param {type: \"string\"}\n", - "csv_bytes = requests.get(URL).content\n", - "\n", - "csv_path = pathlib.Path('california_houses.csv')\n", - "csv_path.write_bytes(csv_bytes)\n", - "\n", - "# copy just 500 lines from the dataset\n", - "with open('california_houses.csv', 'r') as f_in:\n", - " with open('houses.csv', 'w') as f_out:\n", - " for line in f_in.readlines()[0:100]:\n", - " f_out.write(str(line))\n", - "\n", - "houses_path = pathlib.Path('houses.csv')" + "california_housing = fetch_california_housing(as_frame=True)" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 53, "metadata": { "id": "CYBM1tCf3pOh" }, @@ -434,7 +413,7 @@ }, "text/html": [ "\n", - "
\n", + "
\n", "
\n", "