From 4d7925cb37be30747850c233359120a5727f47ef Mon Sep 17 00:00:00 2001 From: junyanxu Date: Fri, 4 Oct 2024 00:38:49 +0000 Subject: [PATCH] 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();