Skip to content

Commit 93416ed

Browse files
authored
docs: add code snippets for llm text generatiion (#669)
* docs: add code snippets for llm text generatiion
1 parent 57ccabc commit 93416ed

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def test_llm_text_generation() -> None:
17+
# Determine project id, in this case prefer the one set in the environment
18+
# variable GOOGLE_CLOUD_PROJECT (if any)
19+
import os
20+
21+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "bigframes-dev")
22+
LOCATION = "US"
23+
24+
# [START bigquery_dataframes_generate_text_tutorial_create_remote_model]
25+
import bigframes
26+
from bigframes.ml.llm import PaLM2TextGenerator
27+
28+
bigframes.options.bigquery.project = PROJECT_ID
29+
bigframes.options.bigquery.location = LOCATION
30+
31+
model = PaLM2TextGenerator()
32+
# [END bigquery_dataframes_generate_text_tutorial_create_remote_model]
33+
assert model is not None
34+
35+
# [START bigquery_dataframes_generate_text_tutorial_perform_keyword_extraction]
36+
import bigframes.pandas as bpd
37+
38+
df = bpd.read_gbq("bigquery-public-data.imdb.reviews", max_results=5)
39+
df_prompt_prefix = "Extract the key words from the text below: "
40+
df_prompt = df_prompt_prefix + df["review"]
41+
42+
# Predict using the model
43+
df_pred = model.predict(df_prompt, temperature=0.2, max_output_tokens=100)
44+
df_pred.peek(5)
45+
# [END bigquery_dataframes_generate_text_tutorial_perform_keyword_extraction]
46+
# peek() is used to show a preview of the results. If the output
47+
# of this sample changes, also update the screenshot for the associated
48+
# tutorial on cloud.google.com.
49+
assert df_pred["ml_generate_text_llm_result"] is not None
50+
assert df_pred["ml_generate_text_llm_result"].iloc[0] is not None
51+
52+
# [START bigquery_dataframes_generate_text_tutorial_perform_sentiment_analysis]
53+
import bigframes.pandas as bpd
54+
55+
df = bpd.read_gbq("bigquery-public-data.imdb.reviews", max_results=5)
56+
df_prompt_prefix = "perform sentiment analysis on the following text, return one the following categories: positive, negative: "
57+
df_prompt = df_prompt_prefix + df["review"]
58+
59+
# Predict using the model
60+
df_pred = model.predict(df_prompt, temperature=0.2, max_output_tokens=100)
61+
df_pred.peek(5)
62+
# [END bigquery_dataframes_generate_text_tutorial_perform_sentiment_analysis]
63+
# peek() is used to show a preview of the results. If the output
64+
# of this sample changes, also update the screenshot for the associated
65+
# tutorial on cloud.google.com.
66+
67+
assert df_pred["ml_generate_text_llm_result"] is not None
68+
assert df_pred["ml_generate_text_llm_result"].iloc[0] is not None

0 commit comments

Comments
 (0)