Skip to content

Commit 9c5a96d

Browse files
feat(genai): Add thinking feature examples (#13341)
* feat(genai): Add example for thinking feature - Created a new folder for thinking models - Add new thinking_budget example - Add new thinking include_thoughts example * Update genai/thinking/thinking_with_txt.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * feat(genai): update model name --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 11d0e0a commit 9c5a96d

8 files changed

+298
-0
lines changed

genai/text_generation/thinking_textgen_with_txt.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515

16+
# TODO: To deprecate this sample. Moving thinking samples to `thinking` folder.
1617
def generate_content() -> str:
1718
# [START googlegenaisdk_thinking_textgen_with_txt]
1819
from google import genai

genai/thinking/noxfile_config.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2021 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+
# http://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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.12"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}

genai/thinking/requirements-test.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
backoff==2.2.1
2+
google-api-core==2.19.0
3+
pytest==8.2.0
4+
pytest-asyncio==0.23.6

genai/thinking/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-genai==1.13.0
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
import os
15+
16+
import thinking_budget_with_txt
17+
import thinking_includethoughts_with_txt
18+
import thinking_with_txt
19+
20+
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
21+
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
22+
# The project name is included in the CICD pipeline
23+
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"
24+
25+
26+
def test_thinking_budget_with_txt() -> None:
27+
assert thinking_budget_with_txt.generate_content()
28+
29+
30+
def test_thinking_includethoughts_with_txt() -> None:
31+
assert thinking_includethoughts_with_txt.generate_content()
32+
33+
34+
def test_thinking_with_txt() -> None:
35+
assert thinking_with_txt.generate_content()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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_thinking_budget_with_txt]
18+
from google import genai
19+
from google.genai.types import GenerateContentConfig, ThinkingConfig
20+
21+
client = genai.Client()
22+
23+
response = client.models.generate_content(
24+
model="gemini-2.5-flash-preview-04-17",
25+
contents="solve x^2 + 4x + 4 = 0",
26+
config=GenerateContentConfig(
27+
thinking_config=ThinkingConfig(
28+
thinking_budget=1024, # Use `0` to turn off thinking
29+
)
30+
),
31+
)
32+
33+
print(response.text)
34+
# Example response:
35+
# To solve the equation $x^2 + 4x + 4 = 0$, you can use several methods:
36+
# **Method 1: Factoring**
37+
# 1. Look for two numbers that multiply to the constant term (4) and add up to the coefficient of the $x$ term (4).
38+
# 2. The numbers are 2 and 2 ($2 \times 2 = 4$ and $2 + 2 = 4$).
39+
# ...
40+
# ...
41+
# All three methods yield the same solution. This quadratic equation has exactly one distinct solution (a repeated root).
42+
# The solution is **x = -2**.
43+
44+
# Token count for `Thinking`
45+
print(response.usage_metadata.thoughts_token_count)
46+
# Example response:
47+
# 886
48+
49+
# Total token count
50+
print(response.usage_metadata.total_token_count)
51+
# Example response:
52+
# 1525
53+
# [END googlegenaisdk_thinking_budget_with_txt]
54+
return response.text
55+
56+
57+
if __name__ == "__main__":
58+
generate_content()
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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_thinking_includethoughts_with_txt]
18+
from google import genai
19+
from google.genai.types import GenerateContentConfig, ThinkingConfig
20+
21+
client = genai.Client()
22+
response = client.models.generate_content(
23+
model="gemini-2.5-pro-preview-05-06",
24+
contents="solve x^2 + 4x + 4 = 0",
25+
config=GenerateContentConfig(
26+
thinking_config=ThinkingConfig(include_thoughts=True)
27+
),
28+
)
29+
30+
print(response.text)
31+
# Example Response:
32+
# Okay, let's solve the quadratic equation x² + 4x + 4 = 0.
33+
# ...
34+
# **Answer:**
35+
# The solution to the equation x² + 4x + 4 = 0 is x = -2. This is a repeated root (or a root with multiplicity 2).
36+
37+
for part in response.candidates[0].content.parts:
38+
if part and part.thought: # show thoughts
39+
print(part.text)
40+
# Example Response:
41+
# **My Thought Process for Solving the Quadratic Equation**
42+
#
43+
# Alright, let's break down this quadratic, x² + 4x + 4 = 0. First things first:
44+
# it's a quadratic; the x² term gives it away, and we know the general form is
45+
# ax² + bx + c = 0.
46+
#
47+
# So, let's identify the coefficients: a = 1, b = 4, and c = 4. Now, what's the
48+
# most efficient path to the solution? My gut tells me to try factoring; it's
49+
# often the fastest route if it works. If that fails, I'll default to the quadratic
50+
# formula, which is foolproof. Completing the square? It's good for deriving the
51+
# formula or when factoring is difficult, but not usually my first choice for
52+
# direct solving, but it can't hurt to keep it as an option.
53+
#
54+
# Factoring, then. I need to find two numbers that multiply to 'c' (4) and add
55+
# up to 'b' (4). Let's see... 1 and 4 don't work (add up to 5). 2 and 2? Bingo!
56+
# They multiply to 4 and add up to 4. This means I can rewrite the equation as
57+
# (x + 2)(x + 2) = 0, or more concisely, (x + 2)² = 0. Solving for x is now
58+
# trivial: x + 2 = 0, thus x = -2.
59+
#
60+
# Okay, just to be absolutely certain, I'll run the quadratic formula just to
61+
# double-check. x = [-b ± √(b² - 4ac)] / 2a. Plugging in the values, x = [-4 ±
62+
# √(4² - 4 * 1 * 4)] / (2 * 1). That simplifies to x = [-4 ± √0] / 2. So, x =
63+
# -2 again – a repeated root. Nice.
64+
#
65+
# Now, let's check via completing the square. Starting from the same equation,
66+
# (x² + 4x) = -4. Take half of the b-value (4/2 = 2), square it (2² = 4), and
67+
# add it to both sides, so x² + 4x + 4 = -4 + 4. Which simplifies into (x + 2)²
68+
# = 0. The square root on both sides gives us x + 2 = 0, therefore x = -2, as
69+
# expected.
70+
#
71+
# Always, *always* confirm! Let's substitute x = -2 back into the original
72+
# equation: (-2)² + 4(-2) + 4 = 0. That's 4 - 8 + 4 = 0. It checks out.
73+
#
74+
# Conclusion: the solution is x = -2. Confirmed.
75+
# [END googlegenaisdk_thinking_includethoughts_with_txt]
76+
return response.text
77+
78+
79+
if __name__ == "__main__":
80+
generate_content()

genai/thinking/thinking_with_txt.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_thinking_with_txt]
18+
from google import genai
19+
20+
client = genai.Client()
21+
response = client.models.generate_content(
22+
model="gemini-2.5-pro-preview-05-06",
23+
contents="solve x^2 + 4x + 4 = 0",
24+
)
25+
print(response.text)
26+
# Example Response:
27+
# Okay, let's solve the quadratic equation x² + 4x + 4 = 0.
28+
#
29+
# We can solve this equation by factoring, using the quadratic formula, or by recognizing it as a perfect square trinomial.
30+
#
31+
# **Method 1: Factoring**
32+
#
33+
# 1. We need two numbers that multiply to the constant term (4) and add up to the coefficient of the x term (4).
34+
# 2. The numbers 2 and 2 satisfy these conditions: 2 * 2 = 4 and 2 + 2 = 4.
35+
# 3. So, we can factor the quadratic as:
36+
# (x + 2)(x + 2) = 0
37+
# or
38+
# (x + 2)² = 0
39+
# 4. For the product to be zero, the factor must be zero:
40+
# x + 2 = 0
41+
# 5. Solve for x:
42+
# x = -2
43+
#
44+
# **Method 2: Quadratic Formula**
45+
#
46+
# The quadratic formula for an equation ax² + bx + c = 0 is:
47+
# x = [-b ± sqrt(b² - 4ac)] / (2a)
48+
#
49+
# 1. In our equation x² + 4x + 4 = 0, we have a=1, b=4, and c=4.
50+
# 2. Substitute these values into the formula:
51+
# x = [-4 ± sqrt(4² - 4 * 1 * 4)] / (2 * 1)
52+
# x = [-4 ± sqrt(16 - 16)] / 2
53+
# x = [-4 ± sqrt(0)] / 2
54+
# x = [-4 ± 0] / 2
55+
# x = -4 / 2
56+
# x = -2
57+
#
58+
# **Method 3: Perfect Square Trinomial**
59+
#
60+
# 1. Notice that the expression x² + 4x + 4 fits the pattern of a perfect square trinomial: a² + 2ab + b², where a=x and b=2.
61+
# 2. We can rewrite the equation as:
62+
# (x + 2)² = 0
63+
# 3. Take the square root of both sides:
64+
# x + 2 = 0
65+
# 4. Solve for x:
66+
# x = -2
67+
#
68+
# All methods lead to the same solution.
69+
#
70+
# **Answer:**
71+
# The solution to the equation x² + 4x + 4 = 0 is x = -2. This is a repeated root (or a root with multiplicity 2).
72+
# [END googlegenaisdk_thinking_with_txt]
73+
return response.text
74+
75+
76+
if __name__ == "__main__":
77+
generate_content()

0 commit comments

Comments
 (0)