Skip to content

Commit

Permalink
refactor(generative-ai): style update (#11619)
Browse files Browse the repository at this point in the history
  • Loading branch information
gericdong authored May 6, 2024
1 parent 892c56a commit 2b3e64e
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 113 deletions.
17 changes: 10 additions & 7 deletions generative_ai/gemini_chat_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
# limitations under the License.


def chat_text_example(project_id: str, location: str) -> str:
def chat_text_example(project_id: str) -> str:
# [START generativeaionvertexai_gemini_multiturn_chat]
import vertexai

from vertexai.generative_models import GenerativeModel, ChatSession

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "us-central1"
vertexai.init(project=project_id, location=location)

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

model = GenerativeModel(model_name="gemini-1.0-pro-002")

chat = model.start_chat()

def get_chat_response(chat: ChatSession, prompt: str) -> str:
Expand All @@ -43,17 +44,19 @@ def get_chat_response(chat: ChatSession, prompt: str) -> str:
return get_chat_response(chat, "Hello")


def chat_stream_example(project_id: str, location: str) -> str:
def chat_stream_example(project_id: str) -> str:
# [START generativeaionvertexai_gemini_multiturn_chat_stream]
import vertexai

from vertexai.generative_models import GenerativeModel, ChatSession

# TODO(developer): Update and un-comment below lines
# TODO(developer): Update and un-comment below line
# project_id = "PROJECT_ID"
# location = "us-central1"
vertexai.init(project=project_id, location=location)

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

model = GenerativeModel(model_name="gemini-1.0-pro-002")

chat = model.start_chat()

def get_chat_response(chat: ChatSession, prompt: str) -> str:
Expand Down
20 changes: 8 additions & 12 deletions generative_ai/gemini_count_token_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,21 @@
# limitations under the License.


def generate_text(project_id: str, location: str) -> str:
def count_tokens(project_id: str) -> str:
# [START generativeaionvertexai_gemini_token_count]
import vertexai

from vertexai.generative_models import GenerativeModel

# Initialize Vertex AI
vertexai.init(project=project_id, location=location)
# TODO(developer): Update and un-comment below line
# project_id = "PROJECT_ID"

# Load the model
model = GenerativeModel(model_name="gemini-1.0-pro-002")
vertexai.init(project=project_id, location="us-central1")

# prompt tokens count
print(model.count_tokens("why is sky blue?"))
model = GenerativeModel(model_name="gemini-1.0-pro-002")

# Load example images
response = model.generate_content("why is sky blue?")
response = model.count_tokens("Why is sky blue?")
print(response)

# response tokens count
print(response._raw_response.usage_metadata)
# [END generativeaionvertexai_gemini_token_count]
return response.text
return response
21 changes: 11 additions & 10 deletions generative_ai/gemini_grounding_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
from vertexai.preview.generative_models import GenerationResponse


def generate_text_with_grounding_web(
project_id: str, location: str
) -> GenerationResponse:
def generate_text_with_grounding_web(project_id: str) -> GenerationResponse:
# [START generativeaionvertexai_gemini_grounding_with_web]
import vertexai

from vertexai.preview.generative_models import grounding
from vertexai.generative_models import GenerationConfig, GenerativeModel, Tool

# Initialize Vertex AI
vertexai.init(project=project_id, location=location)
# TODO(developer): Update and un-comment below line
# project_id = "PROJECT_ID"

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

# Load the model
model = GenerativeModel(model_name="gemini-1.0-pro-002")

# Use Google Search for grounding
Expand All @@ -49,18 +49,19 @@ def generate_text_with_grounding_web(


def generate_text_with_grounding_vertex_ai_search(
project_id: str, location: str, data_store_path: str
project_id: str, data_store_path: str
) -> GenerationResponse:
# [START generativeaionvertexai_gemini_grounding_with_vais]
import vertexai

from vertexai.preview.generative_models import grounding
from vertexai.generative_models import GenerationConfig, GenerativeModel, Tool

# Initialize Vertex AI
vertexai.init(project=project_id, location=location)
# TODO(developer): Update and un-comment below line
# project_id = "PROJECT_ID"

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

# Load the model
model = GenerativeModel(model_name="gemini-1.0-pro-002")

# Use Vertex AI Search data store
Expand Down
31 changes: 16 additions & 15 deletions generative_ai/gemini_guide_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,30 @@
# limitations under the License.


def generate_text(project_id: str, location: str) -> str:
def generate_text(project_id: str) -> str:
# [START generativeaionvertexai_gemini_get_started]

import vertexai

from vertexai.generative_models import GenerativeModel, Part

# Initialize Vertex AI
vertexai.init(project=project_id, location=location)
# Load the model
multimodal_model = GenerativeModel(model_name="gemini-1.0-pro-vision-001")
# Query the model
response = multimodal_model.generate_content(
# TODO(developer): Update and un-comment below line
# project_id = "PROJECT_ID"

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

model = GenerativeModel(model_name="gemini-1.0-pro-vision-001")

response = model.generate_content(
[
# Add an example image
Part.from_uri(
"gs://generativeai-downloads/images/scones.jpg", mime_type="image/jpeg"
"gs://cloud-samples-data/generative-ai/image/scones.jpg",
mime_type="image/jpeg",
),
# Add an example query
"what is shown in this image?",
"What is shown in this image?",
]
)
print(response)
return response.text

print(response.text)
# [END generativeaionvertexai_gemini_get_started]

# [END generativeaionvertexai_gemini_get_started]
return response.text
43 changes: 19 additions & 24 deletions generative_ai/gemini_multi_image_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,42 @@
# limitations under the License.


def generate_text_multimodal(project_id: str, location: str) -> str:
def generate_text_multimodal(project_id: str) -> str:
# [START generativeaionvertexai_gemini_single_turn_multi_image]
import http.client
import typing
import urllib.request
import vertexai

from vertexai.generative_models import GenerativeModel, Image
from vertexai.generative_models import GenerativeModel, Part

# Initialize Vertex AI
vertexai.init(project=project_id, location=location)
# TODO(developer): Update and un-comment below line
# project_id = "PROJECT_ID"

# create helper function
def load_image_from_url(image_url: str) -> Image:
with urllib.request.urlopen(image_url) as response:
response = typing.cast(http.client.HTTPResponse, response)
image_bytes = response.read()
return Image.from_bytes(image_bytes)
vertexai.init(project=project_id, location="us-central1")

# Load images from Cloud Storage URI
landmark1 = load_image_from_url(
"https://storage.googleapis.com/cloud-samples-data/vertex-ai/llm/prompts/landmark1.png"
image_file1 = Part.from_uri(
"gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png",
mime_type="image/png",
)
landmark2 = load_image_from_url(
"https://storage.googleapis.com/cloud-samples-data/vertex-ai/llm/prompts/landmark2.png"
image_file2 = Part.from_uri(
"gs://cloud-samples-data/vertex-ai/llm/prompts/landmark2.png",
mime_type="image/png",
)
landmark3 = load_image_from_url(
"https://storage.googleapis.com/cloud-samples-data/vertex-ai/llm/prompts/landmark3.png"
image_file3 = Part.from_uri(
"gs://cloud-samples-data/vertex-ai/llm/prompts/landmark3.png",
mime_type="image/png",
)

# Pass multimodal prompt
model = GenerativeModel(model_name="gemini-1.0-pro-vision-001")
response = model.generate_content(
[
landmark1,
image_file1,
"city: Rome, Landmark: the Colosseum",
landmark2,
image_file2,
"city: Beijing, Landmark: Forbidden City",
landmark3,
image_file3,
]
)
print(response)
print(response.text)

# [END generativeaionvertexai_gemini_single_turn_multi_image]
return response.text
19 changes: 10 additions & 9 deletions generative_ai/gemini_pro_basic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@
# limitations under the License.


def generate_text(project_id: str, location: str) -> None:
def generate_text(project_id: str) -> None:
# [START generativeaionvertexai_gemini_pro_example]
import vertexai

from vertexai.generative_models import GenerativeModel, Part

# Initialize Vertex AI
vertexai.init(project=project_id, location=location)
# TODO(developer): Update and un-comment below line
# project_id = "PROJECT_ID"

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

# Load the model
model = GenerativeModel(model_name="gemini-1.0-pro-vision-001")

# Load example image
image_url = "gs://generativeai-downloads/images/scones.jpg"
image_content = Part.from_uri(image_url, "image/jpeg")
image_file = Part.from_uri(
"gs://cloud-samples-data/generative-ai/image/scones.jpg", "image/jpeg"
)

# Query the model
response = model.generate_content([image_content, "what is this image?"])
print(response)
response = model.generate_content([image_file, "what is this image?"])
print(response.text)

# [END generativeaionvertexai_gemini_pro_example]
return response.text
9 changes: 5 additions & 4 deletions generative_ai/gemini_pro_config_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@
# limitations under the License.


def generate_text(project_id: str, location: str) -> None:
def generate_text(project_id: str) -> None:
# [START generativeaionvertexai_gemini_pro_config_example]
import base64
import vertexai

from vertexai.generative_models import GenerationConfig, GenerativeModel, Part

# Initialize Vertex AI
vertexai.init(project=project_id, location=location)
# TODO(developer): Update and un-comment below line
# project_id = "PROJECT_ID"

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

# Load the model
model = GenerativeModel(model_name="gemini-1.0-pro-vision-001")

# Load example image from local storage
Expand Down
18 changes: 12 additions & 6 deletions generative_ai/gemini_safety_config_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
from vertexai.generative_models import Part


def generate_text(project_id: str, location: str, image: Part) -> str:
def generate_text(project_id: str) -> str:
# [START generativeaionvertexai_gemini_safety_settings]
import vertexai

from vertexai import generative_models

# Initialize Vertex AI
vertexai.init(project=project_id, location=location)
# TODO(developer): Update and un-comment below line
# project_id = "PROJECT_ID"

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

# Load the model
model = generative_models.GenerativeModel(model_name="gemini-1.0-pro-vision-001")

# Generation config
Expand All @@ -44,17 +45,22 @@ def generate_text(project_id: str, location: str, image: Part) -> str:
),
]

image_file = Part.from_uri(
"gs://cloud-samples-data/generative-ai/image/scones.jpg", "image/jpeg"
)

# Generate content
responses = model.generate_content(
[image, "Add your prompt here"],
[image_file, "What is in this image?"],
generation_config=generation_config,
stream=True,
safety_settings=safety_config,
stream=True,
)

text_responses = []
for response in responses:
print(response.text)
text_responses.append(response.text)
# [END generativeaionvertexai_gemini_safety_settings]

return "".join(text_responses)
13 changes: 8 additions & 5 deletions generative_ai/gemini_single_turn_video_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
# limitations under the License.


def generate_text(project_id: str, location: str) -> str:
def generate_text(project_id: str) -> str:
# [START generativeaionvertexai_gemini_single_turn_video]
import vertexai

from vertexai.generative_models import GenerativeModel, Part

# Initialize Vertex AI
vertexai.init(project=project_id, location=location)
# Load the model
# TODO(developer): Update and un-comment below line
# project_id = "PROJECT_ID"

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

vision_model = GenerativeModel(model_name="gemini-1.0-pro-vision-001")

# Generate text
response = vision_model.generate_content(
[
Expand All @@ -32,7 +35,7 @@ def generate_text(project_id: str, location: str) -> str:
"What is in the video?",
]
)
print(response)
print(response.text)
# [END generativeaionvertexai_gemini_single_turn_video]

return response.text
Loading

0 comments on commit 2b3e64e

Please sign in to comment.