{
  "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": [
        "# Prompt Design - Best Practices\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/prompts/intro_prompt_design.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%2Fprompts%2Fintro_prompt_design.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/prompts/intro_prompt_design.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/prompts/intro_prompt_design.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",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://goo.gle/4fWHlze\">\n",
        "      <img width=\"32px\" src=\"https://cdn.qwiklabs.com/assets/gcp_cloud-e3a77215f0b8bfa9b3f611c0d2208c7e8708ed31.svg\" alt=\"Google Cloud logo\"><br> Open in  Cloud Skills Boost\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/prompts/intro_prompt_design.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/prompts/intro_prompt_design.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/prompts/intro_prompt_design.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/prompts/intro_prompt_design.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/prompts/intro_prompt_design.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": [
        "| Authors |\n",
        "| --- |\n",
        "| [Polong Lin](https://github.com/polong-lin) |\n",
        "| [Karl Weinmeister](https://github.com/kweinmeister) |\n",
        "| [Holt Skinner](https://github.com/holtskinner) |"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tvgnzT1CKxrO"
      },
      "source": [
        "## Overview\n",
        "\n",
        "This notebook covers the essentials of prompt engineering, including some best practices.\n",
        "\n",
        "Learn more about prompt design in the [official documentation](https://cloud.google.com/vertex-ai/docs/generative-ai/text/text-overview).\n",
        "\n",
        "In this notebook, you learn best practices around prompt engineering -- how to design prompts to improve the quality of your responses.\n",
        "\n",
        "This notebook covers the following best practices for prompt engineering:\n",
        "\n",
        "- Be concise\n",
        "- Be specific and well-defined\n",
        "- Ask one task at a time\n",
        "- Turn generative tasks into classification tasks\n",
        "- Improve response quality by including examples"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "61RBz8LLbxCR"
      },
      "source": [
        "## Getting Started"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "No17Cw5hgx12"
      },
      "source": [
        "### Install Google Gen AI SDK\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 (Colab only)\n",
        "\n",
        "Authenticate your environment on Google Colab.\n"
      ]
    },
    {
      "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": "06489bd14f16"
      },
      "source": [
        "### Import libraries\n"
      ]
    },
    {
      "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": null,
      "metadata": {
        "id": "Nqwi-5ufWp_B"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "from IPython.display import Markdown, display\n",
        "from google import genai\n",
        "from google.genai.types import GenerateContentConfig\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 = os.environ.get(\"GOOGLE_CLOUD_REGION\", \"us-central1\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "QqRWdPGmW3NJ"
      },
      "outputs": [],
      "source": [
        "client = genai.Client(enterprise=True, project=PROJECT_ID, location=LOCATION)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OnFPpCRtXRl4"
      },
      "source": [
        "### Load model\n",
        "\n",
        "Learn more about all [Gemini models on Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models#gemini-models)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "IQYu_9SvXQah"
      },
      "outputs": [],
      "source": [
        "MODEL_ID = \"gemini-2.5-flash\"  # @param {type: \"string\"}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cVOtUNJ5X0PY"
      },
      "source": [
        "## Prompt engineering best practices"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uv_e0fEPX60q"
      },
      "source": [
        "Prompt engineering is all about how to design your prompts so that the response is what you were indeed hoping to see.\n",
        "\n",
        "The idea of using \"unfancy\" prompts is to minimize the noise in your prompt to reduce the possibility of the LLM misinterpreting the intent of the prompt. Below are a few guidelines on how to engineer \"unfancy\" prompts.\n",
        "\n",
        "In this section, you'll cover the following best practices when engineering prompts:\n",
        "\n",
        "* Be concise\n",
        "* Be specific, and well-defined\n",
        "* Ask one task at a time\n",
        "* Improve response quality by including examples\n",
        "* Turn generative tasks to classification tasks to improve safety"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0pY4XX0OX9_Y"
      },
      "source": [
        "### Be concise"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xlRpxyxGYA1K"
      },
      "source": [
        "🛑 Not recommended. The prompt below is unnecessarily verbose."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "YKV4G-CfXdbi"
      },
      "outputs": [],
      "source": [
        "prompt = \"What do you think could be a good name for a flower shop that specializes in selling bouquets of dried flowers more than fresh flowers?\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YrJexRHJYnmC"
      },
      "source": [
        "✅ Recommended. The prompt below is to the point and concise."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "VHetn9lCYrXB"
      },
      "outputs": [],
      "source": [
        "prompt = \"Suggest a name for a flower shop that sells bouquets of dried flowers\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eXTAvdOHY0OC"
      },
      "source": [
        "### Be specific, and well-defined"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FTH4GEIgY1dp"
      },
      "source": [
        "Suppose that you want to brainstorm creative ways to describe Earth."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "o5BmXBiGY4KC"
      },
      "source": [
        "🛑 The prompt below might be a bit too generic (which is certainly OK if you'd like to ask a generic question!)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "eHBaMvv7Y6mR"
      },
      "outputs": [],
      "source": [
        "prompt = \"Tell me about Earth\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4iyvEbteZnFL"
      },
      "source": [
        "✅ Recommended. The prompt below is specific and well-defined."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "JQ80z8urZnne"
      },
      "outputs": [],
      "source": [
        "prompt = \"Generate a list of ways that makes Earth unique compared to other planets\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "R5kmfZYHZsJ7"
      },
      "source": [
        "### Ask one task at a time"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rsAezxeYZuUN"
      },
      "source": [
        "🛑 Not recommended. The prompt below has two parts to the question that could be asked separately."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ElywPXpuZtWf"
      },
      "outputs": [],
      "source": [
        "prompt = \"What's the best method of boiling water and why is the sky blue?\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ejzahazBZ8vk"
      },
      "source": [
        "✅ Recommended. The prompts below asks one task a time."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "C5ckp2F0Z_Ba"
      },
      "outputs": [],
      "source": [
        "prompt = \"What's the best method of boiling water?\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "KwUzhud4aA89"
      },
      "outputs": [],
      "source": [
        "prompt = \"Why is the sky blue?\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "PJIL2RTQaGcT"
      },
      "source": [
        "### Watch out for hallucinations"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8Y8kYxrSaHE9"
      },
      "source": [
        "Although LLMs have been trained on a large amount of data, they can generate text containing statements not grounded in truth or reality; these responses from the LLM are often referred to as \"hallucinations\" due to their limited memorization capabilities. Note that simply prompting the LLM to provide a citation isn't a fix to this problem, as there are instances of LLMs providing false or inaccurate citations. Dealing with hallucinations is a fundamental challenge of LLMs and an ongoing research area, so it is important to be cognizant that LLMs may seem to give you confident, correct-sounding statements that are in fact incorrect.\n",
        "\n",
        "Note that if you intend to use LLMs for the creative use cases, hallucinating could actually be quite useful."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8NY5nAGeaJYS"
      },
      "source": [
        "Try the prompt like the one below repeatedly. We set the temperature to `1.0` so that it takes more risks in its choices. It's possible that it may provide an inaccurate, but confident answer."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "QALPjEILaM62"
      },
      "outputs": [],
      "source": [
        "generation_config = GenerateContentConfig(temperature=1.0)\n",
        "\n",
        "prompt = \"What day is it today?\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "BRkwzbgRbhKt"
      },
      "source": [
        "Since LLMs do not have access to real-time information without further integrations, you may have noticed it hallucinates what day it is today in some of the outputs."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3c811e310d02"
      },
      "source": [
        "### Using system instructions to guardrail the model from irrelevant responses\n",
        "\n",
        "How can we attempt to reduce the chances of irrelevant responses and hallucinations?\n",
        "\n",
        "One way is to provide the LLM with [system instructions](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/system-instruction-introduction).\n",
        "\n",
        "Let's see how system instructions works and how you can use them to reduce hallucinations or irrelevant questions for a travel chatbot.\n",
        "\n",
        "Suppose we ask a simple question about one of Italy's most famous tourist spots."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "rB6zJU76biFK"
      },
      "outputs": [],
      "source": [
        "generation_config = GenerateContentConfig(temperature=1.0)\n",
        "\n",
        "chat = client.chats.create(\n",
        "    model=MODEL_ID,\n",
        "    config=GenerateContentConfig(\n",
        "        system_instruction=[\n",
        "            \"Hello! You are an AI chatbot for a travel web site.\",\n",
        "            \"Your mission is to provide helpful queries for travelers.\",\n",
        "            \"Remember that before you answer a question, you must check to see if it complies with your mission.\",\n",
        "            \"If not, you can say, Sorry I can't answer that question.\",\n",
        "        ]\n",
        "    ),\n",
        ")\n",
        "\n",
        "prompt = \"What is the best place for sightseeing in Milan, Italy?\"\n",
        "\n",
        "response = chat.send_message(prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WZa-Qcf9cF4A"
      },
      "source": [
        "Now let us pretend to be a user asks the chatbot a question that is unrelated to travel."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "AZKBIDr2cGnu"
      },
      "outputs": [],
      "source": [
        "prompt = \"How do I make pizza dough at home?\"\n",
        "\n",
        "response = chat.send_message(prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JiUYIhwpctCy"
      },
      "source": [
        "You can see that this way, a guardrail in the prompt prevented the chatbot from veering off course."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZuuDhA37cvmP"
      },
      "source": [
        "### Turn generative tasks into classification tasks to reduce output variability"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kUCUrsUzczmb"
      },
      "source": [
        "#### Generative tasks lead to higher output variability"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "a1xASHAkc46n"
      },
      "source": [
        "The prompt below results in an open-ended response, useful for brainstorming, but response is highly variable."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "nPfXQWIacwRf"
      },
      "outputs": [],
      "source": [
        "prompt = \"I'm a high school student. Recommend me a programming activity to improve my skills.\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iAmm9wPYc_1o"
      },
      "source": [
        "#### Classification tasks reduces output variability"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VvRpK_0GdCpf"
      },
      "source": [
        "The prompt below results in a choice and may be useful if you want the output to be easier to control."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "kYDKh0r2dAqo"
      },
      "outputs": [],
      "source": [
        "prompt = \"\"\"I'm a high school student. Which of these activities do you suggest and why:\n",
        "a) learn Python\n",
        "b) learn JavaScript\n",
        "c) learn Fortran\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iTd60b1GdIsx"
      },
      "source": [
        "### Improve response quality by including examples"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yJi44NejdJYE"
      },
      "source": [
        "Another way to improve response quality is to add examples in your prompt. The LLM learns in-context from the examples on how to respond. Typically, one to five examples (shots) are enough to improve the quality of responses. Including too many examples can cause the model to over-fit the data and reduce the quality of responses.\n",
        "\n",
        "Similar to classical model training, the quality and distribution of the examples is very important. Pick examples that are representative of the scenarios that you need the model to learn, and keep the distribution of the examples (e.g. number of examples per class in the case of classification) aligned with your actual distribution."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sMbLginWdOKs"
      },
      "source": [
        "#### Zero-shot prompt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Crh2Loi2dQ0v"
      },
      "source": [
        "Below is an example of zero-shot prompting, where you don't provide any examples to the LLM within the prompt itself."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "-7myRc-SdTQ4"
      },
      "outputs": [],
      "source": [
        "prompt = \"\"\"Decide whether a Tweet's sentiment is positive, neutral, or negative.\n",
        "\n",
        "Tweet: I loved the new YouTube video you made!\n",
        "Sentiment:\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ucRtPn9SdL64"
      },
      "source": [
        "#### One-shot prompt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rs0gQH2vdYBi"
      },
      "source": [
        "Below is an example of one-shot prompting, where you provide one example to the LLM within the prompt to give some guidance on what type of response you want."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "iEq-KxGYdaT5"
      },
      "outputs": [],
      "source": [
        "prompt = \"\"\"Decide whether a Tweet's sentiment is positive, neutral, or negative.\n",
        "\n",
        "Tweet: I loved the new YouTube video you made!\n",
        "Sentiment: positive\n",
        "\n",
        "Tweet: That was awful. Super boring 😠\n",
        "Sentiment:\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JnKLjJzmdfL_"
      },
      "source": [
        "#### Few-shot prompt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6Zv-9F5OdgI_"
      },
      "source": [
        "Below is an example of few-shot prompting, where you provide a few examples to the LLM within the prompt to give some guidance on what type of response you want."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "u37P9tG4dk9S"
      },
      "outputs": [],
      "source": [
        "prompt = \"\"\"Decide whether a Tweet's sentiment is positive, neutral, or negative.\n",
        "\n",
        "Tweet: I loved the new YouTube video you made!\n",
        "Sentiment: positive\n",
        "\n",
        "Tweet: That was awful. Super boring 😠\n",
        "Sentiment: negative\n",
        "\n",
        "Tweet: Something surprised me about this video - it was actually original. It was not the same old recycled stuff that I always see. Watch it - you will not regret it.\n",
        "Sentiment:\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wDMD3xb2dvX6"
      },
      "source": [
        "#### Choosing between zero-shot, one-shot, few-shot prompting methods"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "s92W0YpNdxJp"
      },
      "source": [
        "Which prompt technique to use will solely depends on your goal. The zero-shot prompts are more open-ended and can give you creative answers, while one-shot and few-shot prompts teach the model how to behave so you can get more predictable answers that are consistent with the examples provided."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gemini3_intro"
      },
      "source": [
        "## Updates for Gemini 3 Pro\n",
        "\n",
        "**Quality Changes:**\n",
        "Customers migrating from 2.5 Pro to 3 Pro can expect to see significant improvements in high-level reasoning, complex instruction following, use of tools / agentic use cases, and better long context capabilities (including image and document understanding).\n",
        "\n",
        "**Behavior Changes:**\n",
        "Gemini 3 Pro is designed for high efficiency and action. It defaults to concise, direct answers and attempts to solve user intent immediately. Because the model prioritizes being helpful, it may occasionally guess when information is missing or prioritize a satisfying answer over strict instructions. Users can steer the model to curb this behavior with prompting.\n",
        "\n",
        "**Prompt Optimization:**\n",
        "We've detailed strategies that can help you control the model's output, ensure factual accuracy, and strictly enforce constraints. For any other quality regressions, please try prompting Gemini 3 Pro with the regression you're seeing like so:\n",
        "\n",
        "```md\n",
        "You are an expert prompt engineer. Rewrite the **Original Prompt** below to prevent the specific failure described in the **Bad Response** and **Feedback**. The new prompt should address the constraints and logic gaps that caused the error.\n",
        "\n",
        "**Original Prompt:**\n",
        "[Insert Prompt]\n",
        "\n",
        "**Bad Response:**\n",
        "[Insert Bad Response]\n",
        "\n",
        "**Feedback:**\n",
        "[Insert Feedback]\n",
        "\n",
        "**Output:** Provide only the optimized prompt with the changes that were made.\n",
        "```\n",
        "\n",
        "**Known Regressions:**\n",
        "3 Pro models do not prioritize supporting audio understanding or image segmentation use cases (use 2.5 Flash or Pro). For information dense or complicated graphs, tables, charts, etc, the model can incorrectly extract information or misinterpret them."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gemini3_model_setup"
      },
      "outputs": [],
      "source": [
        "MODEL_ID = \"gemini-3.1-pro-preview\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gemini3_temperature"
      },
      "source": [
        "### Temperature\n",
        "\n",
        "For Gemini 3, we strongly recommend keeping the temperature parameter at its default value of `1.0`.\n",
        "\n",
        "While previous models often benefited from tuning temperature to control creativity versus determinism, Gemini 3's reasoning capabilities are optimized for the default setting. Changing the temperature (setting it below `1.0`) may lead to unexpected behavior, such as looping or degraded performance, particularly in complex mathematical or reasoning tasks."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gemini3_deduction"
      },
      "source": [
        "### I. Distinguish between deduction and external information\n",
        "\n",
        "When you instruct the model with \"do not infer\" or \"do not guess,\" it may interpret this too strictly and refuse to perform basic logic (like arithmetic) or synthesize information found in different parts of a document.\n",
        "\n",
        "Instead of a blanket negative constraint, explicitly tell the model to use the provided text for deductions while banning outside knowledge."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gemini3_deduction_code"
      },
      "outputs": [],
      "source": [
        "less_effective_prompt = \"\"\"What was the profit? Do not infer.\"\"\"\n",
        "prompt = \"\"\"You are expected to perform calculations and logical deductions based strictly on the provided text. Do not introduce external information.\n",
        "\n",
        "Text: The company earned 50M revenue and had 30M costs.\n",
        "Question: What was the profit?\"\"\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gemini3_split_step"
      },
      "source": [
        "### II. Use split-step verification for unknown topics\n",
        "\n",
        "When the model encounters a topic it does not know (such as an obscure person) or a capability it does not have (such as accessing a specific live URL), it may generate plausible but incorrect information to satisfy the request.\n",
        "\n",
        "To prevent this, split the prompt into two steps: first verify the information or capability exists, then generate the answer."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gemini3_split_step_code"
      },
      "outputs": [],
      "source": [
        "# Example prompt structure for checking capabilities before executing\n",
        "prompt = \"\"\"Verify with high confidence if you're able to access the NYT page. If you cannot verify, state 'No Info' and STOP. If verified, proceed to generate a response.\n",
        "\n",
        "Query: Summarize the headlines from NYT today\"\"\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gemini3_constraints_end"
      },
      "source": [
        "### III. Place most important constraints at the end of the prompt\n",
        "\n",
        "When dealing with complex requests, it may drop negative constraints (what *not* to do) or formatting or quantitative constraints (word counts) if they appear early in the prompt.\n",
        "\n",
        "Place your core ask and most critical restrictions, especially negative ones, as the final line of your instruction."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gemini3_constraints_code"
      },
      "outputs": [],
      "source": [
        "prompt = \"\"\"[Context and Source Material]\n",
        "Here is a list of fruits: Apple, Banana, Orange, Mango, Grape.\n",
        "\n",
        "[Main Task Instructions]\n",
        "Write a poem about these fruits.\n",
        "\n",
        "[Negative, Formatting, Quantitative Constraints]\n",
        "Do not use the letter 'e' in your poem. Keep it under 20 words.\"\"\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gemini3_persona"
      },
      "source": [
        "### IV. Use persona carefully\n",
        "\n",
        "The model treats the persona it is assigned seriously and will, sometimes, ignore instructions in order to maintain adherence to the described persona.\n",
        "\n",
        "Review the persona that's assigned to the model and avoid ambiguous situations."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gemini3_persona_code"
      },
      "outputs": [],
      "source": [
        "prompt = \"\"\"You are a data extractor. You are forbidden from clarifying, explaining, or expanding terms. Output text exactly as it appears. Do not explain why.\n",
        "\n",
        "Input: The user ID is 12345 (active status).\n",
        "Extract: User ID\"\"\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gemini3_grounding"
      },
      "source": [
        "### V. Help maintain grounding\n",
        "\n",
        "The model has strong internal knowledge and a desire to be helpful. If you provide a hypothetical scenario that contradicts real-world facts (e.g., \"The car was invented in 2024\"), the model may revert to its training data (1886) rather than your prompt or go off-book to be as helpful as possible.\n",
        "\n",
        "Explicitly state that the provided context is the only source of truth for the current session."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gemini3_grounding_code"
      },
      "outputs": [],
      "source": [
        "prompt = \"\"\"Context: In this hypothetical world, the car was invented in the year 2024 by a time traveler.\n",
        "Question: When was the car invented? Disregard all outside knowledge. Use only the provided context, even if it contradicts facts you know.\"\"\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gemini3_synthesis"
      },
      "source": [
        "### VI. Synthesize information across the entire document / data\n",
        "\n",
        "If a document separates related information (e.g., a definition on Page 1 and a rule on Page 50), the model may stop reading after the first relevant match to avoid \"guessing.\"\n",
        "\n",
        "When working with large datasets (e.g., entire books, codebases, or long videos), place your specific instructions or questions at the end of the prompt, after the data context. Anchor the model's reasoning to the provided data by starting your question with a phrase like, \"Based on the entire document above...\"."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gemini3_synthesis_code"
      },
      "outputs": [],
      "source": [
        "prompt = \"\"\"[Document Context Start]\n",
        "Part 1: The 'Flirble' is a small, blue creature.\n",
        "...\n",
        "[Long gap in text]\n",
        "...\n",
        "Part 10: Flirbles are known to fly only at night.\n",
        "[Document Context End]\n",
        "\n",
        "Based on the entire document above, provide a comprehensive answer. Synthesize all relevant information from the text that pertains to the question's scenario.\n",
        "Question: Describe the characteristics of a Flirble.\"\"\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gemini3_verbosity"
      },
      "source": [
        "### VII. Steering output verbosity\n",
        "\n",
        "By default, Gemini 3.0 is less verbose and prefers providing direct, efficient answers.\n",
        "\n",
        "If your use case requires a more conversational or \"chatty\" persona, you must explicitly steer the model in the prompt."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gemini3_verbosity_code"
      },
      "outputs": [],
      "source": [
        "prompt = \"Explain quantum entanglement as a friendly, talkative assistant.\"\n",
        "\n",
        "response = client.models.generate_content(model=MODEL_ID, contents=prompt)\n",
        "display(Markdown(response.text))"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "intro_prompt_design.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
