Skip to content

Commit 70df78b

Browse files
holtskinnermsampathkumarm-strzelczyk
authored
docs(generative_ai): Add Vertex AI Express Mode Sample (#12988)
* docs(generative_ai): Add Vertex AI Express Mode Sample - https://cloud.google.com/vertex-ai/generative-ai/docs/start/express-mode/overview --------- Co-authored-by: Sampath Kumar <sampathm@google.com> Co-authored-by: Maciej Strzelczyk <strzelczyk@google.com>
1 parent 72190ce commit 70df78b

5 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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() -> None:
17+
# [START generativeaionvertexai_gemini_express_mode]
18+
import vertexai
19+
from vertexai.generative_models import GenerativeModel
20+
21+
# TODO(developer): Update below line
22+
vertexai.init(api_key="YOUR_API_KEY")
23+
24+
model = GenerativeModel("gemini-1.5-flash")
25+
26+
response = model.generate_content("Explain bubble sort to me")
27+
28+
print(response.text)
29+
# Example response:
30+
# Bubble Sort is a simple sorting algorithm that repeatedly steps through the list
31+
# [END generativeaionvertexai_gemini_express_mode]
32+
return response.text
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 unittest.mock import MagicMock, patch
16+
17+
from vertexai.generative_models import (
18+
GenerationResponse,
19+
GenerativeModel,
20+
)
21+
22+
import api_key_example
23+
24+
25+
@patch.object(GenerativeModel, "generate_content")
26+
def test_api_key_example(mock_generate_content: MagicMock) -> None:
27+
# Mock the API response
28+
mock_generate_content.return_value = GenerationResponse.from_dict(
29+
{
30+
"candidates": [
31+
{
32+
"content": {
33+
"parts": [{"text": "This is a mocked bubble sort explanation."}]
34+
}
35+
}
36+
]
37+
}
38+
)
39+
40+
# Call the function
41+
response = api_key_example.generate_content()
42+
43+
# Assert that the function returns the expected value
44+
assert response == "This is a mocked bubble sort explanation."
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: 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-cloud-aiplatform==1.74.0

0 commit comments

Comments
 (0)