|
| 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") |
0 commit comments