Skip to content

Commit 5d4e4f3

Browse files
feat(genai): add python samples for text generation
1 parent 6afd47e commit 5d4e4f3

File tree

8 files changed

+244
-0
lines changed

8 files changed

+244
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-api-core==2.24.0
2+
pytest==8.2.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-genai==0.6.0
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2025 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 textgen_with_txt
16+
import textgen_with_txt_img
17+
import textgen_with_txt_stream
18+
import textgen_chat_with_txt
19+
import textgen_chat_with_txt_stream
20+
21+
22+
def test_textgen_with_txt() -> None:
23+
response = textgen_with_txt.generate_content()
24+
assert response
25+
26+
27+
def test_textgen_with_txt_img() -> None:
28+
response = textgen_with_txt_img.generate_content()
29+
assert response
30+
31+
32+
def test_textgen_with_txt_stream() -> None:
33+
response = textgen_with_txt_stream.generate_content()
34+
assert response
35+
36+
37+
def test_textgen_chat_with_txt() -> None:
38+
response = textgen_chat_with_txt.generate_content()
39+
assert response
40+
41+
42+
def test_textgen_chat_with_txt_stream() -> None:
43+
response = textgen_chat_with_txt_stream.generate_content()
44+
assert response
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2025 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+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_textgen_chat_with_txt]
18+
from google import genai
19+
from google.genai.types import Content, Part
20+
21+
client = genai.Client()
22+
chat = client.chats.create(
23+
model="gemini-2.0-flash-001",
24+
history=[
25+
Content(
26+
parts=[Part(text="Hello")],
27+
role="user"
28+
),
29+
Content(
30+
parts=[Part(text="Great to meet you. What would you like to know?")],
31+
role="model"
32+
)
33+
]
34+
)
35+
response = chat.send_message("tell me a story")
36+
print(response.text)
37+
# Example response:
38+
# Okay, here's a story for you:
39+
# ...
40+
# [END googlegenaisdk_textgen_chat_with_txt]
41+
return response.text
42+
43+
44+
if __name__ == "__main__":
45+
generate_content()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2025 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+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_textgen_chat_with_txt_stream]
18+
from google import genai
19+
20+
client = genai.Client()
21+
chat = client.chats.create(model="gemini-2.0-flash-001")
22+
response_text = ""
23+
24+
for chunk in chat.send_message_stream("Why is the sky blue?"):
25+
print(chunk.text)
26+
response_text += chunk.text
27+
# Example response:
28+
# The
29+
# sky appears blue due to a phenomenon called **Rayleigh scattering**. Here's
30+
# a breakdown of why:
31+
# ...
32+
# [END googlegenaisdk_textgen_chat_with_txt_stream]
33+
return response_text
34+
35+
36+
if __name__ == "__main__":
37+
generate_content()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2025 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+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_textgen_with_txt]
18+
from google import genai
19+
20+
client = genai.Client()
21+
response = client.models.generate_content(
22+
model="gemini-2.0-flash-001",
23+
contents="How does AI work?"
24+
)
25+
print(response.text)
26+
# Example response:
27+
# Okay, let's break down how AI works. It's a broad field, so I'll focus on the ...
28+
#
29+
# Here's a simplified overview:
30+
# ...
31+
# [END googlegenaisdk_textgen_with_txt]
32+
return response.text
33+
34+
35+
if __name__ == "__main__":
36+
generate_content()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2025 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+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_textgen_with_txt_img]
18+
from google import genai
19+
from google.genai.types import Part
20+
21+
client = genai.Client()
22+
response = client.models.generate_content(
23+
model="gemini-2.0-flash-001",
24+
contents=[
25+
"What is shown in this image?",
26+
Part.from_uri(
27+
file_uri="gs://cloud-samples-data/generative-ai/image/scones.jpg",
28+
mime_type="image/jpeg"
29+
)
30+
]
31+
)
32+
print(response.text)
33+
# Example response:
34+
# The image shows a flat lay of blueberry scones arranged on parchment paper. There are ...
35+
# [END googlegenaisdk_textgen_with_txt_img]
36+
return response.text
37+
38+
39+
if __name__ == "__main__":
40+
generate_content()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2025 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+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_textgen_with_txt_stream]
18+
from google import genai
19+
20+
client = genai.Client()
21+
response_text = ""
22+
23+
for chunk in client.models.generate_content_stream(
24+
model="gemini-2.0-flash-001",
25+
contents="Why is the sky blue?"
26+
):
27+
print(chunk.text)
28+
response_text += chunk.text
29+
# Example response:
30+
# The
31+
# sky appears blue due to a phenomenon called **Rayleigh scattering**. Here's
32+
# a breakdown of why:
33+
# ...
34+
# [END googlegenaisdk_textgen_with_txt_stream]
35+
return response_text
36+
37+
38+
if __name__ == "__main__":
39+
generate_content()

0 commit comments

Comments
 (0)