Skip to content

Commit bb553d0

Browse files
authored
Add samples for log prob and search grounding (#273)
* Add samples for log prob and search grounding * Fix the comment. * Update search grounding and log probs example. Resolve comment from Christina * Remove s * Add import comments to the sample
1 parent cec7440 commit bb553d0

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

samples/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ to comment out cases you do not want to run.
4646
| [function_calling.js](./function_calling.js) | Using function calling |
4747
| [safety_settings.js](./safety_settings.js) | Setting and using safety controls |
4848
| [system_instruction.js](./system_instruction.js) | Setting system instructions |
49+
| [search_grounding.js](./search_grounding.js) | Generate with google search grounding |
50+
| [log_prob.js](./log_prob.js) | Generate with log probability for each token |
4951
| [text_generation.js](./text_generation.js) | Generating text |

samples/log_prob.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import {GoogleGenerativeAI} from "@google/generative-ai";
19+
20+
async function enableLogProb() {
21+
// [START log_prob]
22+
// Make sure to include these imports:
23+
// import {GoogleGenerativeAI} from "@google/generative-ai";
24+
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
25+
const model = genAI.getGenerativeModel(
26+
{
27+
// Only 002 models + flash 1.5 8b models are enabled with log probs
28+
// option.
29+
model: "gemini-1.5-flash-002",
30+
generationConfig: {
31+
responseLogprobs: true
32+
},
33+
},
34+
);
35+
const prompt = "Hello!";
36+
const result = await model.generateContent(prompt);
37+
console.log(result.response.candidates[0].logprobsResult);
38+
// [END log_prob]
39+
}
40+
async function runAll() {
41+
// Comment out or delete any sample cases you don't want to run.
42+
await enableLogProb();
43+
}
44+
45+
runAll();

samples/search_grounding.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import {
19+
DynamicRetrievalMode,
20+
GoogleGenerativeAI,
21+
} from "@google/generative-ai";
22+
23+
async function searchGrounding() {
24+
// [START search_grounding]
25+
// Make sure to include these imports:
26+
// import {
27+
// DynamicRetrievalMode,
28+
// GoogleGenerativeAI,
29+
// } from "@google/generative-ai";
30+
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
31+
const model = genAI.getGenerativeModel(
32+
{
33+
model: "gemini-1.5-flash",
34+
tools: [
35+
{
36+
googleSearchRetrieval: {
37+
dynamicRetrievalConfig: {
38+
mode: DynamicRetrievalMode.MODE_DYNAMIC,
39+
dynamicThreshold: 0.7,
40+
},
41+
},
42+
},
43+
],
44+
},
45+
{ apiVersion: "v1beta" },
46+
);
47+
48+
const prompt = "What is the price of Google stock today?";
49+
const result = await model.generateContent(prompt);
50+
console.log(result.response.candidates[0].groundingMetadata);
51+
// [END search_grounding]
52+
}
53+
async function runAll() {
54+
// Comment out or delete any sample cases you don't want to run.
55+
await searchGrounding();
56+
}
57+
58+
runAll();

0 commit comments

Comments
 (0)