Skip to content
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

Civic Integrity Harm Category needs to be added #246

Open
samrahimi opened this issue Sep 3, 2024 · 2 comments
Open

Civic Integrity Harm Category needs to be added #246

samrahimi opened this issue Sep 3, 2024 · 2 comments
Assignees
Labels
component:documentation Improvements or additions to documentation status:triaged Issue/PR triaged to the corresponding sub-team type:feature request New feature request/enhancement

Comments

@samrahimi
Copy link

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

@manojssmk manojssmk added component:documentation Improvements or additions to documentation type:feature request New feature request/enhancement status:triaged Issue/PR triaged to the corresponding sub-team labels Sep 5, 2024
@samrahimi
Copy link
Author

samrahimi commented Sep 8, 2024

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();```

@samrahimi
Copy link
Author

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:documentation Improvements or additions to documentation status:triaged Issue/PR triaged to the corresponding sub-team type:feature request New feature request/enhancement
Projects
None yet
Development

No branches or pull requests

4 participants