Skip to content

Commit 27454d8

Browse files
Valeriy-Burlakacode-review-assist[bot]
andauthored
feat(genai): Add new samples for batch predict (#13120)
* feat: batch predict with GCS * feat: batch predict with BQ * update comments * bump the sdk version * Update genai/batch_prediction/submit_with_gcs.py Co-authored-by: code-review-assist[bot] <182814678+code-review-assist[bot]@users.noreply.github.com> * fix suggestion --------- Co-authored-by: code-review-assist[bot] <182814678+code-review-assist[bot]@users.noreply.github.com>
1 parent 8ab5755 commit 27454d8

File tree

6 files changed

+202
-1
lines changed

6 files changed

+202
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
google-api-core==2.24.0
2+
google-cloud-bigquery==3.29.0
3+
google-cloud-storage==2.19.0
4+
pytest==8.2.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-genai==0.8.0
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2025 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+
# https://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 generate_content(output_uri: str) -> str:
17+
# [START googlegenaisdk_batchpredict_with_bq]
18+
import time
19+
20+
from google import genai
21+
22+
client = genai.Client()
23+
# TODO(developer): Update and un-comment below line
24+
# output_uri = f"bq://your-project.your_dataset.your_table"
25+
26+
job = client.batches.create(
27+
model="gemini-1.5-pro-002",
28+
src="bq://storage-samples.generative_ai.batch_requests_for_multimodal_input",
29+
config={
30+
"dest": output_uri
31+
}
32+
)
33+
print(f"Job name: {job.name}")
34+
print(f"Job state: {job.state}")
35+
# Example response:
36+
# Job name: projects/%PROJECT_ID%/locations/us-central1/batchPredictionJobs/9876453210000000000
37+
# Job state: JOB_STATE_PENDING
38+
39+
# See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.BatchJob
40+
completed_states = [
41+
"JOB_STATE_SUCCEEDED",
42+
"JOB_STATE_FAILED",
43+
"JOB_STATE_CANCELLED",
44+
"JOB_STATE_PAUSED",
45+
]
46+
while job.state not in completed_states:
47+
time.sleep(30)
48+
job = client.batches.get(name=job.name)
49+
print(f"Job state: {job.state}")
50+
# Example response:
51+
# Job state: JOB_STATE_PENDING
52+
# Job state: JOB_STATE_RUNNING
53+
# Job state: JOB_STATE_RUNNING
54+
# ...
55+
# Job state: JOB_STATE_SUCCEEDED
56+
57+
# [END googlegenaisdk_batchpredict_with_bq]
58+
return job.state
59+
60+
61+
if __name__ == "__main__":
62+
generate_content(
63+
output_uri="bq://your-project.your_dataset.your_table"
64+
)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2025 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+
# https://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 generate_content(output_uri: str) -> str:
17+
# [START googlegenaisdk_batchpredict_with_gcs]
18+
import time
19+
20+
from google import genai
21+
22+
client = genai.Client()
23+
# TODO(developer): Update and un-comment below line
24+
# output_uri = "gs://your-bucket/your-prefix"
25+
26+
# See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.batches.Batches.create
27+
job = client.batches.create(
28+
model="gemini-1.5-pro-002",
29+
# Source link: https://storage.cloud.google.com/cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl
30+
src="gs://cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl",
31+
config={
32+
"dest": output_uri
33+
}
34+
)
35+
print(f"Job name: {job.name}")
36+
print(f"Job state: {job.state}")
37+
# Example response:
38+
# Job name: projects/%PROJECT_ID%/locations/us-central1/batchPredictionJobs/9876453210000000000
39+
# Job state: JOB_STATE_PENDING
40+
41+
# See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.BatchJob
42+
completed_states = [
43+
"JOB_STATE_SUCCEEDED",
44+
"JOB_STATE_FAILED",
45+
"JOB_STATE_CANCELLED",
46+
"JOB_STATE_PAUSED",
47+
]
48+
while job.state not in completed_states:
49+
time.sleep(30)
50+
job = client.batches.get(name=job.name)
51+
print(f"Job state: {job.state}")
52+
# Example response:
53+
# Job state: JOB_STATE_PENDING
54+
# Job state: JOB_STATE_RUNNING
55+
# Job state: JOB_STATE_RUNNING
56+
# ...
57+
# Job state: JOB_STATE_SUCCEEDED
58+
59+
# [END googlegenaisdk_batchpredict_with_gcs]
60+
return job.state
61+
62+
63+
if __name__ == "__main__":
64+
generate_content(
65+
output_uri="gs://your-bucket/your-prefix"
66+
)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2025 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+
# https://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+
from datetime import datetime as dt
16+
import os
17+
18+
from google.cloud import bigquery, storage
19+
from google.genai.types import JobState
20+
21+
import pytest
22+
23+
import submit_with_bq
24+
import submit_with_gcs
25+
26+
27+
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
28+
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
29+
# The project name is included in the CICD pipeline
30+
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"
31+
BQ_OUTPUT_DATASET = f"{os.environ['GOOGLE_CLOUD_PROJECT']}.gen_ai_batch_prediction"
32+
GCS_OUTPUT_BUCKET = "python-docs-samples-tests"
33+
34+
35+
@pytest.fixture(scope="session")
36+
def bq_output_uri():
37+
table_name = f"text_output_{dt.now().strftime('%Y_%m_%d_T%H_%M_%S')}"
38+
table_uri = f"{BQ_OUTPUT_DATASET}.{table_name}"
39+
40+
yield f"bq://{table_uri}"
41+
42+
bq_client = bigquery.Client()
43+
bq_client.delete_table(table_uri, not_found_ok=True)
44+
45+
46+
@pytest.fixture(scope="session")
47+
def gcs_output_uri():
48+
prefix = f"text_output/{dt.now()}"
49+
50+
yield f"gs://{GCS_OUTPUT_BUCKET}/{prefix}"
51+
52+
storage_client = storage.Client()
53+
bucket = storage_client.get_bucket(GCS_OUTPUT_BUCKET)
54+
blobs = bucket.list_blobs(prefix=prefix)
55+
for blob in blobs:
56+
blob.delete()
57+
58+
59+
def test_batch_prediction_with_bq(bq_output_uri) -> None:
60+
response = submit_with_bq.generate_content(output_uri=bq_output_uri)
61+
assert response == JobState.JOB_STATE_SUCCEEDED
62+
63+
64+
def test_batch_prediction_with_gcs(gcs_output_uri) -> None:
65+
response = submit_with_gcs.generate_content(output_uri=gcs_output_uri)
66+
assert response == JobState.JOB_STATE_SUCCEEDED

genai/controlled_generation/ctrlgen_with_class_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def generate_content() -> str:
1717
# [START googlegenaisdk_ctrlgen_with_class_schema]
1818
from google import genai
19-
from pydantic import BaseModel, TypeAdapter
19+
from pydantic import BaseModel
2020

2121
class Recipe(BaseModel):
2222
recipe_name: str

0 commit comments

Comments
 (0)