tudoujunha / gemini-google-extension

Chrome extension to view Gemini results alongside Google search
https://gemini.tudoujunha.com
GNU General Public License v3.0
78 stars 21 forks source link

Please add change safety setting and add option for gemini 1.5 pro and flash #12

Closed Nghiauet closed 3 months ago

Nghiauet commented 3 months ago

Some time the api be block because of safety system. And it quite annoying error message

Failed to load response from Gemini:
[GoogleGenerativeAI Error]: Candidate was blocked due to SAFETY

Can you add the change safety for the model and have option for change model type. Or can I open a pull request? like this image

This code for none safety check

"""
import {
  GoogleGenerativeAI,
  HarmCategory,
  HarmBlockThreshold,
}  from '@google/generative-ai'
import { GenerateAnswerParams, Provider } from '../types'

export class GeminiProvider implements Provider {
  private genAI: GoogleGenerativeAI

  constructor(private token: string , private model_gemini: string) {
    this.token = token
    this.model_gemini = model_gemini
    this.genAI = new GoogleGenerativeAI(this.token)
  }

  async generateAnswer(params: GenerateAnswerParams) {
    const generationConfig = {
      temperature: 1,
      topP: 0.95,
      topK: 64,
      maxOutputTokens: 8192,
      responseMimeType: "text/plain",
    };
    const safetySettings = [
      {
        category: HarmCategory.HARM_CATEGORY_HARASSMENT,
        threshold: HarmBlockThreshold.BLOCK_NONE,
      },
      {
        category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
        threshold: HarmBlockThreshold.BLOCK_NONE,
      },
      {
        category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
        threshold: HarmBlockThreshold.BLOCK_NONE,
      },
      {
        category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
        threshold: HarmBlockThreshold.BLOCK_NONE,
      },
    ];
    const model = this.genAI.getGenerativeModel({ model: this.model_gemini , safetySettings, generationConfig});
    const result = await model.generateContentStream(params.prompt)
    let text = ''
    for await (const chunk of result.stream) {
      const chunkText = chunk.text()
      text += chunkText
      console.debug('chunkText:', chunkText)
      params.onEvent({
        type: 'answer',
        data: {
          text: text,
          messageId: '',
          conversationId: '',
        },
      })
    }
    params.onEvent({ type: 'done' })
    return {}
  }
}
tudoujunha commented 3 months ago

Yes, you can pull a request. I don't know much about SAFETY😂.

Nghiauet commented 3 months ago

I have create the pull request can you check it for me.

tudoujunha commented 3 months ago

Thank you for your pull request. I checked it out and adopted some of it. The version has been updated, and I pushed the code I wrote before. https://github.com/tudoujunha/gemini-google-extension/releases/tag/v0.1.2