diff --git a/docs/ais-templates/aistudio_gemini_prompt_freeform_nofiles.ipynb b/docs/ais-templates/aistudio_gemini_prompt_freeform_nofiles.ipynb new file mode 100644 index 00000000..95cdfe7c --- /dev/null +++ b/docs/ais-templates/aistudio_gemini_prompt_freeform_nofiles.ipynb @@ -0,0 +1,279 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "Tce3stUlHN0L" + }, + "source": [ + "##### Copyright 2023 Google LLC" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "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": 5, + "metadata": { + "id": "RXInneX6xx7c" + }, + "outputs": [], + "source": [ + "!pip install -U -q \"google-generativeai>=0.8.2\"" + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "Yw7aMYZ2t0N-" + }, + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "id": "kWIuwKG2_oWE", + "outputId": "31dc66f5-5c9f-4102-e2e6-781b3106d0c9", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 564 + } + }, + "outputs": [ + { + "output_type": "error", + "ename": "MessageError", + "evalue": "Error: credential propagation was unsuccessful", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mMessageError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mgoogle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolab\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdrive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mdrive\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"/gdrive\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;31m# The SDK will automatically read it from the GOOGLE_API_KEY environment variable.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.11/dist-packages/google/colab/drive.py\u001b[0m in \u001b[0;36mmount\u001b[0;34m(mountpoint, force_remount, timeout_ms, readonly)\u001b[0m\n\u001b[1;32m 98\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmountpoint\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mforce_remount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout_ms\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m120000\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreadonly\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 99\u001b[0m \u001b[0;34m\"\"\"Mount your Google Drive at the specified mountpoint path.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 100\u001b[0;31m return _mount(\n\u001b[0m\u001b[1;32m 101\u001b[0m \u001b[0mmountpoint\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 102\u001b[0m \u001b[0mforce_remount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mforce_remount\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.11/dist-packages/google/colab/drive.py\u001b[0m in \u001b[0;36m_mount\u001b[0;34m(mountpoint, force_remount, timeout_ms, ephemeral, readonly)\u001b[0m\n\u001b[1;32m 135\u001b[0m )\n\u001b[1;32m 136\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mephemeral\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 137\u001b[0;31m _message.blocking_request(\n\u001b[0m\u001b[1;32m 138\u001b[0m \u001b[0;34m'request_auth'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 139\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'authType'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'dfs_ephemeral'\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.11/dist-packages/google/colab/_message.py\u001b[0m in \u001b[0;36mblocking_request\u001b[0;34m(request_type, request, timeout_sec, parent)\u001b[0m\n\u001b[1;32m 174\u001b[0m \u001b[0mrequest_type\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mparent\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mparent\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexpect_reply\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 175\u001b[0m )\n\u001b[0;32m--> 176\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mread_reply_from_input\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrequest_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout_sec\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/usr/local/lib/python3.11/dist-packages/google/colab/_message.py\u001b[0m in \u001b[0;36mread_reply_from_input\u001b[0;34m(message_id, timeout_sec)\u001b[0m\n\u001b[1;32m 101\u001b[0m ):\n\u001b[1;32m 102\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;34m'error'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mreply\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 103\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mMessageError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreply\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'error'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 104\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mreply\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'data'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 105\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mMessageError\u001b[0m: Error: credential propagation was unsuccessful" + ] + } + ], + "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": "code", + "source": [ + "# @title Example form fields\n", + "# @markdown Forms support many types of fields.\n", + "\n", + "no_type_checking = '' # @param\n", + "string_type = 'example' # @param {type: \"string\"}\n", + "slider_value = 142 # @param {type: \"slider\", min: 100, max: 200}\n", + "number = 102 # @param {type: \"number\"}\n", + "date = '2010-11-05' # @param {type: \"date\"}\n", + "pick_me = \"monday\" # @param ['monday', 'tuesday', 'wednesday', 'thursday']\n", + "select_or_input = \"apples\" # @param [\"apples\", \"bananas\", \"oranges\"] {allow-input: true}\n", + "# @markdown ---" + ], + "metadata": { + "id": "g1gW9mM_ubOf" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# @title Example form fields\n", + "# @markdown Forms support many types of fields.\n", + "\n", + "no_type_checking = '' # @param\n", + "string_type = 'example' # @param {type: \"string\"}\n", + "slider_value = 142 # @param {type: \"slider\", min: 100, max: 200}\n", + "number = 102 # @param {type: \"number\"}\n", + "date = '2010-11-05' # @param {type: \"date\"}\n", + "pick_me = \"monday\" # @param ['monday', 'tuesday', 'wednesday', 'thursday']\n", + "select_or_input = \"apples\" # @param [\"apples\", \"bananas\", \"oranges\"] {allow-input: true}\n", + "# @markdown ---" + ], + "metadata": { + "id": "h-iZ7VmkueMp" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# @title Example form fields\n", + "# @markdown Forms support many types of fields.\n", + "\n", + "no_type_checking = '' # @param\n", + "string_type = 'example' # @param {type: \"string\"}\n", + "slider_value = 142 # @param {type: \"slider\", min: 100, max: 200}\n", + "number = 102 # @param {type: \"number\"}\n", + "date = '2010-11-05' # @param {type: \"date\"}\n", + "pick_me = \"monday\" # @param ['monday', 'tuesday', 'wednesday', 'thursday']\n", + "select_or_input = \"apples\" # @param [\"apples\", \"bananas\", \"oranges\"] {allow-input: true}\n", + "# @markdown ---" + ], + "metadata": { + "id": "hhcqKxL3ufQN" + }, + "execution_count": null, + "outputs": [] + }, + { + "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": "code", + "source": [], + "metadata": { + "id": "NrPCiw9Lt_LM" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "from google.colab import drive\n", + "drive.mount('/content/drive')" + ], + "metadata": { + "id": "98RE5lG5uE57" + }, + "execution_count": null, + "outputs": [] + } + ], + "metadata": { + "colab": { + "collapsed_sections": [ + "Tce3stUlHN0L" + ], + "name": "aistudio_gemini_prompt_freeform_nofiles.ipynb", + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file