From bdcca35718acfaca886460da995ccab5d1f537d6 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 30 May 2024 14:26:54 -0700 Subject: [PATCH] Add example for few shot prompting (#163) * Add example for few shot prompting * update few shot prompting example * Update examples/prompting/Few_shot_prompting.ipynb Co-authored-by: Mark Daoust * Update examples/prompting/Few_shot_prompting.ipynb Co-authored-by: Mark Daoust --------- Co-authored-by: Mark Daoust --- examples/prompting/Few_shot_prompting.ipynb | 224 ++++++++++++++++++++ 1 file changed, 224 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..ac4f17b3a --- /dev/null +++ b/examples/prompting/Few_shot_prompting.ipynb @@ -0,0 +1,224 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "Ucyrp0e4qwEr" + }, + "source": [ + "##### 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", + "
" + ] + }, + { + "cell_type": "markdown", + "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", + "execution_count": null, + "metadata": { + "id": "Ne-3gnXqR0hI" + }, + "outputs": [], + "source": [ + "!pip install -U -q google-generativeai" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "EconMHePQHGw" + }, + "outputs": [], + "source": [ + "import google.generativeai as genai" + ] + }, + { + "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." + ] + }, + { + "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)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "la7vu95MX8PN" + }, + "source": [ + "## Examples\n", + "\n", + "Use Gemini 1.5 Flash as your model to run through the following examples." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "BVEceozQX9Mw" + }, + "outputs": [], + "source": [ + "model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest')" + ] + }, + { + "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", + "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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "1bKVqbfLoLi8" + }, + "outputs": [ + { + "data": { + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + }, + "text/plain": [ + "'```json\\n{\"Austin\": \"United States\", \"Lisbon\": \"Portugal\"}\\n``` \\n'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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, generation_config={'response_mime_type':'application/json'}).text" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UGVE-b_8rEUB" + }, + "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." + ] + } + ], + "metadata": { + "colab": { + "name": "Few_shot_prompting.ipynb", + "toc_visible": true + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}