Skip to content

Commit 9466399

Browse files
authored
Adding google search tool sample (#318)
* Add a sample for google_search tool with gemini 2.0 models
1 parent eefaaf6 commit 9466399

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

samples/search_grounding.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
GoogleGenerativeAI,
2121
} from "@google/generative-ai";
2222

23+
// `google_search_retrieval` is available in Gemini 1.5
2324
async function searchGrounding() {
2425
// [START search_grounding]
2526
// Make sure to include these imports:
@@ -50,9 +51,36 @@ async function searchGrounding() {
5051
console.log(result.response.candidates[0].groundingMetadata);
5152
// [END search_grounding]
5253
}
54+
55+
// `google_search` is available in Gemini 2.0
56+
async function googleSearch() {
57+
// [START google_search]
58+
// Make sure to include these imports:
59+
// import {
60+
// GoogleGenerativeAI,
61+
// } from "@google/generative-ai";
62+
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
63+
const model = genAI.getGenerativeModel(
64+
{
65+
model: "gemini-2.0-flash-exp",
66+
tools: [
67+
{
68+
googleSearch: {},
69+
},
70+
],
71+
},
72+
);
73+
74+
const prompt = "What is the price of Google stock today?";
75+
const result = await model.generateContent(prompt);
76+
console.log(result.response.candidates[0].groundingMetadata);
77+
// [END google_search]
78+
}
79+
5380
async function runAll() {
5481
// Comment out or delete any sample cases you don't want to run.
5582
await searchGrounding();
83+
await googleSearch();
5684
}
5785

5886
runAll();

0 commit comments

Comments
 (0)