Skip to content

Instantly share code, notes, and snippets.

@cdpath
Last active January 31, 2024 06:47
Show Gist options
  • Save cdpath/7070432bb6cd52c4fa980b17f9f70306 to your computer and use it in GitHub Desktop.
Save cdpath/7070432bb6cd52c4fa980b17f9f70306 to your computer and use it in GitHub Desktop.
Gemini PopClip
// #popclip extension for Google Gemini
// name: Google Gemini
// icon: "square filled G"
// language: javascript
// module: true
// entitlements: [network]
// options: [{
// identifier: apikey, label: API Key, type: string,
// description: 'Obtain API key from Google Cloud Console'
// }, {
// identifier: prompt, label: 'Prompt Template', type: string,
// description: 'Enter the prompt template using {input} as a placeholder for the text'
// }]
const axios = require("axios");
async function generateContent(input, options) {
const prompt = options.prompt.replace('{input}', input.text);
const requestBody = {
"contents": [{
"parts": [
{"text": prompt}
]
}],
"safetySettings": [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_ONLY_HIGH"
}
],
"generationConfig": {
"stopSequences": [
"Title"
],
"temperature": 1.0,
"maxOutputTokens": 800,
"topP": 0.8,
"topK": 10
}
};
try {
const response = await axios.post(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${options.apikey}`,
requestBody,
{ headers: { 'Content-Type': 'application/json' } }
);
const generatedText = response.data.candidates[0].content.parts.map(part => part.text).join('\n');
return generatedText;
} catch (error) {
console.error("Error generating content:", error);
return "Error generating content: " + error.message;
}
}
exports.actions = [{
title: "Generate with Google Gemini",
after: "paste-result",
code: async (input, options) => generateContent(input, options),
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment