Skip to content
Merged
29 changes: 29 additions & 0 deletions genai/test/thinking-budget-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -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', async () => {
it('should return Thought Process', async function () {
this.timeout(50000);
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
});
29 changes: 29 additions & 0 deletions genai/test/thinking-includethoughts-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -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', async () => {
it('should return Thought Process ', async function () {
this.timeout(50000);
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
});
29 changes: 29 additions & 0 deletions genai/test/thinking-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -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', async () => {
it('should return Thought Process', async function () {
this.timeout(50000);
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
});
69 changes: 69 additions & 0 deletions genai/thinking/thinking-budget-with-txt.js
Original file line number Diff line number Diff line change
@@ -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 generateContent(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const response = await ai.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 = {
generateContent,
};
96 changes: 96 additions & 0 deletions genai/thinking/thinking-includethoughts-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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 generateContent(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const response = await ai.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 = {
generateContent,
};
97 changes: 97 additions & 0 deletions genai/thinking/thinking-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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 generateContent(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const response = await ai.models.generateContent({
model: 'gemini-2.5-pro',
contents: 'solve x^2 + 4x + 4 = 0',
});

console.log(response.text);

return 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).

// [END googlegenaisdk_thinking_with_txt]

module.exports = {
generateContent,
};
Loading