|
| 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 | +'use strict'; |
| 16 | + |
| 17 | +// [START googlegenaisdk_thinking_includethoughts_with_txt] |
| 18 | +const {GoogleGenAI} = require('@google/genai'); |
| 19 | + |
| 20 | +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; |
| 21 | +const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; |
| 22 | + |
| 23 | +async function generateWithThoughts( |
| 24 | + projectId = GOOGLE_CLOUD_PROJECT, |
| 25 | + location = GOOGLE_CLOUD_LOCATION |
| 26 | +) { |
| 27 | + const client = new GoogleGenAI({ |
| 28 | + vertexai: true, |
| 29 | + project: projectId, |
| 30 | + location: location, |
| 31 | + }); |
| 32 | + |
| 33 | + const response = await client.models.generateContent({ |
| 34 | + model: 'gemini-2.5-pro', |
| 35 | + contents: 'solve x^2 + 4x + 4 = 0', |
| 36 | + config: { |
| 37 | + thinkingConfig: { |
| 38 | + includeThoughts: true, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + console.log(response.text); |
| 44 | + // Example Response: |
| 45 | + // Okay, let's solve the quadratic equation x² + 4x + 4 = 0. |
| 46 | + // ... |
| 47 | + // **Answer:** |
| 48 | + // The solution to the equation x² + 4x + 4 = 0 is x = -2. This is a repeated root (or a root with multiplicity 2). |
| 49 | + |
| 50 | + for (const part of response.candidates[0].content.parts) { |
| 51 | + if (part && part.thought) { |
| 52 | + console.log(part.text); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Example Response: |
| 57 | + // **My Thought Process for Solving the Quadratic Equation** |
| 58 | + // |
| 59 | + // Alright, let's break down this quadratic, x² + 4x + 4 = 0. First things first: |
| 60 | + // it's a quadratic; the x² term gives it away, and we know the general form is |
| 61 | + // ax² + bx + c = 0. |
| 62 | + // |
| 63 | + // So, let's identify the coefficients: a = 1, b = 4, and c = 4. Now, what's the |
| 64 | + // most efficient path to the solution? My gut tells me to try factoring; it's |
| 65 | + // often the fastest route if it works. If that fails, I'll default to the quadratic |
| 66 | + // formula, which is foolproof. Completing the square? It's good for deriving the |
| 67 | + // formula or when factoring is difficult, but not usually my first choice for |
| 68 | + // direct solving, but it can't hurt to keep it as an option. |
| 69 | + // |
| 70 | + // Factoring, then. I need to find two numbers that multiply to 'c' (4) and add |
| 71 | + // up to 'b' (4). Let's see... 1 and 4 don't work (add up to 5). 2 and 2? Bingo! |
| 72 | + // They multiply to 4 and add up to 4. This means I can rewrite the equation as |
| 73 | + // (x + 2)(x + 2) = 0, or more concisely, (x + 2)² = 0. Solving for x is now |
| 74 | + // trivial: x + 2 = 0, thus x = -2. |
| 75 | + // |
| 76 | + // Okay, just to be absolutely certain, I'll run the quadratic formula just to |
| 77 | + // double-check. x = [-b ± √(b² - 4ac)] / 2a. Plugging in the values, x = [-4 ± |
| 78 | + // √(4² - 4 * 1 * 4)] / (2 * 1). That simplifies to x = [-4 ± √0] / 2. So, x = |
| 79 | + // -2 again – a repeated root. Nice. |
| 80 | + // |
| 81 | + // Now, let's check via completing the square. Starting from the same equation, |
| 82 | + // (x² + 4x) = -4. Take half of the b-value (4/2 = 2), square it (2² = 4), and |
| 83 | + // add it to both sides, so x² + 4x + 4 = -4 + 4. Which simplifies into (x + 2)² |
| 84 | + // = 0. The square root on both sides gives us x + 2 = 0, therefore x = -2, as |
| 85 | + // expected. |
| 86 | + // |
| 87 | + // Always, *always* confirm! Let's substitute x = -2 back into the original |
| 88 | + // equation: (-2)² + 4(-2) + 4 = 0. That's 4 - 8 + 4 = 0. It checks out. |
| 89 | + // |
| 90 | + // Conclusion: the solution is x = -2. Confirmed. |
| 91 | + |
| 92 | + return response.text; |
| 93 | +} |
| 94 | +// [END googlegenaisdk_thinking_includethoughts_with_txt] |
| 95 | + |
| 96 | +module.exports = { |
| 97 | + generateWithThoughts, |
| 98 | +}; |
0 commit comments