From a9dda8502b08fb8d1acaf9ff621930c4f8ec7085 Mon Sep 17 00:00:00 2001 From: Holt Skinner Date: Mon, 10 Mar 2025 15:45:45 -0500 Subject: [PATCH] chore: Remove `openai` directory - Samples are duplicates of `chat_completions` directory --- generative_ai/openai/chat_openai_example.py | 59 --------------- .../openai/chat_openai_image_example.py | 70 ------------------ .../chat_openai_image_stream_example.py | 71 ------------------- .../openai/chat_openai_stream_example.py | 61 ---------------- .../credentials_refresher_class_example.py | 46 ------------ .../credentials_refresher_usage_example.py | 51 ------------- generative_ai/openai/noxfile_config.py | 42 ----------- generative_ai/openai/requirements-test.txt | 4 -- generative_ai/openai/requirements.txt | 14 ---- generative_ai/openai/test_openai_examples.py | 44 ------------ 10 files changed, 462 deletions(-) delete mode 100644 generative_ai/openai/chat_openai_example.py delete mode 100644 generative_ai/openai/chat_openai_image_example.py delete mode 100644 generative_ai/openai/chat_openai_image_stream_example.py delete mode 100644 generative_ai/openai/chat_openai_stream_example.py delete mode 100644 generative_ai/openai/credentials_refresher_class_example.py delete mode 100644 generative_ai/openai/credentials_refresher_usage_example.py delete mode 100644 generative_ai/openai/noxfile_config.py delete mode 100644 generative_ai/openai/requirements-test.txt delete mode 100644 generative_ai/openai/requirements.txt delete mode 100644 generative_ai/openai/test_openai_examples.py diff --git a/generative_ai/openai/chat_openai_example.py b/generative_ai/openai/chat_openai_example.py deleted file mode 100644 index fccc6ae72303..000000000000 --- a/generative_ai/openai/chat_openai_example.py +++ /dev/null @@ -1,59 +0,0 @@ -# Copyright 2024 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 os - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def generate_text() -> object: - # [START generativeaionvertexai_gemini_chat_completions_non_streaming] - import vertexai - import openai - - from google.auth import default, transport - - # TODO(developer): Update and un-comment below line - # PROJECT_ID = "your-project-id" - location = "us-central1" - - vertexai.init(project=PROJECT_ID, location=location) - - # Programmatically get an access token - credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"]) - auth_request = transport.requests.Request() - credentials.refresh(auth_request) - - # # OpenAI Client - client = openai.OpenAI( - base_url=f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi", - api_key=credentials.token, - ) - - response = client.chat.completions.create( - model="google/gemini-1.5-flash-002", - messages=[{"role": "user", "content": "Why is the sky blue?"}], - ) - - print(response.choices[0].message.content) - # Example response: - # The sky is blue due to a phenomenon called **Rayleigh scattering**. - # Sunlight is made up of all the colors of the rainbow. - # As sunlight enters the Earth's atmosphere ... - - # [END generativeaionvertexai_gemini_chat_completions_non_streaming] - return response - - -if __name__ == "__main__": - generate_text() diff --git a/generative_ai/openai/chat_openai_image_example.py b/generative_ai/openai/chat_openai_image_example.py deleted file mode 100644 index f0c9cf9e3bf6..000000000000 --- a/generative_ai/openai/chat_openai_image_example.py +++ /dev/null @@ -1,70 +0,0 @@ -# Copyright 2024 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 os - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def generate_text() -> object: - # [START generativeaionvertexai_gemini_chat_completions_non_streaming_image] - import vertexai - import openai - - from google.auth import default, transport - - # TODO(developer): Update and un-comment below lines - # PROJECT_ID = "your-project-id" - location = "us-central1" - - vertexai.init(project=PROJECT_ID, location=location) - - # Programmatically get an access token - credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"]) - auth_request = transport.requests.Request() - credentials.refresh(auth_request) - - # OpenAI Client - client = openai.OpenAI( - base_url=f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi", - api_key=credentials.token, - ) - - response = client.chat.completions.create( - model="google/gemini-1.5-flash-002", - messages=[ - { - "role": "user", - "content": [ - {"type": "text", "text": "Describe the following image:"}, - { - "type": "image_url", - "image_url": "gs://cloud-samples-data/generative-ai/image/scones.jpg", - }, - ], - } - ], - ) - - print(response.choices[0].message.content) - # Example response: - # Here's a description of the image: - # High-angle, close-up view of a rustic arrangement featuring several blueberry scones - # on a piece of parchment paper. The scones are golden-brown... - - # [END generativeaionvertexai_gemini_chat_completions_non_streaming_image] - return response - - -if __name__ == "__main__": - generate_text() diff --git a/generative_ai/openai/chat_openai_image_stream_example.py b/generative_ai/openai/chat_openai_image_stream_example.py deleted file mode 100644 index 60daa3f31dff..000000000000 --- a/generative_ai/openai/chat_openai_image_stream_example.py +++ /dev/null @@ -1,71 +0,0 @@ -# Copyright 2024 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 os - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def generate_text() -> object: - # [START generativeaionvertexai_gemini_chat_completions_streaming_image] - import vertexai - import openai - - from google.auth import default, transport - - # TODO(developer): Update and un-comment below line - # PROJECT_ID = "your-project-id" - location = "us-central1" - - vertexai.init(project=PROJECT_ID, location=location) - - # Programmatically get an access token - credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"]) - auth_request = transport.requests.Request() - credentials.refresh(auth_request) - - # OpenAI Client - client = openai.OpenAI( - base_url=f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi", - api_key=credentials.token, - ) - - response = client.chat.completions.create( - model="google/gemini-1.5-flash-002", - messages=[ - { - "role": "user", - "content": [ - {"type": "text", "text": "Describe the following image:"}, - { - "type": "image_url", - "image_url": "gs://cloud-samples-data/generative-ai/image/scones.jpg", - }, - ], - } - ], - stream=True, - ) - for chunk in response: - print(chunk.choices[0].delta.content) - # Example response: - # Here's a description of the image: - # High-angle, close-up view of a rustic scene featuring several blueberry - # scones arranged on a piece of parchment paper... - - # [END generativeaionvertexai_gemini_chat_completions_streaming_image] - return response - - -if __name__ == "__main__": - generate_text() diff --git a/generative_ai/openai/chat_openai_stream_example.py b/generative_ai/openai/chat_openai_stream_example.py deleted file mode 100644 index 2eeb85b5feb0..000000000000 --- a/generative_ai/openai/chat_openai_stream_example.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright 2024 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 os - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def generate_text() -> object: - # [START generativeaionvertexai_gemini_chat_completions_streaming] - import vertexai - import openai - - from google.auth import default, transport - - # TODO(developer): Update and un-comment below line - # PROJECT_ID = "your-project-id" - location = "us-central1" - - vertexai.init(project=PROJECT_ID, location=location) - - # Programmatically get an access token - credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"]) - auth_request = transport.requests.Request() - credentials.refresh(auth_request) - - # OpenAI Client - client = openai.OpenAI( - base_url=f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi", - api_key=credentials.token, - ) - - response = client.chat.completions.create( - model="google/gemini-1.5-flash-002", - messages=[{"role": "user", "content": "Why is the sky blue?"}], - stream=True, - ) - for chunk in response: - print(chunk.choices[0].delta.content) - # Example response: - # The sky is blue due to a phenomenon called **Rayleigh scattering**. Sunlight is - # made up of all the colors of the rainbow. When sunlight enters the Earth 's atmosphere, - # it collides with tiny air molecules (mostly nitrogen and oxygen). ... - - # [END generativeaionvertexai_gemini_chat_completions_streaming] - - return response - - -if __name__ == "__main__": - generate_text() diff --git a/generative_ai/openai/credentials_refresher_class_example.py b/generative_ai/openai/credentials_refresher_class_example.py deleted file mode 100644 index f8344a1dd48b..000000000000 --- a/generative_ai/openai/credentials_refresher_class_example.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright 2024 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. - -# Disable linting on `Any` type annotations (needed for OpenAI kwargs and attributes). -# flake8: noqa ANN401 - -# [START generativeaionvertexai_credentials_refresher_class] -from typing import Any - -import google.auth -import google.auth.transport.requests -import openai - - -class OpenAICredentialsRefresher: - def __init__(self, **kwargs: Any) -> None: - # Set a dummy key here - self.client = openai.OpenAI(**kwargs, api_key="DUMMY") - self.creds, self.project = google.auth.default( - scopes=["https://www.googleapis.com/auth/cloud-platform"] - ) - - def __getattr__(self, name: str) -> Any: - if not self.creds.valid: - auth_req = google.auth.transport.requests.Request() - self.creds.refresh(auth_req) - - if not self.creds.valid: - raise RuntimeError("Unable to refresh auth") - - self.client.api_key = self.creds.token - return getattr(self.client, name) - - -# [END generativeaionvertexai_credentials_refresher_class] diff --git a/generative_ai/openai/credentials_refresher_usage_example.py b/generative_ai/openai/credentials_refresher_usage_example.py deleted file mode 100644 index 5b4ef68356a0..000000000000 --- a/generative_ai/openai/credentials_refresher_usage_example.py +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright 2024 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. - -# Disable linting on `Any` type annotations (needed for OpenAI kwargs and attributes). -# flake8: noqa ANN401 -import os - -from credentials_refresher_class_example import OpenAICredentialsRefresher - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def generate_text() -> object: - # [START generativeaionvertexai_credentials_refresher_usage] - - # TODO(developer): Update and un-comment below line - # PROJECT_ID = "your-project-id" - location = "us-central1" - - client = OpenAICredentialsRefresher( - base_url=f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi", - ) - - response = client.chat.completions.create( - model="google/gemini-1.5-flash-002", - messages=[{"role": "user", "content": "Why is the sky blue?"}], - ) - - print(response.choices[0].message.content) - # Example response: - # The sky is blue due to a phenomenon called **Rayleigh scattering**. - # Sunlight is made up of all the colors of the rainbow. - # When sunlight enters the Earth's atmosphere, it collides with ... - - # [END generativeaionvertexai_credentials_refresher_usage] - return response - - -if __name__ == "__main__": - generate_text() diff --git a/generative_ai/openai/noxfile_config.py b/generative_ai/openai/noxfile_config.py deleted file mode 100644 index 962ba40a9269..000000000000 --- a/generative_ai/openai/noxfile_config.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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.13"], - # 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/generative_ai/openai/requirements-test.txt b/generative_ai/openai/requirements-test.txt deleted file mode 100644 index 92281986e50a..000000000000 --- a/generative_ai/openai/requirements-test.txt +++ /dev/null @@ -1,4 +0,0 @@ -backoff==2.2.1 -google-api-core==2.19.0 -pytest==8.2.0 -pytest-asyncio==0.23.6 diff --git a/generative_ai/openai/requirements.txt b/generative_ai/openai/requirements.txt deleted file mode 100644 index bf6a44625f1e..000000000000 --- a/generative_ai/openai/requirements.txt +++ /dev/null @@ -1,14 +0,0 @@ -pandas==1.3.5; python_version == '3.7' -pandas==2.0.3; python_version == '3.8' -pandas==2.1.4; python_version > '3.8' -pillow==10.3.0; python_version < '3.8' -pillow==10.3.0; python_version >= '3.8' -google-cloud-aiplatform[all]==1.69.0 -sentencepiece==0.2.0 -google-auth==2.38.0 -anthropic[vertex]==0.28.0 -langchain-core==0.2.33 -langchain-google-vertexai==1.0.10 -numpy<2 -openai==1.30.5 -immutabledict==4.2.0 diff --git a/generative_ai/openai/test_openai_examples.py b/generative_ai/openai/test_openai_examples.py deleted file mode 100644 index 01e4bab0124a..000000000000 --- a/generative_ai/openai/test_openai_examples.py +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright 2024 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 chat_openai_example -import chat_openai_image_example -import chat_openai_image_stream_example -import chat_openai_stream_example -import credentials_refresher_usage_example - - -def test_credentials_refresher() -> None: - response = credentials_refresher_usage_example.generate_text() - assert response - - -def test_non_streaming_text() -> None: - response = chat_openai_example.generate_text() - assert response - - -def test_non_streaming_image() -> None: - response = chat_openai_image_example.generate_text() - assert response - - -def test_streaming_image() -> None: - response = chat_openai_image_stream_example.generate_text() - assert response - - -def test_streaming_text() -> None: - response = chat_openai_stream_example.generate_text() - assert response