1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Ucyrp0e4qwEr"
},
"source": [
"##### Copyright 2025 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": [
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/prompting/Few_shot_prompting.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" height=30/></a>"
]
},
{
"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-genai>=1.0.0\""
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "EconMHePQHGw"
},
"outputs": [],
"source": [
"from google import genai\n",
"from google.genai import types"
]
},
{
"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",
"client=genai.Client(api_key=GOOGLE_API_KEY)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "la7vu95MX8PN"
},
"source": [
"## Examples\n",
"\n",
"Use Gemini Flash as your model to run through the following examples."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "BVEceozQX9Mw"
},
"outputs": [],
"source": [
"MODEL_ID=\"gemini-2.5-flash\" # @param [\"gemini-2.5-flash-lite\",\"gemini-2.5-flash\",\"gemini-2.5-pro\",\"gemini-3-pro-preview\"] {\"allow-input\":true, isTemplate: true}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "i5VGZpBjmpL3"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Whale > Monkey > Goldfish\n",
"\n"
]
}
],
"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",
"\"\"\"\n",
"\n",
"respose=client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=prompt,\n",
")\n",
"\n",
"print(respose.text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "1bKVqbfLoLi8"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"Austin\": \"USA\", \"Lisbon\": \"Portugal\"}\n"
]
}
],
"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",
"\"\"\"\n",
"\n",
"respose=client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=prompt,\n",
" config=types.GenerateContentConfig(\n",
" response_mime_type= 'application/json',\n",
" ),\n",
")\n",
"\n",
"print(respose.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
}