|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package genai.thinking; |
| 18 | + |
| 19 | +// [START googlegenaisdk_thinking_includethoughts_with_txt] |
| 20 | + |
| 21 | +import com.google.genai.Client; |
| 22 | +import com.google.genai.types.Candidate; |
| 23 | +import com.google.genai.types.Content; |
| 24 | +import com.google.genai.types.GenerateContentConfig; |
| 25 | +import com.google.genai.types.GenerateContentResponse; |
| 26 | +import com.google.genai.types.HttpOptions; |
| 27 | +import com.google.genai.types.ThinkingConfig; |
| 28 | + |
| 29 | +public class ThinkingIncludeThoughtsWithTxt { |
| 30 | + |
| 31 | + public static void main(String[] args) { |
| 32 | + // TODO(developer): Replace these variables before running the sample. |
| 33 | + String modelId = "gemini-2.5-pro"; |
| 34 | + generateContent(modelId); |
| 35 | + } |
| 36 | + |
| 37 | + // Generates text including thoughts in the response |
| 38 | + public static String generateContent(String modelId) { |
| 39 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 40 | + // once, and can be reused for multiple requests. |
| 41 | + try (Client client = |
| 42 | + Client.builder() |
| 43 | + .location("global") |
| 44 | + .vertexAI(true) |
| 45 | + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) |
| 46 | + .build()) { |
| 47 | + |
| 48 | + GenerateContentConfig contentConfig = |
| 49 | + GenerateContentConfig.builder() |
| 50 | + .thinkingConfig(ThinkingConfig.builder().includeThoughts(true).build()) |
| 51 | + .build(); |
| 52 | + |
| 53 | + GenerateContentResponse response = |
| 54 | + client.models.generateContent(modelId, "solve x^2 + 4x + 4 = 0", contentConfig); |
| 55 | + |
| 56 | + System.out.println(response.text()); |
| 57 | + // Example response: |
| 58 | + // We can solve the equation x² + 4x + 4 = 0 using a couple of common methods. |
| 59 | + // |
| 60 | + // ### Method 1: Factoring (The Easiest Method for this Problem) |
| 61 | + // **Recognize the pattern:** The pattern for a perfect square trinomial |
| 62 | + // is a² + 2ab + b² = (a + b)². |
| 63 | + // ... |
| 64 | + // ### Final Answer: |
| 65 | + // The solution is **x = -2**. |
| 66 | + |
| 67 | + // Get parts of the response and print thoughts |
| 68 | + response |
| 69 | + .candidates() |
| 70 | + .flatMap(candidates -> candidates.stream().findFirst()) |
| 71 | + .flatMap(Candidate::content) |
| 72 | + .flatMap(Content::parts) |
| 73 | + .ifPresent( |
| 74 | + parts -> { |
| 75 | + parts.forEach( |
| 76 | + part -> { |
| 77 | + if (part.thought().orElse(false)) { |
| 78 | + part.text().ifPresent(System.out::println); |
| 79 | + } |
| 80 | + }); |
| 81 | + }); |
| 82 | + // Example response: |
| 83 | + // Alright, let's break down this quadratic equation, x² + 4x + 4 = 0. My initial thought is, |
| 84 | + // "classic quadratic." I'll need to find the values of 'x' that make this equation true. The |
| 85 | + // equation is in standard form, and since the coefficients are relatively small, I |
| 86 | + // immediately suspect that factoring might be the easiest route. It's worth checking. |
| 87 | + // |
| 88 | + // First, I assessed what I had. *a* is 1, *b* is 4, and *c* is 4. I consider my toolkit. |
| 89 | + // Factoring is the likely first choice, then I can use the quadratic formula as a backup, |
| 90 | + // because that ALWAYS works, and I could use graphing. However, for this, factoring seems the |
| 91 | + // cleanest approach. |
| 92 | + // |
| 93 | + // Okay, factoring. I need two numbers that multiply to *c* (which is 4) and add up to *b* |
| 94 | + // (also 4). I quickly run through the factor pairs of 4: (1, 4), (-1, -4), (2, 2), (-2, -2). |
| 95 | + // Aha! 2 and 2 fit the bill. They multiply to 4 *and* add up to 4. Therefore, I can rewrite |
| 96 | + // the equation as (x + 2)(x + 2) = 0. That simplifies to (x + 2)² = 0. Perfect square |
| 97 | + // trinomial – nice and tidy. Seeing that pattern from the outset can save a step or two. Now, |
| 98 | + // to solve for *x*: if (x + 2)² = 0, then x + 2 must equal 0. Therefore, x = -2. Done. |
| 99 | + // |
| 100 | + // But, for the sake of a full explanation, let's use the quadratic formula as a second |
| 101 | + // method. It's a reliable way to double-check the answer, plus it's good practice. I plug my |
| 102 | + // *a*, *b*, and *c* values into the formula: x = [-b ± √(b² - 4ac)] / (2a). That gives me x |
| 103 | + // = [-4 ± √(4² - 4 * 1 * 4)] / (2 * 1). Simplifying under the radical, I get x = [-4 ± √(16 - |
| 104 | + // 16)] / 2. So, x = [-4 ± √0] / 2. The square root of 0 is zero, which is very telling! When |
| 105 | + // the discriminant (b² - 4ac) is zero, you get one real solution, a repeated root. This means |
| 106 | + // x = -4 / 2, which simplifies to x = -2. Exactly the same as before. |
| 107 | + // |
| 108 | + // Therefore, the answer is x = -2. Factoring was the most straightforward route. For |
| 109 | + // completeness, I showed the solution via the quadratic formula, too. Both approaches lead to |
| 110 | + // the same single solution. This is a repeated root – a double root, if you will. |
| 111 | + // |
| 112 | + // And to be absolutely sure...let's check our answer! Substitute -2 back into the original |
| 113 | + // equation. (-2)² + 4(-2) + 4 = 4 - 8 + 4 = 0. Yep, 0 = 0. The solution is correct. |
| 114 | + return response.text(); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | +// [END googlegenaisdk_thinking_includethoughts_with_txt] |
0 commit comments