diff --git a/model_garden/deepseek/noxfile_config.py b/model_garden/deepseek/noxfile_config.py new file mode 100644 index 00000000000..2a0f115c38f --- /dev/null +++ b/model_garden/deepseek/noxfile_config.py @@ -0,0 +1,42 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Default TEST_CONFIG_OVERRIDE for python repos. + +# You can copy this file into your directory, then it will be imported from +# the noxfile.py. + +# The source of truth: +# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py + +TEST_CONFIG_OVERRIDE = { + # You can opt out from the test for specific Python versions. + "ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.12"], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + "enforce_type_hints": True, + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. + "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', + # If you need to use a specific version of pip, + # change pip_version_override to the string representation + # of the version number, for example, "20.2.4" + "pip_version_override": None, + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": {}, +} diff --git a/model_garden/deepseek/requirements-test.txt b/model_garden/deepseek/requirements-test.txt new file mode 100644 index 00000000000..e43b7792721 --- /dev/null +++ b/model_garden/deepseek/requirements-test.txt @@ -0,0 +1,2 @@ +google-api-core==2.24.0 +pytest==8.2.0 diff --git a/model_garden/deepseek/requirements.txt b/model_garden/deepseek/requirements.txt new file mode 100644 index 00000000000..bd6f2345868 --- /dev/null +++ b/model_garden/deepseek/requirements.txt @@ -0,0 +1 @@ +requests==2.32.4 diff --git a/model_garden/deepseek/test_examples.py b/model_garden/deepseek/test_examples.py new file mode 100644 index 00000000000..e59ea7b9660 --- /dev/null +++ b/model_garden/deepseek/test_examples.py @@ -0,0 +1,21 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import textgen_with_txt + + +def test_textgen_with_txt() -> None: + """Test the example in the README.""" + content = textgen_with_txt.generate_content() + assert content diff --git a/model_garden/deepseek/textgen_with_txt.py b/model_garden/deepseek/textgen_with_txt.py new file mode 100644 index 00000000000..6f004dd8027 --- /dev/null +++ b/model_garden/deepseek/textgen_with_txt.py @@ -0,0 +1,91 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def get_bearer_token() -> str: + """To get project name and bearer token for Google Cloud ADC authentication.""" + import google.auth + from google.auth.transport.requests import Request + + creds, project_name = google.auth.default( + scopes=["https://www.googleapis.com/auth/cloud-platform"] + ) + auth_req = Request() + creds.refresh(auth_req) + bearer_token = creds.token + return project_name, bearer_token + + +def generate_content() -> str: + """To generate content from DeepSeek model hosted in Vertex AI Model Garden.""" + import requests + import json + + project_name, bearer_token = get_bearer_token() + + # Read more about the model here: + # https://cloud.google.com/vertex-ai/generative-ai/docs/maas/deepseek#streaming + location = "us-central1" + url = f"https://us-central1-aiplatform.googleapis.com/v1/projects/{project_name}/locations/{location}/endpoints/openapi/chat/completions" + + # Set the request header + headers = { + "Authorization": f"Bearer {bearer_token}", + "Content-Type": "application/json", + } + data = { + "model": "deepseek-ai/deepseek-r1-0528-maas", + # "stream": False, # As per the bash script + "messages": [{"role": "user", "content": "Why is the sky blue?"}], + } + # Send the request + response = requests.post(url, headers=headers, data=json.dumps(data)) + print(f"Request Response: {response.status_code}") + # Example response: + # Request Response: 200 + + # Load the response data + model_response = json.loads(response.content) + print(model_response["model"]) + # Example response: + # 'deepseek-ai/deepseek-r1-0528-maas' + print(model_response["usage"]) + # Example response: + # { + # 'completion_tokens': 3294, + # 'prompt_tokens': 10, + # 'total_tokens': 3304 + # } + + # Print user input & Model response + for each in data["messages"]: + print(f"{each['role']}>>> {each['content']}") + for each in model_response["choices"]: + print(f"{each['message']['role']}>>> {each['message']['content']}") + # Example response: + # user>>> Why is the sky blue? + # Example response: + # assistant>>> + # Okay, the user is asking why the sky is blue. + # ... + # + + # The sky appears blue due to a phenomenon called **Rayleigh scattering**, + # which occurs when sunlight passes through Earth's atmosphere and + # ... + return response.content + + +if __name__ == "__main__": + generate_content()