-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Added JS port for Safety notebook #1016
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @Rahul-Lashkari, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new JavaScript quickstart guide that illustrates how to interact with and configure the Gemini API's safety features. It provides practical examples of how to handle potentially harmful prompts, understand safety feedback, and customize safety settings, thereby enhancing the developer's ability to manage content moderation within their applications. The accompanying Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request adds a JavaScript version of the 'Safety' quickstart notebook and updates the corresponding README file. The update to quickstarts-js/README.md is correct and properly includes the new example. However, the new quickstarts-js/Safety.js file contains several critical errors in its use of the Google Generative AI JavaScript SDK. The package name, version, SDK initialization, and API call patterns are all incorrect, which will prevent the code from functioning. I have provided specific, critical-severity comments with code suggestions to correct these issues and align the implementation with the official SDK documentation.
quickstarts-js/Safety.js
Outdated
| const module = await import("https://esm.sh/@google/genai@1.4.0"); | ||
| const { GoogleGenAI, HarmCategory, HarmBlockThreshold } = module; | ||
|
|
||
| const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); | ||
| const MODEL_ID = "gemini-2.5-flash-lite"; |
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.
The SDK import and initialization are incorrect and will prevent the code from running.
- The correct package is
@google/generative-ai, not@google/genai. - The version
1.4.0does not exist for this package. You should use a recent, valid version like0.14.0. - The class name is
GoogleGenerativeAI, notGoogleGenAI. - The
GoogleGenerativeAIconstructor takes the API key as a string directly, not in an options object. - The standard API pattern is to get a model instance using
getGenerativeModel()and then call methods on it. Theai.modelsproperty you are using does not exist.
const { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } = await import("https://esm.sh/@google/generative-ai@0.14.0");
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const MODEL_ID = "gemini-2.5-flash-lite";
const model = genAI.getGenerativeModel({ model: MODEL_ID });
quickstarts-js/Safety.js
Outdated
| const response = await ai.models.generateContent({ | ||
| model: MODEL_ID, | ||
| contents: unsafePrompt, | ||
| }); | ||
| console.log(response.text); |
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.
Following the setup correction, this API call to generateContent is incorrect. It should be called on the model instance. The method returns a GenerateContentResult object, and the text content is accessed via the text() method on the response property.
const result = await model.generateContent(unsafePrompt);
const response = result.response;
console.log(response.text());
quickstarts-js/Safety.js
Outdated
| const responseWithSettings = await ai.models.generateContent({ | ||
| model: MODEL_ID, | ||
| contents: unsafePrompt, | ||
| config: { | ||
| safetySettings: safetySettings, | ||
| }, | ||
| }); | ||
| console.log("Finish Reason:", responseWithSettings.candidates[0].finishReason); | ||
| console.log(responseWithSettings.text); |
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.
This API call is also incorrect and needs to be updated to use the model instance.
- The
safetySettingsshould be passed as a top-level property in the request object, not nested underconfig. - When passing
safetySettings, you must use the object form for the request, which requires structuringcontentsas an array ofContentobjects. - The result of
generateContentis aGenerateContentResult. The actual response is inresult.response. You need to access properties likecandidatesand thetext()method from there.
const result = await model.generateContent({
contents: [{ parts: [{ text: unsafePrompt }] }],
safetySettings,
});
const responseWithSettings = result.response;
console.log("Finish Reason:", responseWithSettings.candidates[0].finishReason);
console.log(responseWithSettings.text());
quickstarts/Safety.ipynbnotebookquickstarts-js/README.md