diff --git a/genai/test/thinking-budget-with-txt.test.js b/genai/test/thinking-budget-with-txt.test.js new file mode 100644 index 0000000000..5daaa2997f --- /dev/null +++ b/genai/test/thinking-budget-with-txt.test.js @@ -0,0 +1,29 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const projectId = process.env.CAIP_PROJECT_ID; +const sample = require('../thinking/thinking-budget-with-txt.js'); + +describe('thinking-budget-with-txt', () => { + it('should return Thought Process', async function () { + this.timeout(50000); + const output = await sample.generateWithThoughts(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/test/thinking-includethoughts-with-txt.test.js b/genai/test/thinking-includethoughts-with-txt.test.js new file mode 100644 index 0000000000..69a8a32800 --- /dev/null +++ b/genai/test/thinking-includethoughts-with-txt.test.js @@ -0,0 +1,29 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const projectId = process.env.CAIP_PROJECT_ID; +const sample = require('../thinking/thinking-includethoughts-with-txt.js'); + +describe('thinking-includethoughts-with-txt', () => { + it('should return Thought Process', async function () { + this.timeout(50000); + const output = await sample.generateWithThoughts(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/test/thinking-with-txt.test.js b/genai/test/thinking-with-txt.test.js new file mode 100644 index 0000000000..0ed201f75a --- /dev/null +++ b/genai/test/thinking-with-txt.test.js @@ -0,0 +1,29 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const projectId = process.env.CAIP_PROJECT_ID; +const sample = require('../thinking/thinking-with-txt.js'); + +describe('thinking-with-txt', () => { + it('should return Thought Process', async function () { + this.timeout(50000); + const output = await sample.generateWithThoughts(projectId); + assert(output.length > 0); + }); +}); diff --git a/genai/thinking/thinking-budget-with-txt.js b/genai/thinking/thinking-budget-with-txt.js new file mode 100644 index 0000000000..879ac159e2 --- /dev/null +++ b/genai/thinking/thinking-budget-with-txt.js @@ -0,0 +1,69 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +// [START googlegenaisdk_thinking_budget_with_txt] +const {GoogleGenAI} = require('@google/genai'); + +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; +const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; + +async function generateWithThoughts( + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const response = await client.models.generateContent({ + model: 'gemini-2.5-flash', + contents: 'solve x^2 + 4x + 4 = 0', + config: { + thinkingConfig: { + thinkingBudget: 1024, + }, + }, + }); + + console.log(response.text); + // Example response: + // To solve the equation $x^2 + 4x + 4 = 0$, you can use several methods: + // **Method 1: Factoring** + // 1. Look for two numbers that multiply to the constant term (4) and add up to the coefficient of the $x$ term (4). + // 2. The numbers are 2 and 2 ($2 \times 2 = 4$ and $2 + 2 = 4$). + // ... + // ... + // All three methods yield the same solution. This quadratic equation has exactly one distinct solution (a repeated root). + // The solution is **x = -2**. + + // Token count for `Thinking` + console.log(response.usageMetadata.thoughtsTokenCount); + // Example response: + // 886 + + // Total token count + console.log(response.usageMetadata.totalTokenCount); + // Example response: + // 1525 + return response.text; +} +// [END googlegenaisdk_thinking_budget_with_txt] + +module.exports = { + generateWithThoughts, +}; diff --git a/genai/thinking/thinking-includethoughts-with-txt.js b/genai/thinking/thinking-includethoughts-with-txt.js new file mode 100644 index 0000000000..ff0b5e7fef --- /dev/null +++ b/genai/thinking/thinking-includethoughts-with-txt.js @@ -0,0 +1,98 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +// [START googlegenaisdk_thinking_includethoughts_with_txt] +const {GoogleGenAI} = require('@google/genai'); + +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; +const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; + +async function generateWithThoughts( + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const response = await client.models.generateContent({ + model: 'gemini-2.5-pro', + contents: 'solve x^2 + 4x + 4 = 0', + config: { + thinkingConfig: { + includeThoughts: true, + }, + }, + }); + + console.log(response.text); + // Example Response: + // Okay, let's solve the quadratic equation x² + 4x + 4 = 0. + // ... + // **Answer:** + // The solution to the equation x² + 4x + 4 = 0 is x = -2. This is a repeated root (or a root with multiplicity 2). + + for (const part of response.candidates[0].content.parts) { + if (part && part.thought) { + console.log(part.text); + } + } + + // Example Response: + // **My Thought Process for Solving the Quadratic Equation** + // + // Alright, let's break down this quadratic, x² + 4x + 4 = 0. First things first: + // it's a quadratic; the x² term gives it away, and we know the general form is + // ax² + bx + c = 0. + // + // So, let's identify the coefficients: a = 1, b = 4, and c = 4. Now, what's the + // most efficient path to the solution? My gut tells me to try factoring; it's + // often the fastest route if it works. If that fails, I'll default to the quadratic + // formula, which is foolproof. Completing the square? It's good for deriving the + // formula or when factoring is difficult, but not usually my first choice for + // direct solving, but it can't hurt to keep it as an option. + // + // Factoring, then. I need to find two numbers that multiply to 'c' (4) and add + // up to 'b' (4). Let's see... 1 and 4 don't work (add up to 5). 2 and 2? Bingo! + // They multiply to 4 and add up to 4. This means I can rewrite the equation as + // (x + 2)(x + 2) = 0, or more concisely, (x + 2)² = 0. Solving for x is now + // trivial: x + 2 = 0, thus x = -2. + // + // Okay, just to be absolutely certain, I'll run the quadratic formula just to + // double-check. x = [-b ± √(b² - 4ac)] / 2a. Plugging in the values, x = [-4 ± + // √(4² - 4 * 1 * 4)] / (2 * 1). That simplifies to x = [-4 ± √0] / 2. So, x = + // -2 again – a repeated root. Nice. + // + // Now, let's check via completing the square. Starting from the same equation, + // (x² + 4x) = -4. Take half of the b-value (4/2 = 2), square it (2² = 4), and + // add it to both sides, so x² + 4x + 4 = -4 + 4. Which simplifies into (x + 2)² + // = 0. The square root on both sides gives us x + 2 = 0, therefore x = -2, as + // expected. + // + // Always, *always* confirm! Let's substitute x = -2 back into the original + // equation: (-2)² + 4(-2) + 4 = 0. That's 4 - 8 + 4 = 0. It checks out. + // + // Conclusion: the solution is x = -2. Confirmed. + + return response.text; +} +// [END googlegenaisdk_thinking_includethoughts_with_txt] + +module.exports = { + generateWithThoughts, +}; diff --git a/genai/thinking/thinking-with-txt.js b/genai/thinking/thinking-with-txt.js new file mode 100644 index 0000000000..babaa00320 --- /dev/null +++ b/genai/thinking/thinking-with-txt.js @@ -0,0 +1,98 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +// [START googlegenaisdk_thinking_with_txt] +const {GoogleGenAI} = require('@google/genai'); + +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; +const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; + +async function generateWithThoughts( + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const response = await client.models.generateContent({ + model: 'gemini-2.5-pro', + contents: 'solve x^2 + 4x + 4 = 0', + }); + + console.log(response.text); + + // Example Response: + // Okay, let's solve the quadratic equation x² + 4x + 4 = 0. + // + // We can solve this equation by factoring, using the quadratic formula, + // or by recognizing it as a perfect square trinomial. + // + // **Method 1: Factoring** + // + // 1. We need two numbers that multiply to the constant term (4) + // and add up to the coefficient of the x term (4). + // 2. The numbers 2 and 2 satisfy these conditions: 2 * 2 = 4 and 2 + 2 = 4. + // 3. So, we can factor the quadratic as: + // (x + 2)(x + 2) = 0 + // or + // (x + 2)² = 0 + // 4. For the product to be zero, the factor must be zero: + // x + 2 = 0 + // 5. Solve for x: + // x = -2 + // + // **Method 2: Quadratic Formula** + // + // The quadratic formula for an equation ax² + bx + c = 0 is: + // x = [-b ± sqrt(b² - 4ac)] / (2a) + // + // 1. In our equation x² + 4x + 4 = 0, we have a = 1, b = 4, and c = 4. + // 2. Substitute these values into the formula: + // x = [-4 ± sqrt(4² - 4 * 1 * 4)] / (2 * 1) + // x = [-4 ± sqrt(16 - 16)] / 2 + // x = [-4 ± sqrt(0)] / 2 + // x = [-4 ± 0] / 2 + // x = -4 / 2 + // x = -2 + // + // **Method 3: Perfect Square Trinomial** + // + // 1. Notice that the expression x² + 4x + 4 fits the pattern of a perfect square trinomial: + // a² + 2ab + b², where a = x and b = 2. + // 2. We can rewrite the equation as: + // (x + 2)² = 0 + // 3. Take the square root of both sides: + // x + 2 = 0 + // 4. Solve for x: + // x = -2 + // + // All methods lead to the same solution. + // + // **Answer:** + // The solution to the equation x² + 4x + 4 = 0 is x = -2. + // This is a repeated root (or a root with multiplicity 2). + + return response.text; +} + +// [END googlegenaisdk_thinking_with_txt] + +module.exports = { + generateWithThoughts, +};