|
| 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