Skip to content

Commit 016d070

Browse files
committed
fix(model_garden): add deepseek example
1 parent 4135331 commit 016d070

5 files changed

Lines changed: 157 additions & 0 deletions

File tree

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: 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+
requests==2.32.4
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+
import textgen_with_txt
16+
17+
18+
def test_textgen_with_txt() -> None:
19+
"""Test the example in the README."""
20+
content = textgen_with_txt.generate_content()
21+
assert content
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 get_bearer_token() -> str:
17+
"""To get project name and bearer token for Google Cloud ADC authentication."""
18+
import google.auth
19+
from google.auth.transport.requests import Request
20+
21+
creds, project_name = google.auth.default(
22+
scopes=["https://www.googleapis.com/auth/cloud-platform"]
23+
)
24+
auth_req = Request()
25+
creds.refresh(auth_req)
26+
bearer_token = creds.token
27+
return project_name, bearer_token
28+
29+
30+
def generate_content() -> str:
31+
"""To generate content from DeepSeek model hosted in Vertex AI Model Garden."""
32+
import requests
33+
import json
34+
35+
project_name, bearer_token = get_bearer_token()
36+
37+
# Read more about the model here:
38+
# https://cloud.google.com/vertex-ai/generative-ai/docs/maas/deepseek#streaming
39+
location = "us-central1"
40+
url = f"https://us-central1-aiplatform.googleapis.com/v1/projects/{project_name}/locations/{location}/endpoints/openapi/chat/completions"
41+
42+
# Set the request header
43+
headers = {
44+
"Authorization": f"Bearer {bearer_token}",
45+
"Content-Type": "application/json",
46+
}
47+
data = {
48+
"model": "deepseek-ai/deepseek-r1-0528-maas",
49+
# "stream": False, # As per the bash script
50+
"messages": [{"role": "user", "content": "Why is the sky blue?"}],
51+
}
52+
# Send the request
53+
response = requests.post(url, headers=headers, data=json.dumps(data))
54+
print(f"Request Response: {response.status_code}")
55+
# Example response:
56+
# Request Response: 200
57+
58+
# Load the response data
59+
model_response = json.loads(response.content)
60+
print(model_response["model"])
61+
# Example response:
62+
# 'deepseek-ai/deepseek-r1-0528-maas'
63+
print(model_response["usage"])
64+
# Example response:
65+
# {
66+
# 'completion_tokens': 3294,
67+
# 'prompt_tokens': 10,
68+
# 'total_tokens': 3304
69+
# }
70+
71+
# Print user input & Model response
72+
for each in data["messages"]:
73+
print(f"{each['role']}>>> {each['content']}")
74+
for each in model_response["choices"]:
75+
print(f"{each['message']['role']}>>> {each['message']['content']}")
76+
# Example response:
77+
# user>>> Why is the sky blue?
78+
# Example response:
79+
# assistant>>> <think>
80+
# Okay, the user is asking why the sky is blue.
81+
# ...
82+
# </think>
83+
84+
# The sky appears blue due to a phenomenon called **Rayleigh scattering**,
85+
# which occurs when sunlight passes through Earth's atmosphere and
86+
# ...
87+
return response.content
88+
89+
90+
if __name__ == "__main__":
91+
generate_content()

0 commit comments

Comments
 (0)