Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vertex-ai) convert "example syntax" markdown samples to python #12980

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions generative_ai/embeddings/multimodal_example_syntax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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

import vertexai

from vertexai.vision_models import MultiModalEmbeddingResponse

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")

vertexai.init(project=PROJECT_ID, location="us-central1")


def create_embeddings() -> MultiModalEmbeddingResponse:
# [START generativeaionvertexai_multimodal_embedding_example_syntax]
from vertexai.vision_models import MultiModalEmbeddingModel

model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding@001")
embeddings = model.get_embeddings(
contextual_text="Cars on Highway",
# image=...,
# video=...,
)
# [END generativeaionvertexai_multimodal_embedding_example_syntax]
return embeddings


if __name__ == "__main__":
create_embeddings()
14 changes: 14 additions & 0 deletions generative_ai/embeddings/test_embeddings_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
import generate_embeddings_with_lower_dimension
import model_tuning_example
import multimodal_example
import multimodal_example_syntax
import multimodal_image_example
import multimodal_video_example
import text_example_syntax


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
Expand Down Expand Up @@ -78,6 +80,18 @@ def test_generate_embeddings_with_lower_dimension() -> None:
assert len(embeddings.text_embedding) == 128


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
def test_create_embeddings() -> None:
embeddings = multimodal_example_syntax.create_embeddings()
assert embeddings is not None


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
def test_create_text_embeddings() -> None:
embeddings = text_example_syntax.create_embeddings()
assert embeddings is not None


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
def test_text_embed_text() -> None:
embeddings = document_retrieval_example.embed_text()
Expand Down
37 changes: 37 additions & 0 deletions generative_ai/embeddings/text_example_syntax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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

import vertexai

from vertexai.language_models import TextEmbedding

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")

vertexai.init(project=PROJECT_ID, location="us-central1")


def create_embeddings() -> TextEmbedding:
# [START generativeaionvertexai_text_embedding_example_syntax]
from vertexai.language_models import TextEmbeddingModel

model = TextEmbeddingModel.from_pretrained("text-embedding-005")
embeddings = model.get_embeddings(["Cars on a highway", "Traffic lights"])
# [END generativeaionvertexai_text_embedding_example_syntax]
return embeddings


if __name__ == "__main__":
create_embeddings()
56 changes: 56 additions & 0 deletions generative_ai/inference/example_syntax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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

import vertexai

from vertexai.language_models import TextEmbedding

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")

vertexai.init(project=PROJECT_ID, location="us-central1")


def generate_response() -> TextEmbedding:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combines the samples for streaming and non-streaming responses. Adds imports and fixes the syntax, so the sample can be copied and ran, but leaves off all details to keep the sample very concise.

# [START generativeaionvertexai_example_syntax]
from vertexai.generative_models import GenerationConfig, GenerativeModel

gemini_model = GenerativeModel("gemini-1.5-flash-002")
generation_config = GenerationConfig(
# See https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/content-generation-parameters
)
safety_settings = {
# See https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/configure-safety-filters
}
model_response = gemini_model.generate_content(
"...prompt content...",
generation_config=generation_config,
safety_settings=safety_settings,
)
# [END generativeaionvertexai_example_syntax]
# [START generativeaionvertexai_example_syntax_streaming]
model_response = gemini_model.generate_content(
"...prompt content...",
generation_config=generation_config,
safety_settings=safety_settings,
stream=True,
)
# [END generativeaionvertexai_example_syntax_streaming]

return model_response


if __name__ == "__main__":
generate_response()
msampathkumar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import example_syntax
import non_stream_multimodality_basic
import non_stream_text_basic
import stream_multimodality_basic
Expand All @@ -36,3 +37,8 @@ def test_stream_text_basic() -> None:
def test_stream_multi_modality_basic() -> None:
responses = stream_multimodality_basic.generate_content()
assert responses


def test_generate_response() -> None:
response = example_syntax.generate_response()
assert response is not None
Loading