From 4d7925cb37be30747850c233359120a5727f47ef Mon Sep 17 00:00:00 2001 From: junyanxu Date: Fri, 4 Oct 2024 00:38:49 +0000 Subject: [PATCH 1/5] Add samples for log prob and search grounding --- samples/log_prob.js | 46 +++++++++++++++++++++++++++++++ samples/search_grouding.js | 56 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 samples/log_prob.js create mode 100644 samples/search_grouding.js diff --git a/samples/log_prob.js b/samples/log_prob.js new file mode 100644 index 0000000..f7436e1 --- /dev/null +++ b/samples/log_prob.js @@ -0,0 +1,46 @@ +/** + * @license + * Copyright 2024 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 + * + * http://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. + */ + +import { + GoogleGenerativeAI, +} from "@google/generative-ai"; + +async function enableLogProb() { + // [START log probability] + const genAI = new GoogleGenerativeAI(process.env.API_KEY); + const model = genAI.getGenerativeModel( + { + // Only 002 models + flash 1.5 8b models are enabled with log probs + // option. + model: "gemini-1.5-flash-002", + generationConfig: { + responseLogprobs: true + }, + }, + ); + const prompt = + "Hello!"; + const result = await model.generateContent(prompt); + console.log(JSON.stringify(result.response)); + // [END log probability] +} +async function runAll() { + // Comment out or delete any sample cases you don't want to run. + await enableLogProb(); +} + +runAll(); diff --git a/samples/search_grouding.js b/samples/search_grouding.js new file mode 100644 index 0000000..9ad9a5d --- /dev/null +++ b/samples/search_grouding.js @@ -0,0 +1,56 @@ +/** + * @license + * Copyright 2024 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 + * + * http://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. + */ + +import { + DynamicRetrievalMode, + GoogleGenerativeAI, +} from "@google/generative-ai"; + +async function searchGrounding() { + // [START search grounding] + const genAI = new GoogleGenerativeAI(process.env.API_KEY); + const model = genAI.getGenerativeModel( + { + model: "gemini-1.5-pro", + generationConfig: {}, + tools: [ + { + googleSearchRetrieval: { + dynamicRetrievalConfig: { + mode: DynamicRetrievalMode.MODE_DYNAMIC, + dynamicThreshold: 0.7, + }, + }, + }, + ], + }, + { apiVersion: "v1beta" }, + ); + + const prompt = + "What is the Google stock today?"; + + const result = await model.generateContent(prompt); + console.log(JSON.stringify(result.response)); + // [END search_grounding] +} +async function runAll() { + // Comment out or delete any sample cases you don't want to run. + await searchGrounding(); +} + +runAll(); From 9eac8858221acfe00d76d61b033703b3c7993194 Mon Sep 17 00:00:00 2001 From: junyanxu Date: Fri, 4 Oct 2024 03:46:17 +0000 Subject: [PATCH 2/5] Fix the comment. --- samples/log_prob.js | 9 +++------ samples/search_grouding.js | 6 ++---- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/samples/log_prob.js b/samples/log_prob.js index f7436e1..0225726 100644 --- a/samples/log_prob.js +++ b/samples/log_prob.js @@ -15,9 +15,7 @@ * limitations under the License. */ -import { - GoogleGenerativeAI, -} from "@google/generative-ai"; +import {GoogleGenerativeAI} from "@google/generative-ai"; async function enableLogProb() { // [START log probability] @@ -32,10 +30,9 @@ async function enableLogProb() { }, }, ); - const prompt = - "Hello!"; + const prompt = "Hello!"; const result = await model.generateContent(prompt); - console.log(JSON.stringify(result.response)); + console.log(result.response); // [END log probability] } async function runAll() { diff --git a/samples/search_grouding.js b/samples/search_grouding.js index 9ad9a5d..30261b3 100644 --- a/samples/search_grouding.js +++ b/samples/search_grouding.js @@ -41,11 +41,9 @@ async function searchGrounding() { { apiVersion: "v1beta" }, ); - const prompt = - "What is the Google stock today?"; - + const prompt = "What is the price of Google stock today?"; const result = await model.generateContent(prompt); - console.log(JSON.stringify(result.response)); + console.log(result.response); // [END search_grounding] } async function runAll() { From 912bc3beb3642e2bf44aa8a3d4f98df7f5bf2ae4 Mon Sep 17 00:00:00 2001 From: junyanxu Date: Fri, 4 Oct 2024 19:38:22 +0000 Subject: [PATCH 3/5] Update search grounding and log probs example. Resolve comment from Christina --- samples/README.md | 2 ++ samples/log_prob.js | 6 +++--- samples/{search_grouding.js => search_grounding.js} | 7 +++---- 3 files changed, 8 insertions(+), 7 deletions(-) rename samples/{search_grouding.js => search_grounding.js} (91%) diff --git a/samples/README.md b/samples/README.md index 4d61ba4..0c37e0e 100644 --- a/samples/README.md +++ b/samples/README.md @@ -46,4 +46,6 @@ to comment out cases you do not want to run. | [function_calling.js](./function_calling.js) | Using function calling | | [safety_settings.js](./safety_settings.js) | Setting and using safety controls | | [system_instruction.js](./system_instruction.js) | Setting system instructions | +| [search_grounding.js](./search_grounding.js) | Generate with google search grounding | +| [log_prob.js](./log_prob.js) | Generate with log probability for each token | | [text_generation.js](./text_generation.js) | Generating text | diff --git a/samples/log_prob.js b/samples/log_prob.js index 0225726..b201668 100644 --- a/samples/log_prob.js +++ b/samples/log_prob.js @@ -18,7 +18,7 @@ import {GoogleGenerativeAI} from "@google/generative-ai"; async function enableLogProb() { - // [START log probability] + // [START log_probs] const genAI = new GoogleGenerativeAI(process.env.API_KEY); const model = genAI.getGenerativeModel( { @@ -32,8 +32,8 @@ async function enableLogProb() { ); const prompt = "Hello!"; const result = await model.generateContent(prompt); - console.log(result.response); - // [END log probability] + console.log(result.response.candidates[0].logprobsResult); + // [END log_probs] } async function runAll() { // Comment out or delete any sample cases you don't want to run. diff --git a/samples/search_grouding.js b/samples/search_grounding.js similarity index 91% rename from samples/search_grouding.js rename to samples/search_grounding.js index 30261b3..ae1e39a 100644 --- a/samples/search_grouding.js +++ b/samples/search_grounding.js @@ -21,12 +21,11 @@ import { } from "@google/generative-ai"; async function searchGrounding() { - // [START search grounding] + // [START search_grounding] const genAI = new GoogleGenerativeAI(process.env.API_KEY); const model = genAI.getGenerativeModel( { - model: "gemini-1.5-pro", - generationConfig: {}, + model: "gemini-1.5-flash", tools: [ { googleSearchRetrieval: { @@ -43,7 +42,7 @@ async function searchGrounding() { const prompt = "What is the price of Google stock today?"; const result = await model.generateContent(prompt); - console.log(result.response); + console.log(result.response.candidates[0].groundingMetadata); // [END search_grounding] } async function runAll() { From fd63588c10223ec1df535cbd3354c351dad5f2ff Mon Sep 17 00:00:00 2001 From: junyanxu Date: Fri, 4 Oct 2024 19:40:25 +0000 Subject: [PATCH 4/5] Remove s --- samples/log_prob.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/log_prob.js b/samples/log_prob.js index b201668..6b9480c 100644 --- a/samples/log_prob.js +++ b/samples/log_prob.js @@ -18,7 +18,7 @@ import {GoogleGenerativeAI} from "@google/generative-ai"; async function enableLogProb() { - // [START log_probs] + // [START log_prob] const genAI = new GoogleGenerativeAI(process.env.API_KEY); const model = genAI.getGenerativeModel( { @@ -33,7 +33,7 @@ async function enableLogProb() { const prompt = "Hello!"; const result = await model.generateContent(prompt); console.log(result.response.candidates[0].logprobsResult); - // [END log_probs] + // [END log_prob] } async function runAll() { // Comment out or delete any sample cases you don't want to run. From cfe0a70d4ac23c4cfcead7ba6dcbb8faf25bed78 Mon Sep 17 00:00:00 2001 From: junyanxu Date: Mon, 7 Oct 2024 17:30:12 +0000 Subject: [PATCH 5/5] Add import comments to the sample --- samples/log_prob.js | 2 ++ samples/search_grounding.js | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/samples/log_prob.js b/samples/log_prob.js index 6b9480c..0e2573e 100644 --- a/samples/log_prob.js +++ b/samples/log_prob.js @@ -19,6 +19,8 @@ import {GoogleGenerativeAI} from "@google/generative-ai"; async function enableLogProb() { // [START log_prob] + // Make sure to include these imports: + // import {GoogleGenerativeAI} from "@google/generative-ai"; const genAI = new GoogleGenerativeAI(process.env.API_KEY); const model = genAI.getGenerativeModel( { diff --git a/samples/search_grounding.js b/samples/search_grounding.js index ae1e39a..363df16 100644 --- a/samples/search_grounding.js +++ b/samples/search_grounding.js @@ -22,6 +22,11 @@ import { async function searchGrounding() { // [START search_grounding] + // Make sure to include these imports: + // import { + // DynamicRetrievalMode, + // GoogleGenerativeAI, + // } from "@google/generative-ai"; const genAI = new GoogleGenerativeAI(process.env.API_KEY); const model = genAI.getGenerativeModel( {