Files
googlecloudplatform--genera…/notebook_template.ipynb
T
2026-07-13 13:30:30 +08:00

446 lines
17 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ur8xi4C7S06n"
},
"outputs": [],
"source": [
"# Copyright 2026 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": [
"# [TODO]: Add Your Notebook Title Here\n",
"\n",
"**[TODO]: Run the `Generate Links` cell below and replace this section with the generated HTML.**\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/notebook_template.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%2Fnotebook_template.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/notebook_template.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/notebook_template.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/notebook_template.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/notebook_template.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/notebook_template.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/notebook_template.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/notebook_template.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": "code",
"execution_count": null,
"metadata": {
"id": "c0831e65d0de"
},
"outputs": [],
"source": [
"# @title Generate Links { vertical-output: true }\n",
"# @markdown 1. Set the path to this notebook (e.g., `gemini/getting-started/intro.ipynb`).\n",
"# @markdown 2. Run this cell.\n",
"# @markdown 3. Copy the generated HTML into the Markdown cell at the top and then delete this cell.\n",
"\n",
"# -------------------------------------------------------\n",
"# ! AUTHORING NOTE: DO NOT EDIT, DELETE, OR MOVE THIS CELL\n",
"# -------------------------------------------------------\n",
"# This cell is used to generate the correct sharing links\n",
"# for the notebooks. Update the variables, run the cell,\n",
"# and then copy the generated HTML into the markdown cell\n",
"# above. After you have copied the HTML, you can delete\n",
"# this cell.\n",
"# -------------------------------------------------------\n",
"\n",
"# File path from the repository root\n",
"file_path = \"notebook_template.ipynb\" # @param {type:\"string\"}\n",
"\n",
"# Include link to Open in BigQuery Studio\n",
"INCLUDE_BIGQUERY_STUDIO = False # @param {type:\"boolean\"}\n",
"\n",
"import re\n",
"from urllib.parse import quote\n",
"\n",
"from IPython.display import Markdown, display\n",
"\n",
"\n",
"def generate_html(file_path: str) -> str:\n",
" match = re.search(\n",
" r\"(?:https://)?(?:github\\.com/)?(?:GoogleCloudPlatform/)?(?:generative-ai/)?(?:blob/)?(?:main/)?([\\w/-]+.ipynb)\",\n",
" file_path,\n",
" )\n",
" if not match:\n",
" return \"Could not generate table.\"\n",
"\n",
" file_path = match.group(1)\n",
"\n",
" base_url = \"https://github.com/GoogleCloudPlatform/generative-ai/blob/main/\"\n",
" raw_url = (\n",
" \"https://raw.githubusercontent.com/GoogleCloudPlatform/generative-ai/main/\"\n",
" )\n",
" encoded_url = quote(f\"{base_url}{file_path}\")\n",
"\n",
" buttons = [\n",
" (\n",
" \"Open in Colab\",\n",
" f\"https://colab.research.google.com/github/GoogleCloudPlatform/generative-ai/blob/main/{file_path}\",\n",
" \"https://www.gstatic.com/pantheon/images/bigquery/welcome_page/colab-logo.svg\",\n",
" \"Google Colaboratory logo\",\n",
" ),\n",
" (\n",
" \"Open in Colab Enterprise\",\n",
" f\"https://console.cloud.google.com/agent-platform/colab/import/{raw_url.replace('/', '%2F')}{file_path.replace('/', '%2F')}\",\n",
" \"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\",\n",
" \"Google Cloud Colab Enterprise logo\",\n",
" ),\n",
" (\n",
" \"Open in Workbench\",\n",
" f\"https://console.cloud.google.com/agent-platform/workbench/instances?download_url={raw_url}{file_path}\",\n",
" \"https://storage.googleapis.com/github-repo/workbench-icon.svg\",\n",
" \"Workbench logo\",\n",
" ),\n",
" ]\n",
"\n",
" if INCLUDE_BIGQUERY_STUDIO:\n",
" buttons.append(\n",
" (\n",
" \"Open in BigQuery Studio\",\n",
" f\"https://console.cloud.google.com/bigquery/import?url={base_url}{file_path}\",\n",
" \"https://www.gstatic.com/images/branding/gcpiconscolors/bigquery/v1/32px.svg\",\n",
" \"BigQuery Studio logo\",\n",
" )\n",
" )\n",
"\n",
" buttons.append(\n",
" (\n",
" \"View on GitHub\",\n",
" f\"{base_url}{file_path}\",\n",
" \"https://raw.githubusercontent.com/primer/octicons/refs/heads/main/icons/mark-github-24.svg\",\n",
" \"GitHub logo\",\n",
" )\n",
" )\n",
"\n",
" # 2. Define social sharing links (name, href, img_src)\n",
" socials = [\n",
" (\n",
" \"LinkedIn\",\n",
" f\"https://www.linkedin.com/sharing/share-offsite/?url={encoded_url}\",\n",
" \"https://upload.wikimedia.org/wikipedia/commons/8/81/LinkedIn_icon.svg\",\n",
" ),\n",
" (\n",
" \"Bluesky\",\n",
" f\"https://bsky.app/intent/compose?text={encoded_url}\",\n",
" \"https://upload.wikimedia.org/wikipedia/commons/7/7a/Bluesky_Logo.svg\",\n",
" ),\n",
" (\n",
" \"X\",\n",
" f\"https://twitter.com/intent/tweet?url={encoded_url}\",\n",
" \"https://upload.wikimedia.org/wikipedia/commons/5/5a/X_icon_2.svg\",\n",
" ),\n",
" (\n",
" \"Reddit\",\n",
" f\"https://reddit.com/submit?url={encoded_url}\",\n",
" \"https://redditinc.com/hubfs/Reddit%20Inc/Brand/Reddit_Logo.png\",\n",
" ),\n",
" (\n",
" \"Facebook\",\n",
" f\"https://www.facebook.com/sharer/sharer.php?u={encoded_url}\",\n",
" \"https://upload.wikimedia.org/wikipedia/commons/5/51/Facebook_f_logo_%282019%29.svg\",\n",
" ),\n",
" ]\n",
"\n",
" # 3. Generate HTML parts using list comprehensions\n",
" table_cells = \"\\n\".join(\n",
" [\n",
" f\"\"\" <td style=\"text-align: center\">\n",
" <a href=\"{href}\">\n",
" <img width=\"32px\" src=\"{img}\" alt=\"{alt}\"><br> {label}\n",
" </a>\n",
" </td>\"\"\"\n",
" for label, href, img, alt in buttons\n",
" ]\n",
" )\n",
"\n",
" social_links = \"\\n\\n\".join(\n",
" [\n",
" f\"\"\"<a href=\"{href}\" target=\"_blank\">\n",
" <img width=\"20px\" src=\"{img}\" alt=\"{name} logo\">\n",
"</a>\"\"\"\n",
" for name, href, img in socials\n",
" ]\n",
" )\n",
"\n",
" # 4. Combine into final HTML block\n",
" return f\"\"\"```html\n",
"<table align=\"left\">\n",
"{table_cells}\n",
"</table>\n",
"\n",
"<div style=\"clear: both;\"></div>\n",
"\n",
"<p>\n",
"<b>Share to:</b>\n",
"\n",
"{social_links}\n",
"</p>\n",
"```\"\"\"\n",
"\n",
"\n",
"display(Markdown(generate_html(file_path)))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "84f0f73a0f76"
},
"source": [
"| Author(s) |\n",
"| --- |\n",
"| [Firstname Lastname](https://github.com/username) |"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tvgnzT1CKxrO"
},
"source": [
"## Overview\n",
"\n",
"{TODO: Explain what this notebook demonstrates, and what you will learn from the notebook}\n",
"\n",
"{TODO: Add high level bullets for the steps performed in the notebook}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "61RBz8LLbxCR"
},
"source": [
"## Get started"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "No17Cw5hgx12"
},
"source": [
"### Install Google Gen AI SDK and other required packages\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "tFy3H3aPgx12"
},
"outputs": [],
"source": [
"%pip install --upgrade --quiet google-genai"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dmWOrTJ3gx13"
},
"source": [
"### Authenticate your notebook environment\n",
"\n",
"If you are running this notebook in **Google Colab**, run the cell below to authenticate your account."
]
},
{
"cell_type": "code",
"execution_count": null,
"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\n",
"\n",
"To get started using Agent Platform, you must have an existing Google Cloud project and [enable the Agent Platform API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
"\n",
"Learn more about [setting up a project](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects) and a [development environment](https://cloud.google.com/docs/authentication/set-up-adc-local-dev-environment)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Nqwi-5ufWp_B"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"from google import genai\n",
"\n",
"# fmt: off\n",
"PROJECT_ID = \"[your-project-id]\" # @param {type: \"string\", placeholder: \"[your-project-id]\", isTemplate: true}\n",
"LOCATION = \"global\" # @param {type: \"string\", placeholder: \"[your-project-id]\", isTemplate: true}\n",
"# fmt: on\n",
"\n",
"if not PROJECT_ID or PROJECT_ID == \"[your-project-id]\":\n",
" PROJECT_ID = str(os.environ.get(\"GOOGLE_CLOUD_PROJECT\"))\n",
"if not LOCATION:\n",
" LOCATION = os.environ.get(\"GOOGLE_CLOUD_REGION\")\n",
"\n",
"client = genai.Client(enterprise=True, project=PROJECT_ID, location=LOCATION)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5303c05f7aa6"
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6fc324893334"
},
"outputs": [],
"source": [
"from IPython.display import Markdown, display\n",
"\n",
"# [TODO]: Add other library imports here"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EdvJRUWRNGHE"
},
"source": [
"## [TODO] Add your notebook tutorial here"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "e43229f3ad4f"
},
"source": [
"### Load model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "cf93d5f0ce00"
},
"outputs": [],
"source": [
"MODEL_ID = \"gemini-3.5-flash\" # @param {type:\"string\"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2a4e033321ad"
},
"source": [
"## Cleaning up\n",
"\n",
"[TODO]: To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial. Add instructions here for deleting specific resources, for example:\n",
"\n",
"- Datasets\n",
"- Models\n",
"- Endpoints"
]
}
],
"metadata": {
"colab": {
"name": "notebook_template.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}