Skip to content

Commit 8eeae80

Browse files
docs(genai):Add anthropic batch predict samples (#13228)
* docs(genai):Add anthropic batch predict samples * updating module names in testing file * updating import statements * updating file names * moving model_garden folder and updating code owners * update region tags * changing folder structure * removing delete files * updating lib
1 parent 1119a15 commit 8eeae80

File tree

7 files changed

+251
-0
lines changed

7 files changed

+251
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
/iam/cloud-client/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
2828
/kms/**/** @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
2929
/media_cdn/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
30+
/model_garden/**/* @GoogleCloudPlatform/generative-ai-devrel @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
3031
/parametermanager/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/cloud-secrets-team @GoogleCloudPlatform/cloud-parameters-team
3132
/privateca/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
3233
/recaptcha_enterprise/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/recaptcha-customer-obsession-reviewers @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 aiplatform_anthropic_batchpredict_with_bq]
18+
import time
19+
20+
from google import genai
21+
from google.genai.types import CreateBatchJobConfig, JobState, HttpOptions
22+
23+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
24+
25+
# TODO(developer): Update and un-comment below line
26+
# output_uri = f"bq://your-project.your_dataset.your_table"
27+
28+
job = client.batches.create(
29+
# anthropic BQ predicitions only work in us-east5
30+
# More about Anthropic model: https://console.cloud.google.com/vertex-ai/publishers/anthropic/model-garden/claude-3-5-haiku
31+
model="publishers/anthropic/models/claude-3-5-haiku",
32+
# The source dataset needs to be created specifically in us-east5
33+
src="bq://python-docs-samples-tests.anthropic_bq_sample.test_data",
34+
config=CreateBatchJobConfig(dest=output_uri),
35+
)
36+
print(f"Job name: {job.name}")
37+
print(f"Job state: {job.state}")
38+
# Example response:
39+
# Job name: projects/%PROJECT_ID%/locations/us-central1/batchPredictionJobs/9876453210000000000
40+
# Job state: JOB_STATE_PENDING
41+
42+
# See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.BatchJob
43+
completed_states = {
44+
JobState.JOB_STATE_SUCCEEDED,
45+
JobState.JOB_STATE_FAILED,
46+
JobState.JOB_STATE_CANCELLED,
47+
JobState.JOB_STATE_PAUSED,
48+
}
49+
50+
while job.state not in completed_states:
51+
time.sleep(30)
52+
job = client.batches.get(name=job.name)
53+
print(f"Job state: {job.state}")
54+
# Example response:
55+
# Job state: JOB_STATE_PENDING
56+
# Job state: JOB_STATE_RUNNING
57+
# Job state: JOB_STATE_RUNNING
58+
# ...
59+
# Job state: JOB_STATE_SUCCEEDED
60+
61+
# [END aiplatform_anthropic_batchpredict_with_bq]
62+
return job.state
63+
64+
65+
if __name__ == "__main__":
66+
# The dataset of the output uri needs to be created specifically in us-east5
67+
generate_content(output_uri="bq://your-project.your_dataset.your_table")
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 aiplatform_anthropic_batchpredict_with_gcs]
18+
import time
19+
20+
from google import genai
21+
from google.genai.types import CreateBatchJobConfig, JobState, HttpOptions
22+
23+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
24+
# TODO(developer): Update and un-comment below line
25+
# output_uri = "gs://your-bucket/your-prefix"
26+
27+
# See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.batches.Batches.create
28+
job = client.batches.create(
29+
# More about Anthropic model: https://console.cloud.google.com/vertex-ai/publishers/anthropic/model-garden/claude-3-5-haiku
30+
model="publishers/anthropic/models/claude-3-5-haiku",
31+
# Source link: https://storage.cloud.google.com/cloud-samples-data/batch/anthropic-test-data-gcs.jsonl
32+
src="gs://cloud-samples-data/anthropic-test-data-gcs.jsonl",
33+
config=CreateBatchJobConfig(dest=output_uri),
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+
JobState.JOB_STATE_SUCCEEDED,
44+
JobState.JOB_STATE_FAILED,
45+
JobState.JOB_STATE_CANCELLED,
46+
JobState.JOB_STATE_PAUSED,
47+
}
48+
49+
while job.state not in completed_states:
50+
time.sleep(30)
51+
job = client.batches.get(name=job.name)
52+
print(f"Job state: {job.state}")
53+
# Example response:
54+
# Job state: JOB_STATE_PENDING
55+
# Job state: JOB_STATE_RUNNING
56+
# Job state: JOB_STATE_RUNNING
57+
# ...
58+
# Job state: JOB_STATE_SUCCEEDED
59+
60+
# [END aiplatform_anthropic_batchpredict_with_gcs]
61+
return job.state
62+
63+
64+
if __name__ == "__main__":
65+
generate_content(output_uri="gs://your-bucket/your-prefix")
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2021 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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.12"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}
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==1.5.0
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
# Using Google Cloud Vertex AI to test the code samples.
17+
#
18+
19+
from datetime import datetime as dt
20+
21+
import os
22+
23+
from google.cloud import bigquery, storage
24+
from google.genai.types import JobState
25+
26+
import pytest
27+
28+
import anthropic_batchpredict_with_bq
29+
import anthropic_batchpredict_with_gcs
30+
31+
32+
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
33+
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-east5"
34+
# The project name is included in the CICD pipeline
35+
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"
36+
BQ_OUTPUT_DATASET = f"{os.environ['GOOGLE_CLOUD_PROJECT']}.anthropic_bq_sample"
37+
GCS_OUTPUT_BUCKET = "python-docs-samples-tests"
38+
39+
40+
@pytest.fixture(scope="session")
41+
def bq_output_uri() -> str:
42+
table_name = f"text_output_{dt.now().strftime('%Y_%m_%d_T%H_%M_%S')}"
43+
table_uri = f"{BQ_OUTPUT_DATASET}.{table_name}"
44+
45+
yield f"bq://{table_uri}"
46+
47+
bq_client = bigquery.Client()
48+
bq_client.delete_table(table_uri, not_found_ok=True)
49+
50+
51+
@pytest.fixture(scope="session")
52+
def gcs_output_uri() -> str:
53+
prefix = f"text_output/{dt.now()}"
54+
55+
yield f"gs://{GCS_OUTPUT_BUCKET}/{prefix}"
56+
57+
storage_client = storage.Client()
58+
bucket = storage_client.get_bucket(GCS_OUTPUT_BUCKET)
59+
blobs = bucket.list_blobs(prefix=prefix)
60+
for blob in blobs:
61+
blob.delete()
62+
63+
64+
def test_batch_prediction_with_bq(bq_output_uri: str) -> None:
65+
response = anthropic_batchpredict_with_bq.generate_content(output_uri=bq_output_uri)
66+
assert response == JobState.JOB_STATE_SUCCEEDED
67+
68+
69+
def test_batch_prediction_with_gcs(gcs_output_uri: str) -> None:
70+
response = anthropic_batchpredict_with_gcs.generate_content(output_uri=gcs_output_uri)
71+
assert response == JobState.JOB_STATE_SUCCEEDED

0 commit comments

Comments
 (0)