Open samrahimi opened 2 months ago
Here is the patch (assumes that you have installed the @google/generative-ai package and it is in your node_modules)
This harm category is supported by gemini-1.5-pro-exp-0827, gemini-1.5-flash-exp-0827 model.
Prerequisites: The Gemini SDK must be installed (npm install @google/generative-ai).
Usage: node patch_gemini_sdk.js (this file should be placed in the root of the project)
*/
const fs = require('fs').promises;
const path = require('path');
const SDK_PATH = path.join(process.cwd(), 'node_modules', '@google', 'generative-ai');
const FILES_TO_PATCH = [
{ path: 'dist/index.js', type: 'js' },
{ path: 'dist/index.mjs', type: 'js' },
{ path: 'dist/generative-ai.d.ts', type: 'ts' },
{ path: 'dist/types/enums.d.ts', type: 'ts' }
];
const JS_SEARCH = 'HarmCategory["HARM_CATEGORY_DANGEROUS_CONTENT"] = "HARM_CATEGORY_DANGEROUS_CONTENT";';
const JS_REPLACE = 'HarmCategory["HARM_CATEGORY_DANGEROUS_CONTENT"] = "HARM_CATEGORY_DANGEROUS_CONTENT";\n HarmCategory["HARM_CATEGORY_CIVIC_INTEGRITY"] = "HARM_CATEGORY_CIVIC_INTEGRITY";';
const TS_SEARCH = 'HARM_CATEGORY_DANGEROUS_CONTENT = "HARM_CATEGORY_DANGEROUS_CONTENT"';
const TS_REPLACE = 'HARM_CATEGORY_DANGEROUS_CONTENT = "HARM_CATEGORY_DANGEROUS_CONTENT",\n HARM_CATEGORY_CIVIC_INTEGRITY = "HARM_CATEGORY_CIVIC_INTEGRITY"';
async function checkSDKInstalled() {
try {
await fs.access(SDK_PATH);
return true;
} catch {
console.log("Gemini SDK not found. Skipping patch.");
return false;
}
}
async function checkIfAlreadyPatched() {
try {
const content = await fs.readFile(path.join(SDK_PATH, 'dist', 'index.js'), 'utf8');
return content.includes('HARM_CATEGORY_CIVIC_INTEGRITY');
} catch {
return false;
}
}
async function patchFile(filePath, search, replace) {
const fullPath = path.join(SDK_PATH, filePath);
try {
let content = await fs.readFile(fullPath, 'utf8');
if (content.includes("HARM_CATEGORY_CIVIC_INTEGRITY")) {
console.log(`Patch already applied in ${filePath}`);
return;
}
if (content.includes(search)) {
content = content.replace(search, replace);
await fs.writeFile(fullPath, content, 'utf8');
console.log(`Successfully patched ${filePath}`);
} else {
console.log(`No changes needed in ${filePath}`);
}
} catch (error) {
console.error(`Error patching ${filePath}:`, error.message);
throw error;
}
}
async function runPatch() {
try {
if (!(await checkSDKInstalled())) {
return;
}
if (await checkIfAlreadyPatched()) {
console.log("Patch has already been applied. Skipping.");
return;
}
for (const file of FILES_TO_PATCH) {
const search = file.type === 'js' ? JS_SEARCH : TS_SEARCH;
const replace = file.type === 'js' ? JS_REPLACE : TS_REPLACE;
await patchFile(file.path, search, replace);
}
console.log("All steps completed successfully");
} catch (error) {
console.error("Error during patching process:", error.message);
process.exit(1);
}
}
runPatch();```
@manojssmk @lahirumaramba no this is NOT documentation... There is an actual enum, HarmCategory, that contains the strings for each supported category of harm you can either block or not...
And it is MISSING the new HARM_CATEGORY_CIVIC_INTEGRITY category, therefore making it impossible to configure this category using the SDKs current release. Can I make a PR and add it properly?
The person who tagged this completely misunderstood your PR. I also noticed it's missing, it's quite annoying!
@manojssmk @lahirumaramba please fix.
Related
Description of the bug:
The new gemini pro and flash models released 0827 (exp) have an additional harm category called civic integrity, which is present in the AI studio UI but is totally undocumented anywhere and is missing from the npm package @google/generative-ai.
In order to keep my customers happy, I patched the package and added the item to the enum... how do I pull request it so it gets merged in?
Actual vs expected behavior:
No response
Any other information you'd like to share?
No response