Skip to content

Commit 1d4f38a

Browse files
authored
feat(genai): Add Provisioned Throughput samples for Gen AI SDK and Vertex AI SDK (#13192)
* docs(genai): Add Provisioned Throughput sample for Gen AI SDK * Add Provisioned Throughput sample for Vertex AI SDK * Update based on comments
1 parent aa24c20 commit 1d4f38a

File tree

10 files changed

+250
-0
lines changed

10 files changed

+250
-0
lines changed
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.13"],
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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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() -> str:
17+
# [START googlegenaisdk_provisionedthroughput_with_txt]
18+
19+
from google import genai
20+
from google.genai.types import HttpOptions
21+
22+
client = genai.Client(
23+
http_options=HttpOptions(
24+
api_version="v1",
25+
headers={
26+
# Options:
27+
# - "dedicated": Use Provisioned Throughput
28+
# - "shared": Use pay-as-you-go
29+
# https://cloud.google.com/vertex-ai/generative-ai/docs/use-provisioned-throughput
30+
"X-Vertex-AI-LLM-Request-Type": "shared"
31+
},
32+
)
33+
)
34+
response = client.models.generate_content(
35+
model="gemini-2.0-flash-001",
36+
contents="How does AI work?",
37+
)
38+
print(response.text)
39+
40+
# Example response:
41+
# Okay, let's break down how AI works. It's a broad field, so I'll focus on the ...
42+
#
43+
# Here's a simplified overview:
44+
# ...
45+
# [END googlegenaisdk_provisionedthroughput_with_txt]
46+
return response.text
47+
48+
49+
if __name__ == "__main__":
50+
generate_content()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-api-core==2.24.0
2+
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.3.0
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
import os
20+
21+
import provisionedthroughput_with_txt
22+
23+
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
24+
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
25+
# The project name is included in the CICD pipeline
26+
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"
27+
28+
29+
def test_provisionedthroughput_with_txt() -> None:
30+
response = provisionedthroughput_with_txt.generate_content()
31+
assert response
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.13"],
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def generate_content() -> str:
20+
# [START generativeaionvertexai_provisioned_throughput_with_txt]
21+
import vertexai
22+
from vertexai.generative_models import GenerativeModel
23+
24+
# TODO(developer): Update and un-comment below line
25+
# PROJECT_ID = "your-project-id"
26+
vertexai.init(
27+
project=PROJECT_ID,
28+
location="us-central1",
29+
# Options:
30+
# - "dedicated": Use Provisioned Throughput
31+
# - "shared": Use pay-as-you-go
32+
# https://cloud.google.com/vertex-ai/generative-ai/docs/use-provisioned-throughput
33+
request_metadata=[("x-vertex-ai-llm-request-type", "shared")],
34+
)
35+
36+
model = GenerativeModel("gemini-1.5-flash-002")
37+
38+
response = model.generate_content(
39+
"What's a good name for a flower shop that specializes in selling bouquets of dried flowers?"
40+
)
41+
42+
print(response.text)
43+
# Example response:
44+
# **Emphasizing the Dried Aspect:**
45+
# * Everlasting Blooms
46+
# * Dried & Delightful
47+
# * The Petal Preserve
48+
# ...
49+
50+
# [END generativeaionvertexai_provisioned_throughput_with_txt]
51+
return response.text
52+
53+
54+
if __name__ == "__main__":
55+
generate_content()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
backoff==2.2.1
2+
google-api-core==2.19.0
3+
pytest==8.2.0
4+
pytest-asyncio==0.23.6
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-cloud-aiplatform==1.82.0
2+
google-auth==2.38.0
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
import provisioned_throughput_with_txt
17+
18+
19+
def test_provisioned_throughput_with_txt() -> None:
20+
response = provisioned_throughput_with_txt.generate_content()
21+
assert response

0 commit comments

Comments
 (0)