Skip to content

Commit 8ab5755

Browse files
Text generation(genai): Add new code samples (#13127)
* Text generation(genai): Add new code samples * Text generation(genai): use latest SDK Update to Youtube example region tag * docs(genai): Update textgen_with_youtube_video.py sample region tags
1 parent 81292a8 commit 8ab5755

20 files changed

+275
-54
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-genai==0.7.0
1+
google-genai==0.8.0
1.75 MB
Binary file not shown.

genai/text_generation/test_text_generation.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@
1414

1515
import os
1616

17+
import textgen_async_with_txt
18+
import textgen_chat_stream_with_txt
1719
import textgen_chat_with_txt
18-
import textgen_chat_with_txt_stream
20+
import textgen_code_with_pdf
1921
import textgen_config_with_txt
2022
import textgen_sys_instr_with_txt
2123
import textgen_transcript_with_gcs_audio
2224
import textgen_with_gcs_audio
25+
import textgen_with_local_video
2326
import textgen_with_multi_img
2427
import textgen_with_multi_local_img
2528
import textgen_with_mute_video
2629
import textgen_with_txt
2730
import textgen_with_txt_img
2831
import textgen_with_txt_stream
2932
import textgen_with_video
33+
import textgen_with_youtube_video
3034

3135

3236
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
@@ -51,7 +55,7 @@ def test_textgen_chat_with_txt() -> None:
5155

5256

5357
def test_textgen_chat_with_txt_stream() -> None:
54-
response = textgen_chat_with_txt_stream.generate_content()
58+
response = textgen_chat_stream_with_txt.generate_content()
5559
assert response
5660

5761

@@ -101,3 +105,23 @@ def test_textgen_transcript_with_gcs_audio() -> None:
101105
def test_textgen_with_video() -> None:
102106
response = textgen_with_video.generate_content()
103107
assert response
108+
109+
110+
def test_textgen_async_with_txt() -> None:
111+
response = textgen_async_with_txt.generate_content()
112+
assert response
113+
114+
115+
def test_textgen_with_local_video() -> None:
116+
response = textgen_with_local_video.generate_content()
117+
assert response
118+
119+
120+
def test_textgen_with_youtube_video() -> None:
121+
response = textgen_with_youtube_video.generate_content()
122+
assert response
123+
124+
125+
def test_textgen_code_with_pdf() -> None:
126+
response = textgen_code_with_pdf.generate_content()
127+
assert response
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import asyncio
16+
17+
18+
async def generate_content() -> str:
19+
# [START googlegenaisdk_textgen_async_with_txt]
20+
from google import genai
21+
from google.genai.types import GenerateContentConfig
22+
23+
client = genai.Client()
24+
model_id = "gemini-2.0-flash-exp"
25+
26+
response = await client.aio.models.generate_content(
27+
model=model_id,
28+
contents="Compose a song about the adventures of a time-traveling squirrel.",
29+
config=GenerateContentConfig(
30+
response_modalities=["TEXT"],
31+
),
32+
)
33+
34+
print(response.text)
35+
# Example response:
36+
# (Verse 1)
37+
# Sammy the squirrel, a furry little friend
38+
# Had a knack for adventure, beyond all comprehend
39+
40+
# [END googlegenaisdk_textgen_async_with_txt]
41+
return response.text
42+
43+
44+
if __name__ == "__main__":
45+
asyncio.run(generate_content())

genai/text_generation/textgen_chat_with_txt_stream.py renamed to genai/text_generation/textgen_chat_stream_with_txt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
def generate_content() -> str:
17-
# [START googlegenaisdk_textgen_chat_with_txt_stream]
17+
# [START googlegenaisdk_textgen_chat_stream_with_txt]
1818
from google import genai
1919

2020
client = genai.Client()
@@ -29,7 +29,7 @@ def generate_content() -> str:
2929
# sky appears blue due to a phenomenon called **Rayleigh scattering**. Here's
3030
# a breakdown of why:
3131
# ...
32-
# [END googlegenaisdk_textgen_chat_with_txt_stream]
32+
# [END googlegenaisdk_textgen_chat_stream_with_txt]
3333
return response_text
3434

3535

genai/text_generation/textgen_chat_with_txt.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@ def generate_content() -> str:
2222
chat = client.chats.create(
2323
model="gemini-2.0-flash-001",
2424
history=[
25-
Content(
26-
parts=[Part(text="Hello")],
27-
role="user"
28-
),
25+
Content(parts=[Part(text="Hello")], role="user"),
2926
Content(
3027
parts=[Part(text="Great to meet you. What would you like to know?")],
31-
role="model"
32-
)
33-
]
28+
role="model",
29+
),
30+
],
3431
)
3532
response = chat.send_message("tell me a story")
3633
print(response.text)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# !This sample works with Google Cloud Vertex AI API only.
16+
17+
18+
def generate_content() -> str:
19+
# [START googlegenaisdk_textgen_code_with_pdf]
20+
from google import genai
21+
from google.genai import types
22+
23+
client = genai.Client()
24+
model_id = "gemini-2.0-flash-exp"
25+
26+
python_code = types.Part.from_uri(
27+
file_uri="https://storage.googleapis.com/cloud-samples-data/generative-ai/text/inefficient_fibonacci_series_python_code.pdf",
28+
mime_type="application/pdf",
29+
)
30+
31+
response = client.models.generate_content(
32+
model=model_id,
33+
contents=[
34+
python_code,
35+
"Convert this python code to use Google Python Style Guide.",
36+
],
37+
)
38+
39+
print(response.text)
40+
# Example response:
41+
# ```python
42+
# def fibonacci(n: int) -> list[int] | str:
43+
# ...
44+
# [END googlegenaisdk_textgen_code_with_pdf]
45+
return response.text
46+
47+
48+
if __name__ == "__main__":
49+
generate_content()

genai/text_generation/textgen_config_with_txt.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@ def generate_content() -> str:
2626
config=types.GenerateContentConfig(
2727
temperature=0,
2828
candidate_count=1,
29-
response_mime_type="application/json"
30-
)
29+
response_mime_type="application/json",
30+
top_p=0.95,
31+
top_k=20,
32+
seed=5,
33+
max_output_tokens=100,
34+
stop_sequences=["STOP!"],
35+
presence_penalty=0.0,
36+
frequency_penalty=0.0,
37+
),
3138
)
3239
print(response.text)
3340
# Example response:

genai/text_generation/textgen_sys_instr_with_txt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def generate_content() -> str:
2525
config=types.GenerateContentConfig(
2626
system_instruction=[
2727
"You're a language translator.",
28-
"Your mission is to translate text in English to French."
28+
"Your mission is to translate text in English to French.",
2929
]
30-
)
30+
),
3131
)
3232
print(response.text)
3333
# Example response:

genai/text_generation/textgen_transcript_with_gcs_audio.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def generate_content() -> str:
3030
prompt,
3131
Part.from_uri(
3232
file_uri="gs://cloud-samples-data/generative-ai/audio/pixel.mp3",
33-
mime_type="audio/mpeg"
34-
)
35-
]
33+
mime_type="audio/mpeg",
34+
),
35+
],
3636
)
3737
print(response.text)
3838
# Example response:

0 commit comments

Comments
 (0)