Skip to content

Commit

Permalink
Add example for few shot prompting (#163)
Browse files Browse the repository at this point in the history
* Add example for few shot prompting

* update few shot prompting example

* Update examples/prompting/Few_shot_prompting.ipynb

Co-authored-by: Mark Daoust <markdaoust@google.com>

* Update examples/prompting/Few_shot_prompting.ipynb

Co-authored-by: Mark Daoust <markdaoust@google.com>

---------

Co-authored-by: Mark Daoust <markdaoust@google.com>
  • Loading branch information
shilpakancharla and MarkDaoust authored May 30, 2024
1 parent b67bf46 commit bdcca35
Showing 1 changed file with 224 additions and 0 deletions.
224 changes: 224 additions & 0 deletions examples/prompting/Few_shot_prompting.ipynb
Original file line number Diff line number Diff line change
@@ -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": [
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/prompting/Few_shot_prompting.ipynb\"><img src = \"https://www.tensorflow.org/images/colab_logo_32px.png\"/>Run in Google Colab</a>\n",
" </td>\n",
"</table>"
]
},
{
"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
}

0 comments on commit bdcca35

Please sign in to comment.