From 66e3fb9286781ce67303f949acc4d184fdf87332 Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Thu, 9 Jan 2025 10:38:05 -0800 Subject: [PATCH 1/5] Templates have moved b/379307117 --- {templates => quickstarts}/Template.ipynb | 0 .../aistudio_gemini_prompt_freeform.ipynb | 332 ------------------ ...tudio_gemini_prompt_freeform_nofiles.ipynb | 167 --------- 3 files changed, 499 deletions(-) rename {templates => quickstarts}/Template.ipynb (100%) delete mode 100644 templates/aistudio_gemini_prompt_freeform.ipynb delete mode 100644 templates/aistudio_gemini_prompt_freeform_nofiles.ipynb diff --git a/templates/Template.ipynb b/quickstarts/Template.ipynb similarity index 100% rename from templates/Template.ipynb rename to quickstarts/Template.ipynb diff --git a/templates/aistudio_gemini_prompt_freeform.ipynb b/templates/aistudio_gemini_prompt_freeform.ipynb deleted file mode 100644 index f53d3b2a5..000000000 --- a/templates/aistudio_gemini_prompt_freeform.ipynb +++ /dev/null @@ -1,332 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "Tce3stUlHN0L" - }, - "source": [ - "##### Copyright 2023 Google LLC" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cellView": "form", - "id": "tuOe1ymfHZPu" - }, - "outputs": [], - "source": [ - "# @title 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": "FKwyTRdwB8aW" - }, - "source": [ - "## Setup" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "RXInneX6xx7c" - }, - "outputs": [], - "source": [ - "!pip install -U -q \"google-generativeai>=0.8.2\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cellView": "form", - "id": "kWIuwKG2_oWE" - }, - "outputs": [], - "source": [ - "# import necessary modules.\n", - "import base64\n", - "import copy\n", - "import json\n", - "import pathlib\n", - "import requests\n", - "\n", - "\n", - "import PIL.Image\n", - "import IPython.display\n", - "from IPython.display import Markdown\n", - "\n", - "try:\n", - " # The SDK will automatically read it from the GOOGLE_API_KEY environment variable.\n", - " # In Colab get the key from Colab-secrets (\"🔑\" in the left panel).\n", - " import os\n", - " from google.colab import userdata\n", - "\n", - " os.environ[\"GOOGLE_API_KEY\"] = userdata.get(\"GOOGLE_API_KEY\")\n", - "except ImportError:\n", - " pass\n", - "\n", - "import google.generativeai as genai\n", - "\n", - "# Parse the arguments\n", - "\n", - "model = \"gemini-1.5-flash\" # @param {isTemplate: true}\n", - "contents_b64 = \"W3sicGFydHMiOiBbeyJ0ZXh0IjogIldoYXQncyBpbiB0aGlzIHBpY3R1cmU/In0sIHsiZmlsZV9kYXRhIjogeyJ1cmwiOiAiaHR0cHM6Ly9zdG9yYWdlLmdvb2dsZWFwaXMuY29tL2dlbmVyYXRpdmVhaS1kb3dubG9hZHMvaW1hZ2VzL3Njb25lcy5qcGciLCAibWltZV90eXBlIjogImltYWdlL2pwZWcifX1dfV0=\" # @param {isTemplate: true}\n", - "generation_config_b64 = \"e30=\" # @param {isTemplate: true}\n", - "safety_settings_b64 = \"e30=\" # @param {isTemplate: true}\n", - "\n", - "gais_contents = json.loads(base64.b64decode(contents_b64))\n", - "\n", - "generation_config = json.loads(base64.b64decode(generation_config_b64))\n", - "safety_settings = json.loads(base64.b64decode(safety_settings_b64))\n", - "\n", - "stream = False\n", - "\n", - "# Convert and upload the files\n", - "\n", - "tempfiles = pathlib.Path(f\"tempfiles\")\n", - "tempfiles.mkdir(parents=True, exist_ok=True)\n", - "\n", - "\n", - "drive = None\n", - "def upload_file_data(file_data, index):\n", - " \"\"\"Upload files to the Files API.\n", - "\n", - " For each file, Google AI Studio either sent:\n", - " - a Google Drive ID,\n", - " - a URL,\n", - " - a file path, or\n", - " - The raw bytes (`inline_data`).\n", - "\n", - " The API only understands `inline_data` or it's Files API.\n", - " This code, uploads files to the files API where the API can access them.\n", - " \"\"\"\n", - "\n", - " mime_type = file_data[\"mime_type\"]\n", - " if drive_id := file_data.pop(\"drive_id\", None):\n", - " if drive is None:\n", - " from google.colab import drive\n", - " drive.mount(\"/gdrive\")\n", - "\n", - " path = next(\n", - " pathlib.Path(f\"/gdrive/.shortcut-targets-by-id/{drive_id}\").glob(\"*\")\n", - " )\n", - " print(\"Uploading:\", str(path))\n", - " file_info = genai.upload_file(path=path, mime_type=mime_type)\n", - " file_data[\"file_uri\"] = file_info.uri\n", - " return\n", - "\n", - " if url := file_data.pop(\"url\", None):\n", - " response = requests.get(url)\n", - " data = response.content\n", - " name = url.split(\"/\")[-1]\n", - " path = tempfiles / str(index)\n", - " path.write_bytes(data)\n", - " print(\"Uploading:\", url)\n", - " file_info = genai.upload_file(path, display_name=name, mime_type=mime_type)\n", - " file_data[\"file_uri\"] = file_info.uri\n", - " return\n", - "\n", - " if name := file_data.get(\"filename\", None):\n", - " if not pathlib.Path(name).exists():\n", - " raise IOError(\n", - " f\"local file: `{name}` does not exist. You can upload files \"\n", - " 'to Colab using the file manager (\"📁 Files\" in the left '\n", - " \"toolbar)\"\n", - " )\n", - " file_info = genai.upload_file(path, display_name=name, mime_type=mime_type)\n", - " file_data[\"file_uri\"] = file_info.uri\n", - " return\n", - "\n", - " if \"inline_data\" in file_data:\n", - " return\n", - "\n", - " raise ValueError(\"Either `drive_id`, `url` or `inline_data` must be provided.\")\n", - "\n", - "\n", - "contents = copy.deepcopy(gais_contents)\n", - "\n", - "index = 0\n", - "for content in contents:\n", - " for n, part in enumerate(content[\"parts\"]):\n", - " if file_data := part.get(\"file_data\", None):\n", - " upload_file_data(file_data, index)\n", - " index += 1\n", - "\n", - "import json\n", - "print(json.dumps(contents, indent=4))" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "E7zAD69vE92b" - }, - "source": [ - "## Call `generate_content`" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "LB2LxPmAB95V" - }, - "outputs": [], - "source": [ - "from IPython.display import display\n", - "from IPython.display import Markdown\n", - "\n", - "# Call the model and print the response.\n", - "gemini = genai.GenerativeModel(model_name=model)\n", - "\n", - "response = gemini.generate_content(\n", - " contents,\n", - " generation_config=generation_config,\n", - " safety_settings=safety_settings,\n", - " stream=stream,\n", - ")\n", - "\n", - "display(Markdown(response.text))" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "9c9d345e9868" - }, - "source": [ - "\n", - " \n", - " \n", - "
\n", - " Docs on ai.google.dev\n", - " \n", - " More notebooks in the Cookbook\n", - "
" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "F91AeeGO1ncU" - }, - "source": [ - "## [optional] Show the conversation\n", - "\n", - "This section displays the conversation received from Google AI Studio." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cellView": "form", - "id": "yoL3p3KPylFW" - }, - "outputs": [], - "source": [ - "# @title Show the conversation, in colab.\n", - "import mimetypes\n", - "\n", - "def show_file(file_data):\n", - " mime_type = file_data[\"mime_type\"]\n", - "\n", - " if drive_id := file_data.get(\"drive_id\", None):\n", - " path = next(\n", - " pathlib.Path(f\"/gdrive/.shortcut-targets-by-id/{drive_id}\").glob(\"*\")\n", - " )\n", - " name = path\n", - " # data = path.read_bytes()\n", - " kwargs = {\"filename\": path}\n", - " elif url := file_data.get(\"url\", None):\n", - " name = url\n", - " kwargs = {\"url\": url}\n", - " # response = requests.get(url)\n", - " # data = response.content\n", - " elif data := file_data.get(\"inline_data\", None):\n", - " name = None\n", - " kwargs = {\"data\": data}\n", - " elif name := file_data.get(\"filename\", None):\n", - " if not pathlib.Path(name).exists():\n", - " raise IOError(\n", - " f\"local file: `{name}` does not exist. You can upload files to \"\n", - " 'Colab using the file manager (\"📁 Files\"in the left toolbar)'\n", - " )\n", - " else:\n", - " raise ValueError(\"Either `drive_id`, `url` or `inline_data` must be provided.\")\n", - "\n", - " print(f\"File:\\n name: {name}\\n mime_type: {mime_type}\\n\")\n", - " return\n", - "\n", - " format = mimetypes.guess_extension(mime_type).strip(\".\")\n", - " if mime_type.startswith(\"image/\"):\n", - " image = IPython.display.Image(**kwargs, width=256)\n", - " IPython.display.display(image)\n", - " print()\n", - " return\n", - "\n", - " if mime_type.startswith(\"audio/\"):\n", - " if len(data) < 2**12:\n", - " audio = IPython.display.Audio(**kwargs)\n", - " IPython.display.display(audio)\n", - " print()\n", - " return\n", - "\n", - " if mime_type.startswith(\"video/\"):\n", - " if len(data) < 2**12:\n", - " audio = IPython.display.Video(**kwargs, mimetype=mime_type)\n", - " IPython.display.display(audio)\n", - " print()\n", - " return\n", - "\n", - " print(f\"File:\\n name: {name}\\n mime_type: {mime_type}\\n\")\n", - "\n", - "\n", - "for content in gais_contents:\n", - " if role := content.get(\"role\", None):\n", - " print(\"Role:\", role, \"\\n\")\n", - "\n", - " for n, part in enumerate(content[\"parts\"]):\n", - " if text := part.get(\"text\", None):\n", - " print(text, \"\\n\")\n", - "\n", - " elif file_data := part.get(\"file_data\", None):\n", - " show_file(file_data)\n", - "\n", - " print(\"-\" * 80, \"\\n\")" - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [ - "Tce3stUlHN0L" - ], - "name": "aistudio_gemini_prompt_freeform.ipynb", - "toc_visible": true - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/templates/aistudio_gemini_prompt_freeform_nofiles.ipynb b/templates/aistudio_gemini_prompt_freeform_nofiles.ipynb deleted file mode 100644 index 5182f136b..000000000 --- a/templates/aistudio_gemini_prompt_freeform_nofiles.ipynb +++ /dev/null @@ -1,167 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "Tce3stUlHN0L" - }, - "source": [ - "##### Copyright 2023 Google LLC" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cellView": "form", - "id": "tuOe1ymfHZPu" - }, - "outputs": [], - "source": [ - "# @title 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": "FKwyTRdwB8aW" - }, - "source": [ - "## Setup" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "RXInneX6xx7c" - }, - "outputs": [], - "source": [ - "!pip install -U -q \"google-generativeai>=0.8.2\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cellView": "form", - "id": "kWIuwKG2_oWE" - }, - "outputs": [], - "source": [ - "# import necessary modules.\n", - "\n", - "import google.generativeai as genai\n", - "\n", - "import base64\n", - "import json\n", - "\n", - "try:\n", - " # Mount google drive\n", - " from google.colab import drive\n", - "\n", - " drive.mount(\"/gdrive\")\n", - "\n", - " # The SDK will automatically read it from the GOOGLE_API_KEY environment variable.\n", - " # In Colab get the key from Colab-secrets (\"🔑\" in the left panel).\n", - " import os\n", - " from google.colab import userdata\n", - "\n", - " os.environ[\"GOOGLE_API_KEY\"] = userdata.get(\"GOOGLE_API_KEY\")\n", - "except ImportError:\n", - " pass\n", - "\n", - "# Parse the arguments\n", - "\n", - "model = \"gemini-1.5-flash\" # @param {isTemplate: true}\n", - "contents_b64 = b'W3sicGFydHMiOiBbeyJ0ZXh0IjogIkhlbGxvIn1dfV0='\n", - "generation_config_b64 = \"e30=\" # @param {isTemplate: true}\n", - "safety_settings_b64 = \"e30=\" # @param {isTemplate: true}\n", - "\n", - "contents = json.loads(base64.b64decode(contents_b64))\n", - "\n", - "generation_config = json.loads(base64.b64decode(generation_config_b64))\n", - "safety_settings = json.loads(base64.b64decode(safety_settings_b64))\n", - "\n", - "stream = False\n", - "\n", - "print(json.dumps(contents, indent=4))" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "E7zAD69vE92b" - }, - "source": [ - "## Call `generate_content`" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "LB2LxPmAB95V" - }, - "outputs": [], - "source": [ - "from IPython.display import display\n", - "from IPython.display import Markdown\n", - "\n", - "# Call the model and print the response.\n", - "gemini = genai.GenerativeModel(model_name=model)\n", - "\n", - "response = gemini.generate_content(\n", - " contents,\n", - " generation_config=generation_config,\n", - " safety_settings=safety_settings,\n", - " stream=stream,\n", - ")\n", - "\n", - "display(Markdown(response.text))" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "9c9d345e9868" - }, - "source": [ - "\n", - " \n", - " \n", - "
\n", - " Docs on ai.google.dev\n", - " \n", - " More notebooks in the Cookbook\n", - "
" - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [ - "Tce3stUlHN0L" - ], - "name": "aistudio_gemini_prompt_freeform_nofiles.ipynb", - "toc_visible": true - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} From 66ef8e31b46c820bb81acc0006fcc6099c6df30d Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Thu, 9 Jan 2025 10:48:19 -0800 Subject: [PATCH 2/5] Update Template.ipynb --- quickstarts/Template.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickstarts/Template.ipynb b/quickstarts/Template.ipynb index 2961721f2..632d264fb 100644 --- a/quickstarts/Template.ipynb +++ b/quickstarts/Template.ipynb @@ -48,7 +48,7 @@ "source": [ "\n", " \n", "
\n", - " Run in Google Colab\n", + " Run in Google Colab\n", "
\n" ] From 4abb9ceb3a28774ebac2edd737667f8945c5bdde Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Thu, 9 Jan 2025 10:49:26 -0800 Subject: [PATCH 3/5] Update CONTRIBUTING.md --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9e20f3bbc..61e663600 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -33,7 +33,7 @@ Before you send a PR, or even write a single line, please file an [issue](https: Adding a new guide often involves lots of detailed reviews and we want to make sure that your idea is fully formed and has full support before you start writing anything. If you want to port an existing guide across (e.g. if you have a guide for Gemini on your own GitHub), feel free to link to it in the issue. When you're ready, start by using the [notebook -template](./templates/Template.ipynb) and following the guidance within. +template](./quickstarts/Template.ipynb) and following the guidance within. ## Things we consider From 9033c043244efd8b45d793e9718ea019a346eff9 Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Thu, 9 Jan 2025 10:50:35 -0800 Subject: [PATCH 4/5] Update CONTRIBUTING.md --- examples/CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/CONTRIBUTING.md b/examples/CONTRIBUTING.md index c1329575d..dc1c33a94 100644 --- a/examples/CONTRIBUTING.md +++ b/examples/CONTRIBUTING.md @@ -23,7 +23,7 @@ Before writing anything, [file an issue](https://github.com/google-gemini/cookbo * For example, if you are worried that your English is not good enough, please call that out here. We are an inclusive community and will work with you to meet our high publication standards if your proposal is otherwise accepted. When you're ready to start writing, make a copy of the [notebook -template](./templates/Template.ipynb) and follow the guidance within. +template](./quickstarts/Template.ipynb) and follow the guidance within. All contributions must also adhere to the Gemini API's [terms of service](https://ai.google.dev/gemini-api/terms). From 7d9cf6fb3d292abcd6d0ffb6a1087099574a9887 Mon Sep 17 00:00:00 2001 From: Mark McDonald Date: Mon, 13 Jan 2025 12:22:21 +0800 Subject: [PATCH 5/5] Fix relative link in examples/CONTRIB --- examples/CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/CONTRIBUTING.md b/examples/CONTRIBUTING.md index dc1c33a94..9eeb85f31 100644 --- a/examples/CONTRIBUTING.md +++ b/examples/CONTRIBUTING.md @@ -23,7 +23,7 @@ Before writing anything, [file an issue](https://github.com/google-gemini/cookbo * For example, if you are worried that your English is not good enough, please call that out here. We are an inclusive community and will work with you to meet our high publication standards if your proposal is otherwise accepted. When you're ready to start writing, make a copy of the [notebook -template](./quickstarts/Template.ipynb) and follow the guidance within. +template](../quickstarts/Template.ipynb) and follow the guidance within. All contributions must also adhere to the Gemini API's [terms of service](https://ai.google.dev/gemini-api/terms).