-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(genai): Samples/thinking #4175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b393bec
adding samples, test, lints
6780e2a
adding samples, test, lints
1b76cca
Merge branch 'main' into sample/thinking
Guiners 99dd8a5
Update genai/test/thinking-includethoughts-with-txt.test.js
Guiners 257ed12
Update genai/test/thinking-budget-with-txt.test.js
Guiners aaaebee
Update genai/test/thinking-with-txt.test.js
Guiners 6d3ba66
Update genai/test/thinking-includethoughts-with-txt.test.js
Guiners 45650cf
adding samples, test, lints
ac8dd76
Merge branch 'main' into sample/thinking
msampathkumar a74be42
adding samples, test, lints
1f76398
Merge branch 'main' into sample/thinking
Guiners 2b62432
fixing functions names
1cee316
Merge branch 'main' into sample/thinking
gericdong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () => { | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
it('should return Thought Process ', async function () { | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
this.timeout(50000); | ||
const output = await sample.generateContent(projectId); | ||
assert(output.length > 0); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () => { | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
it('should return Thought Process', async function () { | ||
this.timeout(50000); | ||
const output = await sample.generateContent(projectId); | ||
assert(output.length > 0); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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); | ||
} | ||
} | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.