From 0090535cdc937357439da5963fec65e8b56fb423 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 21 May 2024 13:33:52 -0700 Subject: [PATCH 1/4] Add example for few shot prompting --- examples/prompting/Few_shot_prompting.ipynb | 192 ++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 examples/prompting/Few_shot_prompting.ipynb diff --git a/examples/prompting/Few_shot_prompting.ipynb b/examples/prompting/Few_shot_prompting.ipynb new file mode 100644 index 000000000..c336fba48 --- /dev/null +++ b/examples/prompting/Few_shot_prompting.ipynb @@ -0,0 +1,192 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "# Gemini API: Few-shot prompting" + ], + "metadata": { + "id": "sP8PQnz1QrcF" + } + }, + { + "cell_type": "markdown", + "source": [ + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ], + "metadata": { + "id": "bxGr_x3MRA0z" + } + }, + { + "cell_type": "markdown", + "source": [ + "Some prompts may need a bit more information or require a specific output schema for the LLM to understand and accomplish the requested task. In such cases, providing example questions with answers to the model may greatly increase the quality of the response." + ], + "metadata": { + "id": "ysy--KfNRrCq" + } + }, + { + "cell_type": "code", + "source": [ + "!pip install -U -q google-generativeai" + ], + "metadata": { + "id": "Ne-3gnXqR0hI" + }, + "execution_count": 1, + "outputs": [] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "EconMHePQHGw" + }, + "outputs": [], + "source": [ + "import google.generativeai as genai" + ] + }, + { + "cell_type": "markdown", + "source": [ + "## Configure your API key\n", + "\n", + "To run the following cell, your API key must be stored it in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) for an example." + ], + "metadata": { + "id": "eomJzCa6lb90" + } + }, + { + "cell_type": "code", + "source": [ + "from google.colab import userdata\n", + "GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n", + "\n", + "genai.configure(api_key=GOOGLE_API_KEY)" + ], + "metadata": { + "id": "v-JZzORUpVR2" + }, + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Examples" + ], + "metadata": { + "id": "la7vu95MX8PN" + } + }, + { + "cell_type": "code", + "source": [ + "model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0})" + ], + "metadata": { + "id": "BVEceozQX9Mw" + }, + "execution_count": 9, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "prompt = \"\"\"\n", + "Sort the animals from biggest to smallest.\n", + "Question: Sort Tiger, Bear, Dog\n", + "Answer: Bear > Tiger > Dog}\n", + "Question: Sort Cat, Elephant, Zebra\n", + "Answer: Elephant > Zebra > Cat}\n", + "Question: Sort Whale, Goldfish, Monkey\n", + "Answer:\"\"\"\n", + "model.generate_content(prompt).text" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "i5VGZpBjmpL3", + "outputId": "0feee9b7-75c3-4526-ce7e-2a1c96842030" + }, + "execution_count": 10, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'Answer: Whale > Monkey > Goldfish \\n'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 10 + } + ] + }, + { + "cell_type": "code", + "source": [ + "prompt = \"\"\"\n", + "Extract cities from text, include country they are in.\n", + "USER: I visited Mexico City and Poznan last year\n", + "MODEL: {\"Mexico City\": \"Mexico\", \"Poznan\": \"Poland\"}\n", + "USER: She wanted to visit Lviv, Monaco and Maputo\n", + "MODEL: {\"Minsk\": \"Ukraine\", \"Monaco\": \"Monaco\", \"Maputo\": \"Mozambique\"}\n", + "USER: I am currently in Austin, but I will be moving to Lisbon soon\n", + "MODEL:\"\"\"\n", + "model.generate_content(prompt).text" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "1bKVqbfLoLi8", + "outputId": "a28f899a-ec35-4c76-f7b5-1410e726dce8" + }, + "execution_count": 11, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'```json\\n{\"Austin\": \"United States\", \"Lisbon\": \"Portugal\"}\\n``` \\n'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 11 + } + ] + } + ] +} \ No newline at end of file From bb3b6f035a4452ff6214c7d570a91e8aae35d13a Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 28 May 2024 17:08:44 -0700 Subject: [PATCH 2/4] update few shot prompting example --- examples/prompting/Few_shot_prompting.ipynb | 218 +++++++++++--------- 1 file changed, 125 insertions(+), 93 deletions(-) diff --git a/examples/prompting/Few_shot_prompting.ipynb b/examples/prompting/Few_shot_prompting.ipynb index c336fba48..68f3be73e 100644 --- a/examples/prompting/Few_shot_prompting.ipynb +++ b/examples/prompting/Few_shot_prompting.ipynb @@ -1,64 +1,81 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, "cells": [ { "cell_type": "markdown", + "metadata": { + "id": "Ucyrp0e4qwEr" + }, "source": [ - "# Gemini API: Few-shot prompting" - ], + "##### Copyright 2024 Google LLC." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "cellView": "form", + "id": "LMaMzbbUqxAd" + }, + "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": "sP8PQnz1QrcF" - } + }, + "source": [ + "# Gemini API: Few-shot prompting" + ] }, { "cell_type": "markdown", + "metadata": { + "id": "bxGr_x3MRA0z" + }, "source": [ "\n", " \n", "
\n", - " Run in Google Colab\n", + " Run in Google Colab\n", "
" - ], - "metadata": { - "id": "bxGr_x3MRA0z" - } + ] }, { "cell_type": "markdown", - "source": [ - "Some prompts may need a bit more information or require a specific output schema for the LLM to understand and accomplish the requested task. In such cases, providing example questions with answers to the model may greatly increase the quality of the response." - ], "metadata": { "id": "ysy--KfNRrCq" - } + }, + "source": [ + "Some prompts may need a bit more information or require a specific output schema for the LLM to understand and accomplish the requested task. In such cases, providing example questions with answers to the model may greatly increase the quality of the response." + ] }, { "cell_type": "code", - "source": [ - "!pip install -U -q google-generativeai" - ], + "execution_count": null, "metadata": { "id": "Ne-3gnXqR0hI" }, - "execution_count": 1, - "outputs": [] + "outputs": [], + "source": [ + "!pip install -U -q google-generativeai" + ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "id": "EconMHePQHGw" }, @@ -69,51 +86,72 @@ }, { "cell_type": "markdown", + "metadata": { + "id": "eomJzCa6lb90" + }, "source": [ "## Configure your API key\n", "\n", "To run the following cell, your API key must be stored it in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) for an example." - ], - "metadata": { - "id": "eomJzCa6lb90" - } + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "v-JZzORUpVR2" + }, + "outputs": [], "source": [ "from google.colab import userdata\n", "GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n", "\n", "genai.configure(api_key=GOOGLE_API_KEY)" - ], - "metadata": { - "id": "v-JZzORUpVR2" - }, - "execution_count": 3, - "outputs": [] + ] }, { "cell_type": "markdown", - "source": [ - "## Examples" - ], "metadata": { "id": "la7vu95MX8PN" - } + }, + "source": [ + "## Examples\n", + "\n", + "Use Gemini 1.5 Flash as your model to run through the following examples." + ] }, { "cell_type": "code", - "source": [ - "model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0})" - ], + "execution_count": null, "metadata": { "id": "BVEceozQX9Mw" }, - "execution_count": 9, - "outputs": [] + "outputs": [], + "source": [ + "model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0})" + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "i5VGZpBjmpL3" + }, + "outputs": [ + { + "data": { + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + }, + "text/plain": [ + "'Answer: Whale > Monkey > Goldfish \\n'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "prompt = \"\"\"\n", "Sort the animals from biggest to smallest.\n", @@ -124,34 +162,29 @@ "Question: Sort Whale, Goldfish, Monkey\n", "Answer:\"\"\"\n", "model.generate_content(prompt).text" - ], + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 35 - }, - "id": "i5VGZpBjmpL3", - "outputId": "0feee9b7-75c3-4526-ce7e-2a1c96842030" + "id": "1bKVqbfLoLi8" }, - "execution_count": 10, "outputs": [ { - "output_type": "execute_result", "data": { - "text/plain": [ - "'Answer: Whale > Monkey > Goldfish \\n'" - ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" - } + }, + "text/plain": [ + "'```json\\n{\"Austin\": \"United States\", \"Lisbon\": \"Portugal\"}\\n``` \\n'" + ] }, + "execution_count": 11, "metadata": {}, - "execution_count": 10 + "output_type": "execute_result" } - ] - }, - { - "cell_type": "code", + ], "source": [ "prompt = \"\"\"\n", "Extract cities from text, include country they are in.\n", @@ -162,31 +195,30 @@ "USER: I am currently in Austin, but I will be moving to Lisbon soon\n", "MODEL:\"\"\"\n", "model.generate_content(prompt).text" - ], + ] + }, + { + "cell_type": "markdown", "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 35 - }, - "id": "1bKVqbfLoLi8", - "outputId": "a28f899a-ec35-4c76-f7b5-1410e726dce8" + "id": "UGVE-b_8rEUB" }, - "execution_count": 11, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "'```json\\n{\"Austin\": \"United States\", \"Lisbon\": \"Portugal\"}\\n``` \\n'" - ], - "application/vnd.google.colaboratory.intrinsic+json": { - "type": "string" - } - }, - "metadata": {}, - "execution_count": 11 - } + "source": [ + "## Next steps\n", + "\n", + "Be sure to explore other examples of prompting in the repository. Try writing prompts about classifying your own data, or try some of the other prompting techniques such as zero-shot prompting." ] } - ] -} \ No newline at end of file + ], + "metadata": { + "colab": { + "name": "Few_shot_prompting.ipynb", + "toc_visible": true + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} From 66cc8f74990b796ed37c872bc17abb28cda95cba Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 30 May 2024 13:48:52 -0700 Subject: [PATCH 3/4] Update examples/prompting/Few_shot_prompting.ipynb Co-authored-by: Mark Daoust --- examples/prompting/Few_shot_prompting.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/prompting/Few_shot_prompting.ipynb b/examples/prompting/Few_shot_prompting.ipynb index 68f3be73e..32984d0a4 100644 --- a/examples/prompting/Few_shot_prompting.ipynb +++ b/examples/prompting/Few_shot_prompting.ipynb @@ -194,7 +194,7 @@ "MODEL: {\"Minsk\": \"Ukraine\", \"Monaco\": \"Monaco\", \"Maputo\": \"Mozambique\"}\n", "USER: I am currently in Austin, but I will be moving to Lisbon soon\n", "MODEL:\"\"\"\n", - "model.generate_content(prompt).text" + "model.generate_content(prompt, generation_config={'response_mime_type':'application/json'}).text" ] }, { From 422a8a2d3edc1cd89b172d91802dfac8ed095061 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 30 May 2024 13:48:59 -0700 Subject: [PATCH 4/4] Update examples/prompting/Few_shot_prompting.ipynb Co-authored-by: Mark Daoust --- examples/prompting/Few_shot_prompting.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/prompting/Few_shot_prompting.ipynb b/examples/prompting/Few_shot_prompting.ipynb index 32984d0a4..ac4f17b3a 100644 --- a/examples/prompting/Few_shot_prompting.ipynb +++ b/examples/prompting/Few_shot_prompting.ipynb @@ -128,7 +128,7 @@ }, "outputs": [], "source": [ - "model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0})" + "model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest')" ] }, {