Skip to content

Commit 01a285c

Browse files
docs(genai): Add Text Generation with Thinking Model Sample (GoogleCloudPlatform#13176)
Co-authored-by: Sampath Kumar <sampathm@google.com>
1 parent 1e22407 commit 01a285c

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

genai/text_generation/test_text_generation_examples.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import textgen_with_txt_stream
3636
import textgen_with_video
3737
import textgen_with_youtube_video
38+
import thinking_textgen_with_txt
3839

3940

4041
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
@@ -83,6 +84,11 @@ def test_textgen_with_txt_img() -> None:
8384
assert response
8485

8586

87+
def test_textgen_with_txt_thinking() -> None:
88+
response = thinking_textgen_with_txt.generate_content()
89+
assert response
90+
91+
8692
def test_textgen_with_multi_img() -> None:
8793
response = textgen_with_multi_img.generate_content()
8894
assert response
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_textgen_with_txt]
18+
from google import genai
19+
from google.genai.types import HttpOptions
20+
21+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
22+
response = client.models.generate_content(
23+
model="gemini-2.0-flash-thinking-exp-01-21",
24+
contents="solve x^2 + 4x + 4 = 0",
25+
)
26+
print(response.text)
27+
# Example response:
28+
# To solve the equation x^2 + 4x + 4 = 0, we can use several methods.
29+
#
30+
# **Method 1: Factoring**
31+
#
32+
# We look for two numbers that multiply to 4 (the constant term) and add to 4 (the coefficient of the x term).
33+
# These two numbers are 2 and 2 because 2 * 2 = 4 and 2 + 2 = 4.
34+
# Therefore, we can factor the quadratic expression as:
35+
# (x + 2)(x + 2) = 0
36+
# This can also be written as:
37+
# (x + 2)^2 = 0
38+
#
39+
# To solve for x, we set the factor (x + 2) equal to zero:
40+
# x + 2 = 0
41+
# Subtract 2 from both sides:
42+
# x = -2
43+
#
44+
# **Method 2: Quadratic Formula**
45+
#
46+
# The quadratic formula for an equation of the form ax^2 + bx + c = 0 is given by:
47+
# x = [-b ± sqrt(b^2 - 4ac)] / (2a)
48+
#
49+
# ...
50+
#
51+
#
52+
# All three methods yield the same solution, x = -2.
53+
# This is a repeated root, which is expected since the discriminant (b^2 - 4ac) is 0.
54+
#
55+
# To check our solution, we substitute x = -2 back into the original equation:
56+
# (-2)^2 + 4(-2) + 4 = 4 - 8 + 4 = 0
57+
# The equation holds true, so our solution is correct.
58+
59+
# Final Answer: The final answer is $\boxed{-2}$
60+
61+
# [END googlegenaisdk_thinking_textgen_with_txt]
62+
return response.text
63+
64+
65+
if __name__ == "__main__":
66+
generate_content()

0 commit comments

Comments
 (0)