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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "KLHiTPXNTf2a"
},
"source": [
"##### Copyright 2025 Google LLC."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"cellView": "form",
"id": "oTuT5CsaTigz"
},
"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": "HUP7eCxvtblC"
},
"source": [
"# Gemini API: Entity extraction\n",
"\n",
"Use Gemini API to speed up some of your tasks, such as searching through text to extract needed information. Entity extraction with a Gemini model is a simple query, and you can ask it to retrieve its answer in the form that you prefer.\n",
"\n",
"This notebook shows how to extract entities into a list."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b04e5041d418"
},
"source": [
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Entity_Extraction.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" height=30/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lu51_vs315hS"
},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "esUtyazO2TU5"
},
"outputs": [],
"source": [
"%pip install -U -q \"google-genai>=1.0.0\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6Z8xjVQX2fuB"
},
"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": 3,
"metadata": {
"id": "uwbcbjun2gb3"
},
"outputs": [],
"source": [
"from google import genai\n",
"from google.colab import userdata\n",
"\n",
"GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n",
"client = genai.Client(api_key=GOOGLE_API_KEY)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MdqFCJr7KG_d"
},
"source": [
"# Select the model\n",
"\n",
"Additionally, select the model you want to use from the available options below:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "5oc1zmBMKFdQ"
},
"outputs": [],
"source": [
"MODEL_ID = \"gemini-3-flash-preview\" # @param [\"gemini-2.5-flash-lite\", \"gemini-2.5-flash\", \"gemini-2.5-pro\", \"gemini-2.5-flash-preview\", \"gemini-3-pro-preview\"] {\"allow-input\":true, isTemplate: true}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ky3Njb3t0riS"
},
"source": [
"# Examples"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "D14cShRh4DNl"
},
"source": [
"### Extracting few entities at once\n",
"\n",
"This block of text is about possible ways to travel from the airport to the Colosseum. \n",
"\n",
"Let's extract all street names and proposed forms of transportation from it."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "Qc0w1Ylb7HQB"
},
"outputs": [],
"source": [
"directions = \"\"\"\n",
" To reach the Colosseum from Rome's Fiumicino Airport (FCO),\n",
" your options are diverse. Take the Leonardo Express train from FCO\n",
" to Termini Station, then hop on metro line A towards Battistini and\n",
" alight at Colosseo station.\n",
" Alternatively, hop on a direct bus, like the Terravision shuttle, from\n",
" FCO to Termini, then walk a short distance to the Colosseum on\n",
" Via dei Fori Imperiali.\n",
" If you prefer a taxi, simply hail one at the airport and ask to be taken\n",
" to the Colosseum. The taxi will likely take you through Via del Corso and\n",
" Via dei Fori Imperiali.\n",
" A private transfer service offers a direct ride from FCO to the Colosseum,\n",
" bypassing the hustle of public transport.\n",
" If you're feeling adventurous, consider taking the train from\n",
" FCO to Ostiense station, then walking through the charming\n",
" Trastevere neighborhood, crossing Ponte Palatino to reach the Colosseum,\n",
" passing by the Tiber River and Via della Lungara.\n",
" Remember to validate your tickets on the metro and buses,\n",
" and be mindful of pickpockets, especially in crowded areas.\n",
" No matter which route you choose, you're sure to be awed by the\n",
" grandeur of the Colosseum.\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KelZNWwSytWf"
},
"source": [
"You will use Gemini Flash model for fast responses."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "bxPfNhRi-JsW"
},
"outputs": [
{
"data": {
"text/markdown": [
"```json\n",
"{\n",
" \"Street\": [\n",
" \"Via dei Fori Imperiali\",\n",
" \"Via del Corso\",\n",
" \"Via dei Fori Imperiali\",\n",
" \"Via della Lungara\"\n",
" ],\n",
" \"Transport\": [\n",
" \"train\",\n",
" \"metro\",\n",
" \"bus\",\n",
" \"shuttle\",\n",
" \"taxi\",\n",
" \"private transfer service\",\n",
" \"public transport\",\n",
" \"train\"\n",
" ]\n",
"}\n",
"```"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import Markdown\n",
"\n",
"directions_prompt = f\"\"\"\n",
" From the given text, extract the following entities and return a list of them.\n",
" Entities to extract: street name, form of transport.\n",
" Text: {directions}\n",
" Street = []\n",
" Transport = []\n",
"\"\"\"\n",
"\n",
"response = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=directions_prompt\n",
")\n",
"\n",
"Markdown(response.text)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VYqOM7P76lua"
},
"source": [
"You can modify the form of the answer for your extracted entities even more:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "DZdGgTsN5s6z"
},
"outputs": [
{
"data": {
"text/markdown": [
"Here's the extracted information:\n",
"\n",
"Street = ['Via dei Fori Imperiali', 'Via del Corso', 'Via dei Fori Imperiali', 'Via della Lungara']\n",
"Transport = ['train', 'metro', 'bus', 'shuttle', 'taxi', 'transfer service']\n"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"directions_list_prompt = f\"\"\"\n",
" From the given text, extract the following entities and\n",
" return a list of them.\n",
" Entities to extract: street name, form of transport.\n",
" Text: {directions}\n",
" Return your answer as two lists:\n",
" Street = [street names]\n",
" Transport = [forms of transport]\n",
"\"\"\"\n",
"\n",
"response = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=directions_list_prompt\n",
")\n",
"\n",
"Markdown(response.text)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4d7Om_vjuffj"
},
"source": [
"### Numbers\n",
"\n",
"Try entity extraction of phone numbers"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "tkAi37jQoJFs"
},
"outputs": [],
"source": [
"customer_service_email = \"\"\"\n",
" Hello,\n",
" Thank you for reaching out to our customer support team regarding your\n",
" recent purchase of our premium subscription service.\n",
" Your activation code has been sent to +87 668 098 344\n",
" Additionally, if you require immediate assistance, feel free to contact us\n",
" directly at +1 (800) 555-1234.\n",
" Our team is available Monday through Friday from 9:00 AM to 5:00 PM PST.\n",
" For after-hours support, please call our\n",
" dedicated emergency line at +87 455 555 678.\n",
" Thanks for your business and look forward to resolving any issues\n",
" you may encounter promptly.\n",
" Thank you.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "J_cHeX-wwmeN"
},
"outputs": [
{
"data": {
"text/markdown": [
"```json\n",
"[\n",
" \"+87 668 098 344\",\n",
" \"+1 (800) 555-1234\",\n",
" \"+87 455 555 678\"\n",
"]\n",
"```"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"phone_prompt = f\"\"\"\n",
" From the given text, extract the following entities and return a list of them.\n",
" Entities to extract: phone numbers.\n",
" Text: {customer_service_email}\n",
" Return your answer in a list:\n",
"\"\"\"\n",
"\n",
"response = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=phone_prompt\n",
")\n",
"\n",
"Markdown(response.text)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "o_tNxJhQur-x"
},
"source": [
"### URLs\n",
"\n",
"\n",
"Try entity extraction of URLs and get response as a clickable link."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "J1Ufx9qkxPfJ"
},
"outputs": [],
"source": [
"url_text = \"\"\"\n",
" Gemini API billing FAQs\n",
"\n",
" This page provides answers to frequently asked questions about billing\n",
" for the Gemini API. For pricing information, see the pricing page\n",
" https://ai.google.dev/pricing.\n",
" For legal terms, see the terms of service\n",
" https://ai.google.dev/gemini-api/terms#paid-services.\n",
"\n",
" What am I billed for?\n",
" Gemini API pricing is based on total token count, with different prices\n",
" for input tokens and output tokens. For pricing information,\n",
" see the pricing page https://ai.google.dev/pricing.\n",
"\n",
" Where can I view my quota?\n",
" You can view your quota and system limits in the Google Cloud console\n",
" https://console.cloud.google.com/apis/api/generativelanguage.googleapis.com/quotas.\n",
"\n",
" Is GetTokens billed?\n",
" Requests to the GetTokens API are not billed,\n",
" and they don't count against inference quota.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"id": "eaFWpN1IyOLW"
},
"outputs": [
{
"data": {
"text/markdown": [
"```\n",
"- https://ai.google.dev/pricing\n",
"- https://ai.google.dev/gemini-api/terms#paid-services\n",
"- https://console.cloud.google.com/apis/api/generativelanguage.googleapis.com/quotas\n",
"```"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"url_prompt = f\"\"\"\n",
" From the given text, extract the following entities and return a list of them.\n",
" Entities to extract: URLs.\n",
" Text: {url_text}\n",
" Do not duplicate entities.\n",
" Return your answer in a markdown format:\n",
"\"\"\"\n",
"\n",
"response = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=url_prompt\n",
")\n",
"\n",
"Markdown(response.text)"
]
}
],
"metadata": {
"colab": {
"name": "Entity_Extraction.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}