Skip to content

Commit 98439c7

Browse files
GuinersGuinersgemini-code-assist[bot]msampathkumargericdong
authored
feat(genai): Samples/thinking (#4175)
* adding samples, test, lints * adding samples, test, lints * Update genai/test/thinking-includethoughts-with-txt.test.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update genai/test/thinking-budget-with-txt.test.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update genai/test/thinking-with-txt.test.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update genai/test/thinking-includethoughts-with-txt.test.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * adding samples, test, lints * adding samples, test, lints * fixing functions names --------- Co-authored-by: Guiners <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Sampath Kumar <[email protected]> Co-authored-by: Eric Dong <[email protected]>
1 parent 640a1d2 commit 98439c7

6 files changed

+352
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../thinking/thinking-budget-with-txt.js');
22+
23+
describe('thinking-budget-with-txt', () => {
24+
it('should return Thought Process', async function () {
25+
this.timeout(50000);
26+
const output = await sample.generateWithThoughts(projectId);
27+
assert(output.length > 0);
28+
});
29+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../thinking/thinking-includethoughts-with-txt.js');
22+
23+
describe('thinking-includethoughts-with-txt', () => {
24+
it('should return Thought Process', async function () {
25+
this.timeout(50000);
26+
const output = await sample.generateWithThoughts(projectId);
27+
assert(output.length > 0);
28+
});
29+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../thinking/thinking-with-txt.js');
22+
23+
describe('thinking-with-txt', () => {
24+
it('should return Thought Process', async function () {
25+
this.timeout(50000);
26+
const output = await sample.generateWithThoughts(projectId);
27+
assert(output.length > 0);
28+
});
29+
});
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_budget_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-flash',
35+
contents: 'solve x^2 + 4x + 4 = 0',
36+
config: {
37+
thinkingConfig: {
38+
thinkingBudget: 1024,
39+
},
40+
},
41+
});
42+
43+
console.log(response.text);
44+
// Example response:
45+
// To solve the equation $x^2 + 4x + 4 = 0$, you can use several methods:
46+
// **Method 1: Factoring**
47+
// 1. Look for two numbers that multiply to the constant term (4) and add up to the coefficient of the $x$ term (4).
48+
// 2. The numbers are 2 and 2 ($2 \times 2 = 4$ and $2 + 2 = 4$).
49+
// ...
50+
// ...
51+
// All three methods yield the same solution. This quadratic equation has exactly one distinct solution (a repeated root).
52+
// The solution is **x = -2**.
53+
54+
// Token count for `Thinking`
55+
console.log(response.usageMetadata.thoughtsTokenCount);
56+
// Example response:
57+
// 886
58+
59+
// Total token count
60+
console.log(response.usageMetadata.totalTokenCount);
61+
// Example response:
62+
// 1525
63+
return response.text;
64+
}
65+
// [END googlegenaisdk_thinking_budget_with_txt]
66+
67+
module.exports = {
68+
generateWithThoughts,
69+
};
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
};
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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_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+
});
37+
38+
console.log(response.text);
39+
40+
// Example Response:
41+
// Okay, let's solve the quadratic equation x² + 4x + 4 = 0.
42+
//
43+
// We can solve this equation by factoring, using the quadratic formula,
44+
// or by recognizing it as a perfect square trinomial.
45+
//
46+
// **Method 1: Factoring**
47+
//
48+
// 1. We need two numbers that multiply to the constant term (4)
49+
// and add up to the coefficient of the x term (4).
50+
// 2. The numbers 2 and 2 satisfy these conditions: 2 * 2 = 4 and 2 + 2 = 4.
51+
// 3. So, we can factor the quadratic as:
52+
// (x + 2)(x + 2) = 0
53+
// or
54+
// (x + 2)² = 0
55+
// 4. For the product to be zero, the factor must be zero:
56+
// x + 2 = 0
57+
// 5. Solve for x:
58+
// x = -2
59+
//
60+
// **Method 2: Quadratic Formula**
61+
//
62+
// The quadratic formula for an equation ax² + bx + c = 0 is:
63+
// x = [-b ± sqrt(b² - 4ac)] / (2a)
64+
//
65+
// 1. In our equation x² + 4x + 4 = 0, we have a = 1, b = 4, and c = 4.
66+
// 2. Substitute these values into the formula:
67+
// x = [-4 ± sqrt(4² - 4 * 1 * 4)] / (2 * 1)
68+
// x = [-4 ± sqrt(16 - 16)] / 2
69+
// x = [-4 ± sqrt(0)] / 2
70+
// x = [-4 ± 0] / 2
71+
// x = -4 / 2
72+
// x = -2
73+
//
74+
// **Method 3: Perfect Square Trinomial**
75+
//
76+
// 1. Notice that the expression x² + 4x + 4 fits the pattern of a perfect square trinomial:
77+
// a² + 2ab + b², where a = x and b = 2.
78+
// 2. We can rewrite the equation as:
79+
// (x + 2)² = 0
80+
// 3. Take the square root of both sides:
81+
// x + 2 = 0
82+
// 4. Solve for x:
83+
// x = -2
84+
//
85+
// All methods lead to the same solution.
86+
//
87+
// **Answer:**
88+
// The solution to the equation x² + 4x + 4 = 0 is x = -2.
89+
// This is a repeated root (or a root with multiplicity 2).
90+
91+
return response.text;
92+
}
93+
94+
// [END googlegenaisdk_thinking_with_txt]
95+
96+
module.exports = {
97+
generateWithThoughts,
98+
};

0 commit comments

Comments
 (0)