Skip to content

Commit 208d5d7

Browse files
authored
Update Entity_extraction_JSON notebook to use genai module (#509)
Refers #446 * Updated module google-generativeai to use google-genai>=1.0.0 * Refactored imports Signed-off-by: Faakhir30 <[email protected]>
1 parent 57d58ba commit 208d5d7

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

examples/json_capabilities/Entity_Extraction_JSON.ipynb

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,13 @@
6464
},
6565
{
6666
"cell_type": "code",
67-
"execution_count": 8,
67+
"execution_count": 5,
6868
"metadata": {
6969
"id": "Ne-3gnXqR0hI"
7070
},
7171
"outputs": [],
7272
"source": [
73-
"!pip install -U -q \"google-generativeai>=0.7.2\""
74-
]
75-
},
76-
{
77-
"cell_type": "code",
78-
"execution_count": 9,
79-
"metadata": {
80-
"id": "EconMHePQHGw"
81-
},
82-
"outputs": [],
83-
"source": [
84-
"import google.generativeai as genai\n",
85-
"\n",
86-
"import json\n",
87-
"from enum import Enum\n",
88-
"from typing_extensions import TypedDict # in python 3.12 replace typing_extensions with typing"
73+
"%pip install -U -q \"google-genai>=1.0.0\""
8974
]
9075
},
9176
{
@@ -101,16 +86,17 @@
10186
},
10287
{
10388
"cell_type": "code",
104-
"execution_count": 10,
89+
"execution_count": 6,
10590
"metadata": {
10691
"id": "v-JZzORUpVR2"
10792
},
10893
"outputs": [],
10994
"source": [
11095
"from google.colab import userdata\n",
111-
"GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n",
96+
"from google import genai\n",
11297
"\n",
113-
"genai.configure(api_key=GOOGLE_API_KEY)"
98+
"GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n",
99+
"client = genai.Client(api_key=GOOGLE_API_KEY)"
114100
]
115101
},
116102
{
@@ -124,12 +110,16 @@
124110
},
125111
{
126112
"cell_type": "code",
127-
"execution_count": 20,
113+
"execution_count": 7,
128114
"metadata": {
129115
"id": "QGdJnd0AOKbu"
130116
},
131117
"outputs": [],
132118
"source": [
119+
"from enum import Enum\n",
120+
"from typing_extensions import TypedDict # in python 3.12 replace typing_extensions with typing\n",
121+
"from google.genai import types\n",
122+
"\n",
133123
"entity_recognition_text = \"John Johnson, the CEO of the Oil Inc. and Coal Inc. companies, has unveiled plans to build a new factory in Houston, Texas.\"\n",
134124
"prompt = f\"\"\"\n",
135125
"Generate list of entities in text based on the following Python class structure:\n",
@@ -148,13 +138,21 @@
148138
" entities: list[Entity]\n",
149139
"\n",
150140
"{entity_recognition_text}\"\"\"\n",
151-
"model = genai.GenerativeModel(model_name='gemini-2.0-flash', generation_config={\"temperature\": 0})\n",
152-
"response = model.generate_content(prompt, generation_config={\"response_mime_type\": \"application/json\"})"
141+
"MODEL_ID = \"gemini-2.0-flash\" # @param [\"gemini-2.0-flash-lite\",\"gemini-2.0-flash\",\"gemini-2.0-pro-exp-02-05\"] {\"allow-input\":true, isTemplate: true}\n",
142+
"\n",
143+
"response = client.models.generate_content(\n",
144+
" model=MODEL_ID,\n",
145+
" contents=prompt,\n",
146+
" config=types.GenerateContentConfig(\n",
147+
" temperature=0,\n",
148+
" response_mime_type=\"application/json\"\n",
149+
" )\n",
150+
" )"
153151
]
154152
},
155153
{
156154
"cell_type": "code",
157-
"execution_count": 21,
155+
"execution_count": 8,
158156
"metadata": {
159157
"id": "d5tOgde6ONo3"
160158
},
@@ -191,6 +189,8 @@
191189
}
192190
],
193191
"source": [
192+
"import json\n",
193+
"\n",
194194
"print(json.dumps(json.loads(response.text), indent=4))"
195195
]
196196
},

0 commit comments

Comments
 (0)