Skip to content

Commit

Permalink
feat(genai): Multiple updates to improve samples quality, consistency…
Browse files Browse the repository at this point in the history
… and readability (#13157)

* feat(genai): Update Gen AI SDK Samples for Consistency/Readability

- Updated to latest SDK version
- Added Typing and consistent imports
- Changed all GA Samples to include `HttpOptions` for `v1` endpoint
- Updated Gemini 2.0 Exp to Gemini 2.0 Flash GA
- Updated Batch Prediction to Gemini 2.0 GA

* Fix issue with PIL Image load

* Rename BatchPredict Files
  • Loading branch information
holtskinner authored Feb 12, 2025
1 parent d99c471 commit d3c36d2
Show file tree
Hide file tree
Showing 33 changed files with 174 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ def generate_content(output_uri: str) -> str:
import time

from google import genai
from google.genai.types import CreateBatchJobConfig, JobState, HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))

client = genai.Client()
# TODO(developer): Update and un-comment below line
# output_uri = f"bq://your-project.your_dataset.your_table"

job = client.batches.create(
model="gemini-1.5-pro-002",
model="gemini-2.0-flash-001",
src="bq://storage-samples.generative_ai.batch_requests_for_multimodal_input",
config={
"dest": output_uri
}
config=CreateBatchJobConfig(dest=output_uri),
)
print(f"Job name: {job.name}")
print(f"Job state: {job.state}")
Expand All @@ -37,12 +37,13 @@ def generate_content(output_uri: str) -> str:
# Job state: JOB_STATE_PENDING

# See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.BatchJob
completed_states = [
"JOB_STATE_SUCCEEDED",
"JOB_STATE_FAILED",
"JOB_STATE_CANCELLED",
"JOB_STATE_PAUSED",
]
completed_states = {
JobState.JOB_STATE_SUCCEEDED,
JobState.JOB_STATE_FAILED,
JobState.JOB_STATE_CANCELLED,
JobState.JOB_STATE_PAUSED,
}

while job.state not in completed_states:
time.sleep(30)
job = client.batches.get(name=job.name)
Expand All @@ -59,6 +60,4 @@ def generate_content(output_uri: str) -> str:


if __name__ == "__main__":
generate_content(
output_uri="bq://your-project.your_dataset.your_table"
)
generate_content(output_uri="bq://your-project.your_dataset.your_table")
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ def generate_content(output_uri: str) -> str:
import time

from google import genai
from google.genai.types import CreateBatchJobConfig, JobState, HttpOptions

client = genai.Client()
client = genai.Client(http_options=HttpOptions(api_version="v1"))
# TODO(developer): Update and un-comment below line
# output_uri = "gs://your-bucket/your-prefix"

# See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.batches.Batches.create
job = client.batches.create(
model="gemini-1.5-pro-002",
model="gemini-2.0-flash-001",
# Source link: https://storage.cloud.google.com/cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl
src="gs://cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl",
config={
"dest": output_uri
}
config=CreateBatchJobConfig(dest=output_uri),
)
print(f"Job name: {job.name}")
print(f"Job state: {job.state}")
Expand All @@ -39,12 +38,13 @@ def generate_content(output_uri: str) -> str:
# Job state: JOB_STATE_PENDING

# See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.BatchJob
completed_states = [
"JOB_STATE_SUCCEEDED",
"JOB_STATE_FAILED",
"JOB_STATE_CANCELLED",
"JOB_STATE_PAUSED",
]
completed_states = {
JobState.JOB_STATE_SUCCEEDED,
JobState.JOB_STATE_FAILED,
JobState.JOB_STATE_CANCELLED,
JobState.JOB_STATE_PAUSED,
}

while job.state not in completed_states:
time.sleep(30)
job = client.batches.get(name=job.name)
Expand All @@ -61,6 +61,4 @@ def generate_content(output_uri: str) -> str:


if __name__ == "__main__":
generate_content(
output_uri="gs://your-bucket/your-prefix"
)
generate_content(output_uri="gs://your-bucket/your-prefix")
2 changes: 1 addition & 1 deletion genai/batch_prediction/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
google-genai==0.8.0
google-genai==1.2.0
8 changes: 4 additions & 4 deletions genai/batch_prediction/test_batch_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import pytest

import submit_with_bq
import submit_with_gcs
import batchpredict_with_bq
import batchpredict_with_gcs


os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
Expand Down Expand Up @@ -57,10 +57,10 @@ def gcs_output_uri():


def test_batch_prediction_with_bq(bq_output_uri) -> None:
response = submit_with_bq.generate_content(output_uri=bq_output_uri)
response = batchpredict_with_bq.generate_content(output_uri=bq_output_uri)
assert response == JobState.JOB_STATE_SUCCEEDED


def test_batch_prediction_with_gcs(gcs_output_uri) -> None:
response = submit_with_gcs.generate_content(output_uri=gcs_output_uri)
response = batchpredict_with_gcs.generate_content(output_uri=gcs_output_uri)
assert response == JobState.JOB_STATE_SUCCEEDED
14 changes: 9 additions & 5 deletions genai/controlled_generation/ctrlgen_with_class_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,27 @@
def generate_content() -> str:
# [START googlegenaisdk_ctrlgen_with_class_schema]
from google import genai
from google.genai.types import GenerateContentConfig, HttpOptions

from pydantic import BaseModel

class Recipe(BaseModel):
recipe_name: str
ingredients: list[str]

client = genai.Client()
client = genai.Client(http_options=HttpOptions(api_version="v1"))
response = client.models.generate_content(
model="gemini-2.0-flash-001",
contents="List a few popular cookie recipes.",
config={
"response_mime_type": "application/json",
"response_schema": list[Recipe],
},
config=GenerateContentConfig(
response_mime_type="application/json",
response_schema=list[Recipe],
),
)
# Use the response as a JSON string.
print(response.text)
# Use the response as an object
print(response.parsed)

# Example output:
# [Recipe(recipe_name='Chocolate Chip Cookies', ingredients=['2 1/4 cups all-purpose flour'
Expand Down
11 changes: 6 additions & 5 deletions genai/controlled_generation/ctrlgen_with_enum_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
def generate_content() -> str:
# [START googlegenaisdk_ctrlgen_with_enum_schema]
from google import genai
from google.genai.types import GenerateContentConfig, HttpOptions

client = genai.Client()
client = genai.Client(http_options=HttpOptions(api_version="v1"))
response = client.models.generate_content(
model="gemini-2.0-flash-001",
contents="What type of instrument is an oboe?",
config={
"response_mime_type": "text/x.enum",
"response_schema": {
config=GenerateContentConfig(
response_mime_type="text/x.enum",
response_schema={
"type": "STRING",
"enum": ["Percussion", "String", "Woodwind", "Brass", "Keyboard"],
},
},
),
)

print(response.text)
Expand Down
14 changes: 8 additions & 6 deletions genai/controlled_generation/ctrlgen_with_nested_class_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

def generate_content() -> str:
# [START googlegenaisdk_ctrlgen_with_nested_class_schema]
import enum

from google import genai
from google.genai.types import GenerateContentConfig, HttpOptions

import enum
from pydantic import BaseModel

class Grade(enum.Enum):
Expand All @@ -32,14 +34,14 @@ class Recipe(BaseModel):
recipe_name: str
rating: Grade

client = genai.Client()
client = genai.Client(http_options=HttpOptions(api_version="v1"))
response = client.models.generate_content(
model="gemini-2.0-flash-001",
contents="List about 10 home-baked cookies and give them grades based on tastiness.",
config={
"response_mime_type": "application/json",
"response_schema": list[Recipe],
},
config=GenerateContentConfig(
response_mime_type="application/json",
response_schema=list[Recipe],
),
)

print(response.text)
Expand Down
11 changes: 6 additions & 5 deletions genai/controlled_generation/ctrlgen_with_nullable_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
def generate_content() -> str:
# [START googlegenaisdk_ctrlgen_with_nullable_schema]
from google import genai
from google.genai.types import GenerateContentConfig, HttpOptions

response_schema = {
"type": "OBJECT",
Expand Down Expand Up @@ -48,14 +49,14 @@ def generate_content() -> str:
Finally, Saturday rounds off the week with sunny skies, a temperature of 80°F, and a humidity level of 40%. Winds will be gentle at 8 km/h.
"""

client = genai.Client()
client = genai.Client(http_options=HttpOptions(api_version="v1"))
response = client.models.generate_content(
model="gemini-2.0-flash-001",
contents=prompt,
config={
"response_mime_type": "application/json",
"response_schema": response_schema,
},
config=GenerateContentConfig(
response_mime_type="application/json",
response_schema=response_schema,
),
)

print(response.text)
Expand Down
1 change: 1 addition & 0 deletions genai/controlled_generation/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-genai==1.2.0
2 changes: 1 addition & 1 deletion genai/text_generation/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
google-genai==1.0.0
google-genai==1.2.0
6 changes: 3 additions & 3 deletions genai/text_generation/textgen_async_with_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
async def generate_content() -> str:
# [START googlegenaisdk_textgen_async_with_txt]
from google import genai
from google.genai.types import GenerateContentConfig
from google.genai.types import GenerateContentConfig, HttpOptions

client = genai.Client()
model_id = "gemini-2.0-flash-exp"
client = genai.Client(http_options=HttpOptions(api_version="v1"))
model_id = "gemini-2.0-flash-001"

response = await client.aio.models.generate_content(
model=model_id,
Expand Down
5 changes: 3 additions & 2 deletions genai/text_generation/textgen_chat_stream_with_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
def generate_content() -> str:
# [START googlegenaisdk_textgen_chat_stream_with_txt]
from google import genai
from google.genai.types import HttpOptions

client = genai.Client(http_options={'api_version': 'v1'})
client = genai.Client(http_options=HttpOptions(api_version="v1"))
chat = client.chats.create(model="gemini-2.0-flash-001")
response_text = ""

for chunk in chat.send_message_stream("Why is the sky blue?"):
print(chunk.text)
print(chunk.text, end="")
response_text += chunk.text
# Example response:
# The
Expand Down
4 changes: 2 additions & 2 deletions genai/text_generation/textgen_chat_with_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
def generate_content() -> str:
# [START googlegenaisdk_textgen_chat_with_txt]
from google import genai
from google.genai.types import Content, Part
from google.genai.types import Content, HttpOptions, Part

client = genai.Client(http_options={'api_version': 'v1'})
client = genai.Client(http_options=HttpOptions(api_version="v1"))
chat = client.chats.create(
model="gemini-2.0-flash-001",
history=[
Expand Down
8 changes: 4 additions & 4 deletions genai/text_generation/textgen_code_with_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
def generate_content() -> str:
# [START googlegenaisdk_textgen_code_with_pdf]
from google import genai
from google.genai import types
from google.genai.types import HttpOptions, Part

client = genai.Client()
model_id = "gemini-2.0-flash-exp"
client = genai.Client(http_options=HttpOptions(api_version="v1"))
model_id = "gemini-2.0-flash-001"

python_code = types.Part.from_uri(
python_code = Part.from_uri(
file_uri="https://storage.googleapis.com/cloud-samples-data/generative-ai/text/inefficient_fibonacci_series_python_code.pdf",
mime_type="application/pdf",
)
Expand Down
6 changes: 3 additions & 3 deletions genai/text_generation/textgen_config_with_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
def generate_content() -> str:
# [START googlegenaisdk_textgen_config_with_txt]
from google import genai
from google.genai import types
from google.genai.types import GenerateContentConfig, HttpOptions

client = genai.Client(http_options={'api_version': 'v1'})
client = genai.Client(http_options=HttpOptions(api_version="v1"))
response = client.models.generate_content(
model="gemini-2.0-flash-001",
contents="Why is the sky blue?",
# See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.GenerateContentConfig
config=types.GenerateContentConfig(
config=GenerateContentConfig(
temperature=0,
candidate_count=1,
response_mime_type="application/json",
Expand Down
6 changes: 3 additions & 3 deletions genai/text_generation/textgen_sys_instr_with_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
def generate_content() -> str:
# [START googlegenaisdk_textgen_sys_instr_with_txt]
from google import genai
from google.genai import types
from google.genai.types import GenerateContentConfig, HttpOptions

client = genai.Client(http_options={'api_version': 'v1'})
client = genai.Client(http_options=HttpOptions(api_version="v1"))
response = client.models.generate_content(
model="gemini-2.0-flash-001",
contents="Why is the sky blue?",
config=types.GenerateContentConfig(
config=GenerateContentConfig(
system_instruction=[
"You're a language translator.",
"Your mission is to translate text in English to French.",
Expand Down
4 changes: 2 additions & 2 deletions genai/text_generation/textgen_transcript_with_gcs_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
def generate_content() -> str:
# [START googlegenaisdk_textgen_transcript_with_gcs_audio]
from google import genai
from google.genai.types import Part
from google.genai.types import HttpOptions, Part

client = genai.Client(http_options={'api_version': 'v1'})
client = genai.Client(http_options=HttpOptions(api_version="v1"))
prompt = """
Transcribe the interview, in the format of timecode, speaker, caption.
Use speaker A, speaker B, etc. to identify speakers.
Expand Down
4 changes: 2 additions & 2 deletions genai/text_generation/textgen_with_gcs_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
def generate_content() -> str:
# [START googlegenaisdk_textgen_with_gcs_audio]
from google import genai
from google.genai.types import Part
from google.genai.types import HttpOptions, Part

client = genai.Client(http_options={'api_version': 'v1'})
client = genai.Client(http_options=HttpOptions(api_version="v1"))
prompt = """
Provide the summary of the audio file.
Summarize the main points of the audio concisely.
Expand Down
Loading

0 comments on commit d3c36d2

Please sign in to comment.