Skip to content

Commit

Permalink
chore: update docs and region tags (#11611)
Browse files Browse the repository at this point in the history
* chore: update docs and region tags

No changes to code.

* chore: updates as per linter suggestions
  • Loading branch information
msampathkumar authored May 2, 2024
1 parent 8f2a0a1 commit c8b11a6
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 116 deletions.
18 changes: 8 additions & 10 deletions generative_ai/anthropic_claude_3_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_claude_3_streaming]
# TODO(developer): Vertex AI SDK - uncomment below & run
# pip3 install --upgrade --user google-cloud-aiplatform
# gcloud auth application-default login
# pip3 install -U 'anthropic[vertex]'

from anthropic import AnthropicVertex
def generate_text_streaming(project_id: str, region: str) -> str:
# [START aiplatform_claude_3_streaming]
# TODO(developer): Vertex AI SDK - uncomment below & run
# pip3 install --upgrade --user google-cloud-aiplatform
# gcloud auth application-default login
# pip3 install -U 'anthropic[vertex]'

from anthropic import AnthropicVertex

def generate_text_streaming(project_id: str, region: str) -> str:
client = AnthropicVertex(region=region, project_id=project_id)
result = []

Expand All @@ -39,11 +39,9 @@ def generate_text_streaming(project_id: str, region: str) -> str:
print(text, end="", flush=True)
result.append(text)

# [END aiplatform_claude_3_streaming]
return "".join(result)


# [END aiplatform_claude_3_streaming]


if __name__ == "__main__":
generate_text_streaming()
18 changes: 8 additions & 10 deletions generative_ai/anthropic_claude_3_unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_claude_3_unary]
# TODO(developer): Vertex AI SDK - uncomment below & run
# pip3 install --upgrade --user google-cloud-aiplatform
# gcloud auth application-default login
# pip3 install -U 'anthropic[vertex]'

from anthropic import AnthropicVertex
def generate_text(project_id: str, region: str) -> object:
# [START aiplatform_claude_3_unary]
# TODO(developer): Vertex AI SDK - uncomment below & run
# pip3 install --upgrade --user google-cloud-aiplatform
# gcloud auth application-default login
# pip3 install -U 'anthropic[vertex]'

from anthropic import AnthropicVertex

def generate_text(project_id: str, region: str) -> object:
client = AnthropicVertex(region=region, project_id=project_id)
message = client.messages.create(
model="claude-3-sonnet@20240229",
Expand All @@ -34,11 +34,9 @@ def generate_text(project_id: str, region: str) -> object:
],
)
print(message.model_dump_json(indent=2))
# [END aiplatform_claude_3_unary]
return message


# [END aiplatform_claude_3_unary]


if __name__ == "__main__":
generate_text()
7 changes: 3 additions & 4 deletions generative_ai/code_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_sdk_code_chat]
from vertexai.language_models import CodeChatModel


def write_a_function(temperature: float = 0.5) -> object:
"""Example of using Codey for Code Chat Model to write a function."""
# [START aiplatform_sdk_code_chat]
from vertexai.language_models import CodeChatModel

# TODO developer - override these parameters as needed:
parameters = {
Expand All @@ -32,10 +31,10 @@ def write_a_function(temperature: float = 0.5) -> object:
"Please help write a function to calculate the min of two numbers", **parameters
)
print(f"Response from Model: {response.text}")
# [END aiplatform_sdk_code_chat]

return response


# [END aiplatform_sdk_code_chat]
if __name__ == "__main__":
write_a_function()
7 changes: 3 additions & 4 deletions generative_ai/code_completion_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_sdk_code_completion_comment]
from vertexai.language_models import CodeGenerationModel


def complete_code_function(temperature: float = 0.2) -> object:
"""Example of using Codey for Code Completion to complete a function."""
# [START aiplatform_sdk_code_completion_comment]
from vertexai.language_models import CodeGenerationModel

# TODO developer - override these parameters as needed:
parameters = {
Expand All @@ -31,10 +30,10 @@ def complete_code_function(temperature: float = 0.2) -> object:
)

print(f"Response from Model: {response.text}")
# [END aiplatform_sdk_code_completion_comment]

return response


# [END aiplatform_sdk_code_completion_comment]
if __name__ == "__main__":
complete_code_function()
7 changes: 3 additions & 4 deletions generative_ai/code_completion_test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_sdk_code_completion_test_function]
from vertexai.language_models import CodeGenerationModel


def complete_test_function(temperature: float = 0.2) -> object:
"""Example of using Codey for Code Completion to complete a test function."""
# [START aiplatform_sdk_code_completion_test_function]
from vertexai.language_models import CodeGenerationModel

# TODO developer - override these parameters as needed:
parameters = {
Expand All @@ -34,10 +33,10 @@ def test_empty_input_string()""",
)

print(f"Response from Model: {response.text}")
# [END aiplatform_sdk_code_completion_test_function]

return response


# [END aiplatform_sdk_code_completion_test_function]
if __name__ == "__main__":
complete_test_function()
14 changes: 7 additions & 7 deletions generative_ai/grounding.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_sdk_grounding]
from typing import Optional

import vertexai
from vertexai.language_models import (
GroundingSource,
TextGenerationModel,
TextGenerationResponse,
)
from vertexai.language_models import TextGenerationResponse


def grounding(
Expand All @@ -30,7 +24,12 @@ def grounding(
data_store_id: Optional[str],
) -> TextGenerationResponse:
"""Grounding example with a Large Language Model"""
# [START aiplatform_sdk_grounding]
import vertexai

from vertexai.language_models import GroundingSource, TextGenerationModel

# TODO(developer): Update values for project_id, location
vertexai.init(project=project_id, location=location)

# TODO developer - override these parameters as needed:
Expand All @@ -43,6 +42,7 @@ def grounding(

model = TextGenerationModel.from_pretrained("text-bison@002")

# TODO(developer): Update values for data_store_location, data_store_id
if data_store_id and data_store_location:
# Use Vertex AI Search data store
grounding_source = GroundingSource.VertexAISearch(
Expand Down
10 changes: 5 additions & 5 deletions generative_ai/ideation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_sdk_ideation]
import vertexai
from vertexai.language_models import TextGenerationModel


def interview(
temperature: float,
project_id: str,
location: str,
) -> str:
"""Ideation example with a Large Language Model"""
# [START aiplatform_sdk_ideation]
import vertexai

from vertexai.language_models import TextGenerationModel

# TODO(developer): Update values for project_id, location & temperature
vertexai.init(project=project_id, location=location)
# TODO developer - override these parameters as needed:
parameters = {
"temperature": temperature, # Temperature controls the degree of randomness in token selection.
"max_output_tokens": 256, # Token limit determines the maximum amount of text output.
Expand Down
13 changes: 7 additions & 6 deletions generative_ai/list_tuned_code_generation_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,27 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_sdk_list_tuned_code_generation_models]

import vertexai
from vertexai.language_models import CodeGenerationModel


def list_tuned_code_generation_models(
project_id: str,
location: str,
) -> None:
"""List tuned models."""
# [START aiplatform_sdk_list_tuned_code_generation_models]

import vertexai
from vertexai.preview.language_models import CodeGenerationModel

# TODO(developer): Update values for project_id & location
vertexai.init(project=project_id, location=location)
model = CodeGenerationModel.from_pretrained("code-bison@001")
tuned_model_names = model.list_tuned_model_names()
print(tuned_model_names)
# [END aiplatform_sdk_list_tuned_code_generation_models]

return tuned_model_names


# [END aiplatform_sdk_list_tuned_code_generation_models]
if __name__ == "__main__":
import google.auth

Expand Down
12 changes: 6 additions & 6 deletions generative_ai/list_tuned_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_sdk_list_tuned_models]

import vertexai
from vertexai.language_models import TextGenerationModel


def list_tuned_models(
project_id: str,
location: str,
) -> None:
"""List tuned models."""
# [START aiplatform_sdk_list_tuned_models]
import vertexai

from vertexai.language_models import TextGenerationModel

# TODO(developer): Update values for project_id & location
vertexai.init(project=project_id, location=location)
model = TextGenerationModel.from_pretrained("text-bison@002")
tuned_model_names = model.list_tuned_model_names()
print(tuned_model_names)
# [END aiplatform_sdk_list_tuned_models]

return tuned_model_names


# [END aiplatform_sdk_list_tuned_models]
if __name__ == "__main__":
list_tuned_models()
12 changes: 5 additions & 7 deletions generative_ai/multimodal_embedding_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_sdk_multimodal_embedding_image]
from typing import Optional

import vertexai
from vertexai.vision_models import (
Image,
MultiModalEmbeddingModel,
MultiModalEmbeddingResponse,
)
from vertexai.vision_models import MultiModalEmbeddingResponse


def get_image_embeddings(
Expand All @@ -37,7 +31,11 @@ def get_image_embeddings(
image_path: Path to image (local or Google Cloud Storage) to generate embeddings for.
contextual_text: Text to generate embeddings for.
"""
# [START aiplatform_sdk_multimodal_embedding_image]
import vertexai
from vertexai.vision_models import Image, MultiModalEmbeddingModel

# TODO(developer): Update values for project_id, location, image_path & contextual_text
vertexai.init(project=project_id, location=location)

model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding")
Expand Down
16 changes: 7 additions & 9 deletions generative_ai/multimodal_embedding_image_video_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_sdk_multimodal_embedding_image_video_text]
from typing import Optional

import vertexai
from vertexai.vision_models import (
Image,
MultiModalEmbeddingModel,
MultiModalEmbeddingResponse,
Video,
VideoSegmentConfig,
)
from vertexai.vision_models import MultiModalEmbeddingResponse, VideoSegmentConfig


def get_image_video_text_embeddings(
Expand All @@ -47,7 +39,13 @@ def get_image_video_text_embeddings(
video_segment_config: Define specific segments to generate embeddings for.
https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-multimodal-embeddings#video-best-practices
"""
# [START aiplatform_sdk_multimodal_embedding_image_video_text]
import vertexai

from vertexai.vision_models import Image, MultiModalEmbeddingModel, Video

# TODO(developer): Update values for project_id, location,
# image_path, video_path, contextual_text, video_segment_config
vertexai.init(project=project_id, location=location)

model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding")
Expand Down
15 changes: 7 additions & 8 deletions generative_ai/multimodal_embedding_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_sdk_multimodal_embedding_video]
from typing import Optional

import vertexai
from vertexai.vision_models import (
MultiModalEmbeddingModel,
MultiModalEmbeddingResponse,
Video,
VideoSegmentConfig,
)
from vertexai.vision_models import MultiModalEmbeddingResponse, VideoSegmentConfig


def get_video_embeddings(
Expand All @@ -44,7 +37,13 @@ def get_video_embeddings(
video_segment_config: Define specific segments to generate embeddings for.
https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-multimodal-embeddings#video-best-practices
"""
# [START aiplatform_sdk_multimodal_embedding_video]
import vertexai

from vertexai.vision_models import MultiModalEmbeddingModel, Video

# TODO(developer): Update values for project_id, location,
# video_path, contextual_text, dimension, video_segment_config
vertexai.init(project=project_id, location=location)

model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding")
Expand Down
Loading

0 comments on commit c8b11a6

Please sign in to comment.