Skip to content

Commit 9596b66

Browse files
authored
docs: add Gemini 2.0 text gen sample notebook (#1211)
* feat: add Gemini 2.0 preview text model support * update docs * docs: add Gemini 2.0 text gen sample notebook
1 parent 93ce2ac commit 9596b66

File tree

2 files changed

+378
-0
lines changed

2 files changed

+378
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,377 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# Copyright 2024 Google LLC\n",
10+
"#\n",
11+
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
12+
"# you may not use this file except in compliance with the License.\n",
13+
"# You may obtain a copy of the License at\n",
14+
"#\n",
15+
"# https://www.apache.org/licenses/LICENSE-2.0\n",
16+
"#\n",
17+
"# Unless required by applicable law or agreed to in writing, software\n",
18+
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
19+
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
20+
"# See the License for the specific language governing permissions and\n",
21+
"# limitations under the License."
22+
]
23+
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"# BigFrames Gemini 2.0 Text Generation Simple Example"
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"metadata": {},
34+
"source": [
35+
"Note: This feature is only available in bigframes >= 1.29.0"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"### Import packages"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 3,
48+
"metadata": {},
49+
"outputs": [],
50+
"source": [
51+
"import bigframes.pandas as bpd\n",
52+
"from bigframes.ml import llm"
53+
]
54+
},
55+
{
56+
"cell_type": "markdown",
57+
"metadata": {},
58+
"source": [
59+
"### Create Gemini 2.0 experimental Model with model_name as \"gemini-2.0-flash-exp\""
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": 4,
65+
"metadata": {},
66+
"outputs": [
67+
{
68+
"name": "stderr",
69+
"output_type": "stream",
70+
"text": [
71+
"/usr/local/google/home/garrettwu/src/bigframes/bigframes/ml/llm.py:803: PreviewWarning: Model gemini-2.0-flash-exp is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section of the\n",
72+
" Service Specific Terms(https://cloud.google.com/terms/service-terms#1). Pre-GA products and features are available \"as is\"\n",
73+
" and might have limited support. For more information, see the launch stage descriptions\n",
74+
" (https://cloud.google.com/products#product-launch-stages).\n",
75+
" warnings.warn(\n",
76+
"/usr/local/google/home/garrettwu/src/bigframes/bigframes/pandas/__init__.py:435: DefaultLocationWarning: No explicit location is set, so using location US for the session.\n",
77+
" return global_session.get_global_session()\n"
78+
]
79+
},
80+
{
81+
"data": {
82+
"text/html": [
83+
"Query job f673a2ea-023e-4771-84a2-fb81f808fa1b is DONE. 0 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:f673a2ea-023e-4771-84a2-fb81f808fa1b&page=queryresults\">Open Job</a>"
84+
],
85+
"text/plain": [
86+
"<IPython.core.display.HTML object>"
87+
]
88+
},
89+
"metadata": {},
90+
"output_type": "display_data"
91+
}
92+
],
93+
"source": [
94+
"model = llm.GeminiTextGenerator(model_name=\"gemini-2.0-flash-exp\")"
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"metadata": {},
100+
"source": [
101+
"### Create a simple DataFrame"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": 5,
107+
"metadata": {},
108+
"outputs": [
109+
{
110+
"data": {
111+
"text/html": [
112+
"Query job 2276ea5b-2e08-4ed6-af34-49a7d165d145 is DONE. 0 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:2276ea5b-2e08-4ed6-af34-49a7d165d145&page=queryresults\">Open Job</a>"
113+
],
114+
"text/plain": [
115+
"<IPython.core.display.HTML object>"
116+
]
117+
},
118+
"metadata": {},
119+
"output_type": "display_data"
120+
},
121+
{
122+
"data": {
123+
"text/html": [
124+
"<div>\n",
125+
"<style scoped>\n",
126+
" .dataframe tbody tr th:only-of-type {\n",
127+
" vertical-align: middle;\n",
128+
" }\n",
129+
"\n",
130+
" .dataframe tbody tr th {\n",
131+
" vertical-align: top;\n",
132+
" }\n",
133+
"\n",
134+
" .dataframe thead th {\n",
135+
" text-align: right;\n",
136+
" }\n",
137+
"</style>\n",
138+
"<table border=\"1\" class=\"dataframe\">\n",
139+
" <thead>\n",
140+
" <tr style=\"text-align: right;\">\n",
141+
" <th></th>\n",
142+
" <th>prompt</th>\n",
143+
" </tr>\n",
144+
" </thead>\n",
145+
" <tbody>\n",
146+
" <tr>\n",
147+
" <th>0</th>\n",
148+
" <td>Tell me something about Gemini 2.0.</td>\n",
149+
" </tr>\n",
150+
" </tbody>\n",
151+
"</table>\n",
152+
"<p>1 rows × 1 columns</p>\n",
153+
"</div>[1 rows x 1 columns in total]"
154+
],
155+
"text/plain": [
156+
" prompt\n",
157+
"0 Tell me something about Gemini 2.0.\n",
158+
"\n",
159+
"[1 rows x 1 columns]"
160+
]
161+
},
162+
"execution_count": 5,
163+
"metadata": {},
164+
"output_type": "execute_result"
165+
}
166+
],
167+
"source": [
168+
"df = bpd.DataFrame({\"prompt\": [\"Tell me something about Gemini 2.0.\"]})\n",
169+
"df"
170+
]
171+
},
172+
{
173+
"cell_type": "markdown",
174+
"metadata": {},
175+
"source": [
176+
"### Make predictions"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": 6,
182+
"metadata": {},
183+
"outputs": [
184+
{
185+
"data": {
186+
"text/html": [
187+
"Query job 9ba21e96-6023-491e-8e83-f2e6fa7df0e7 is DONE. 0 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:9ba21e96-6023-491e-8e83-f2e6fa7df0e7&page=queryresults\">Open Job</a>"
188+
],
189+
"text/plain": [
190+
"<IPython.core.display.HTML object>"
191+
]
192+
},
193+
"metadata": {},
194+
"output_type": "display_data"
195+
},
196+
{
197+
"name": "stderr",
198+
"output_type": "stream",
199+
"text": [
200+
"/usr/local/google/home/garrettwu/src/bigframes/bigframes/core/__init__.py:109: PreviewWarning: Interpreting JSON column(s) as StringDtype. This behavior may change in future versions.\n",
201+
" warnings.warn(\n"
202+
]
203+
},
204+
{
205+
"data": {
206+
"text/html": [
207+
"Query job 933d45cc-4bc0-4bdf-b4b8-573da2d58be3 is DONE. 2 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:933d45cc-4bc0-4bdf-b4b8-573da2d58be3&page=queryresults\">Open Job</a>"
208+
],
209+
"text/plain": [
210+
"<IPython.core.display.HTML object>"
211+
]
212+
},
213+
"metadata": {},
214+
"output_type": "display_data"
215+
},
216+
{
217+
"data": {
218+
"text/html": [
219+
"Query job 3dda9bc6-84b1-4f4a-8891-85d25d8848ce is DONE. 4.3 kB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:3dda9bc6-84b1-4f4a-8891-85d25d8848ce&page=queryresults\">Open Job</a>"
220+
],
221+
"text/plain": [
222+
"<IPython.core.display.HTML object>"
223+
]
224+
},
225+
"metadata": {},
226+
"output_type": "display_data"
227+
},
228+
{
229+
"data": {
230+
"text/html": [
231+
"<div>\n",
232+
"<style scoped>\n",
233+
" .dataframe tbody tr th:only-of-type {\n",
234+
" vertical-align: middle;\n",
235+
" }\n",
236+
"\n",
237+
" .dataframe tbody tr th {\n",
238+
" vertical-align: top;\n",
239+
" }\n",
240+
"\n",
241+
" .dataframe thead th {\n",
242+
" text-align: right;\n",
243+
" }\n",
244+
"</style>\n",
245+
"<table border=\"1\" class=\"dataframe\">\n",
246+
" <thead>\n",
247+
" <tr style=\"text-align: right;\">\n",
248+
" <th></th>\n",
249+
" <th>ml_generate_text_llm_result</th>\n",
250+
" <th>ml_generate_text_rai_result</th>\n",
251+
" <th>ml_generate_text_status</th>\n",
252+
" <th>prompt</th>\n",
253+
" </tr>\n",
254+
" </thead>\n",
255+
" <tbody>\n",
256+
" <tr>\n",
257+
" <th>0</th>\n",
258+
" <td>Alright, let's talk about Gemini 2.0! It's a b...</td>\n",
259+
" <td>&lt;NA&gt;</td>\n",
260+
" <td></td>\n",
261+
" <td>Tell me something about Gemini 2.0.</td>\n",
262+
" </tr>\n",
263+
" </tbody>\n",
264+
"</table>\n",
265+
"<p>1 rows × 4 columns</p>\n",
266+
"</div>[1 rows x 4 columns in total]"
267+
],
268+
"text/plain": [
269+
" ml_generate_text_llm_result \\\n",
270+
"0 Alright, let's talk about Gemini 2.0! It's a b... \n",
271+
"\n",
272+
" ml_generate_text_rai_result ml_generate_text_status \\\n",
273+
"0 <NA> \n",
274+
"\n",
275+
" prompt \n",
276+
"0 Tell me something about Gemini 2.0. \n",
277+
"\n",
278+
"[1 rows x 4 columns]"
279+
]
280+
},
281+
"execution_count": 6,
282+
"metadata": {},
283+
"output_type": "execute_result"
284+
}
285+
],
286+
"source": [
287+
"result = model.predict(df)\n",
288+
"result"
289+
]
290+
},
291+
{
292+
"cell_type": "markdown",
293+
"metadata": {},
294+
"source": [
295+
"### Save the model"
296+
]
297+
},
298+
{
299+
"cell_type": "code",
300+
"execution_count": 7,
301+
"metadata": {},
302+
"outputs": [
303+
{
304+
"data": {
305+
"text/html": [
306+
"Copy job 8e68af62-e7ab-475b-99c9-b79e8ba3c40b is DONE. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:8e68af62-e7ab-475b-99c9-b79e8ba3c40b&page=queryresults\">Open Job</a>"
307+
],
308+
"text/plain": [
309+
"<IPython.core.display.HTML object>"
310+
]
311+
},
312+
"metadata": {},
313+
"output_type": "display_data"
314+
},
315+
{
316+
"name": "stderr",
317+
"output_type": "stream",
318+
"text": [
319+
"/usr/local/google/home/garrettwu/src/bigframes/bigframes/ml/llm.py:803: PreviewWarning: Model gemini-2.0-flash-exp is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section of the\n",
320+
" Service Specific Terms(https://cloud.google.com/terms/service-terms#1). Pre-GA products and features are available \"as is\"\n",
321+
" and might have limited support. For more information, see the launch stage descriptions\n",
322+
" (https://cloud.google.com/products#product-launch-stages).\n",
323+
" warnings.warn(\n"
324+
]
325+
},
326+
{
327+
"data": {
328+
"text/html": [
329+
"Query job cae7f929-d8cb-4819-a644-ac832cdc0912 is DONE. 0 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:cae7f929-d8cb-4819-a644-ac832cdc0912&page=queryresults\">Open Job</a>"
330+
],
331+
"text/plain": [
332+
"<IPython.core.display.HTML object>"
333+
]
334+
},
335+
"metadata": {},
336+
"output_type": "display_data"
337+
},
338+
{
339+
"data": {
340+
"text/plain": [
341+
"GeminiTextGenerator(connection_name='bigframes-dev.us.bigframes-rf-connection',\n",
342+
" model_name='gemini-2.0-flash-exp',\n",
343+
" session=<bigframes.session.Session object at 0x7f8210630370>)"
344+
]
345+
},
346+
"execution_count": 7,
347+
"metadata": {},
348+
"output_type": "execute_result"
349+
}
350+
],
351+
"source": [
352+
"model.to_gbq(\"bigframes-dev.garrettwu.gemini_2_flash\", replace=True)"
353+
]
354+
}
355+
],
356+
"metadata": {
357+
"kernelspec": {
358+
"display_name": "venv",
359+
"language": "python",
360+
"name": "python3"
361+
},
362+
"language_info": {
363+
"codemirror_mode": {
364+
"name": "ipython",
365+
"version": 3
366+
},
367+
"file_extension": ".py",
368+
"mimetype": "text/x-python",
369+
"name": "python",
370+
"nbconvert_exporter": "python",
371+
"pygments_lexer": "ipython3",
372+
"version": "3.10.14"
373+
}
374+
},
375+
"nbformat": 4,
376+
"nbformat_minor": 2
377+
}

noxfile.py

+1
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ def notebook(session: nox.Session):
749749
# bq_dataframes_llm_code_generation creates a bucket in the sample.
750750
"notebooks/generative_ai/bq_dataframes_llm_code_generation.ipynb", # Needs BUCKET_URI.
751751
"notebooks/generative_ai/sentiment_analysis.ipynb", # Too slow
752+
"notebooks/generative_ai/bq_dataframes_llm_gemini_2.ipynb", # Gemini 2.0 backend hasn't ready in prod.
752753
# TODO(b/366290533): to protect BQML quota
753754
"notebooks/generative_ai/bq_dataframes_llm_claude3_museum_art.ipynb",
754755
"notebooks/vertex_sdk/sdk2_bigframes_pytorch.ipynb", # Needs BUCKET_URI.

0 commit comments

Comments
 (0)