|
| 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() |
0 commit comments