Skip to content

Commit 2135347

Browse files
authored
chore(genai): Update Chat and Code Exec Samples for 1.3.0 SDK Update (#13188)
1 parent 95eb30b commit 2135347

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-genai==1.2.0
1+
google-genai==1.3.0

genai/text_generation/textgen_chat_with_txt.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,19 @@
1616
def generate_content() -> str:
1717
# [START googlegenaisdk_textgen_chat_with_txt]
1818
from google import genai
19-
from google.genai.types import Content, HttpOptions, Part
19+
from google.genai.types import HttpOptions, ModelContent, Part, UserContent
2020

2121
client = genai.Client(http_options=HttpOptions(api_version="v1"))
2222
chat = client.chats.create(
2323
model="gemini-2.0-flash-001",
2424
history=[
25-
Content(parts=[Part(text="Hello")], role="user"),
26-
Content(
25+
UserContent(parts=[Part(text="Hello")]),
26+
ModelContent(
2727
parts=[Part(text="Great to meet you. What would you like to know?")],
28-
role="model",
2928
),
3029
],
3130
)
32-
response = chat.send_message("tell me a story")
31+
response = chat.send_message("Tell me a story.")
3332
print(response.text)
3433
# Example response:
3534
# Okay, here's a story for you:

genai/tools/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
google-genai==1.2.0
1+
google-genai==1.3.0
22
# PIl is required for tools_code_execution_with_txt_img.py
33
pillow==11.1.0

genai/tools/tools_code_exec_with_txt.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,32 @@ def generate_content() -> str:
3535
temperature=0,
3636
),
3737
)
38-
for part in response.candidates[0].content.parts:
39-
if part.executable_code:
40-
print(part.executable_code)
41-
if part.code_execution_result:
42-
print(part.code_execution_result)
38+
print("# Code:")
39+
print(response.executable_code)
40+
print("# Outcome:")
41+
print(response.code_execution_result)
42+
4343
# Example response:
44-
# code='...' language='PYTHON'
45-
# outcome='OUTCOME_OK' output='The 20th Fibonacci number is: 6765\n'
46-
# code='...' language='PYTHON'
47-
# outcome='OUTCOME_OK' output='Lower Palindrome: 6666\nHigher Palindrome: 6776\nNearest Palindrome to 6765: 6776\n'
44+
# # Code:
45+
# def fibonacci(n):
46+
# if n <= 0:
47+
# return 0
48+
# elif n == 1:
49+
# return 1
50+
# else:
51+
# a, b = 0, 1
52+
# for _ in range(2, n + 1):
53+
# a, b = b, a + b
54+
# return b
55+
#
56+
# fib_20 = fibonacci(20)
57+
# print(f'{fib_20=}')
58+
#
59+
# # Outcome:
60+
# fib_20=6765
4861

4962
# [END googlegenaisdk_tools_code_exec_with_txt]
50-
return str(response.candidates[0].content.parts)
63+
return response.executable_code
5164

5265

5366
if __name__ == "__main__":

genai/tools/tools_code_exec_with_txt_local_img.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,24 @@ def generate_content() -> GenerateContentResponse:
5555
)
5656

5757
print("# Code:")
58-
for part in response.candidates[0].content.parts:
59-
if part.executable_code:
60-
print(part.executable_code)
61-
58+
print(response.executable_code)
6259
print("# Outcome:")
63-
for part in response.candidates[0].content.parts:
64-
if part.code_execution_result:
65-
print(part.code_execution_result)
60+
print(response.code_execution_result)
6661

67-
# Example response:
6862
# # Code:
69-
# code='\nimport random\n\ndef monty_hall_simulation(num_trials):\n
70-
# """Simulates the Monty Hall problem and returns the win rates for switching and not switching."""\n\n
71-
# wins_switching = 0\n wins_not_switching = 0\n\n for _ in range(num_trials):\n # 1. Set up the game:\n
72-
# - Randomly place the car behind one of the three doors.\n car_door = random.randint(0, 2)\n
63+
# import random
64+
65+
# def monty_hall_simulation(num_trials=1000):
66+
# wins_switching = 0
67+
# wins_not_switching = 0
68+
69+
# for _ in range(num_trials):
70+
# # Randomly assign the car to a door (0, 1, or 2)
71+
# car_door = random.randint(0, 2)
7372
# ...
7473
# # Outcome:
75-
# outcome=<Outcome.OUTCOME_OK: 'OUTCOME_OK'> output='Win percentage when switching: 65.90%\nWin percentage when not switching: 34.10%\n'
74+
# Win percentage when switching: 65.50%
75+
# Win percentage when not switching: 34.50%
7676
# [END googlegenaisdk_tools_code_exec_with_txt_local_img]
7777
return response
7878

0 commit comments

Comments
 (0)