-
Notifications
You must be signed in to change notification settings - Fork 154
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
Add samples for log prob and search grounding #273
Changes from 1 commit
4d7925c
9eac885
912bc3b
fd63588
cfe0a70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
junyanxu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const genAI = new GoogleGenerativeAI(process.env.API_KEY); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For devsite snippets we add a comment like this about what imports the user will need, since the import at the top of the file will not be included in the includecode snippet: https://github.com/google-gemini/generative-ai-js/blob/add-samples/samples/text_generation.js#L43-L44 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see this is resolved, but I don't see the comment added? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
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 = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably don't need to split this across two lines. |
||
"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(); |
junyanxu marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
junyanxu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const genAI = new GoogleGenerativeAI(process.env.API_KEY); | ||
const model = genAI.getGenerativeModel( | ||
{ | ||
model: "gemini-1.5-pro", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason for choosing different models for each sample? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see the two models are now flash and flash-002, is there a reason they are different? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the logprob is only supported for 002 model. A bit background: Labs are calling vertex for all 002 models. And logprob is a feature only supported by vertex for now. |
||
generationConfig: {}, | ||
junyanxu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tools: [ | ||
{ | ||
googleSearchRetrieval: { | ||
dynamicRetrievalConfig: { | ||
mode: DynamicRetrievalMode.MODE_DYNAMIC, | ||
dynamicThreshold: 0.7, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
{ apiVersion: "v1beta" }, | ||
); | ||
|
||
const prompt = | ||
"What is the Google stock today?"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "What is the Google stock today?" -> "What is the price of Google stock today?" |
||
|
||
const result = await model.generateContent(prompt); | ||
console.log(JSON.stringify(result.response)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't need to use |
||
// [END search_grounding] | ||
} | ||
async function runAll() { | ||
// Comment out or delete any sample cases you don't want to run. | ||
await searchGrounding(); | ||
} | ||
|
||
runAll(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd leave these imports on a single line, unless they get longer than 80 characters. That's what I usually see in JS.