Created
December 18, 2023 04:07
-
-
Save mahdi-malv/48db42653986b18f8b1210d05e6e58bb to your computer and use it in GitHub Desktop.
Multi-level depth ChatGPT extension for popclip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #popclip extension for ChatGPT | |
// name: ChatGPT Quick Actions | |
// icon: iconify:logos:openai-icon | |
// language: javascript | |
// module: true | |
// entitlements: [network] | |
// options: [{ | |
// identifier: apikey, label: API Key, type: string, | |
// description: 'Obtain API key from https://platform.openai.com/account/api-keys' | |
// }] | |
// 0. Level from L1 (shortest) to L5 (Longest), defaults to L1 if not provided. | |
// 1. Based on https://gist.github.com/alanzchen/57f69ed4c09cb2cf0111a8fcdb6ee6c0 (Credit goes to him) | |
// 2. Easy usage: | |
// - Save this file and add a `.popcliptxt` suffix to the file name. | |
// - Double clip to open it using PopClip | |
// - Enter the APIKey | |
const openai = require("axios").create({ | |
baseURL: "https://api.openai.com/v1/" | |
}); | |
const model = "gpt-3.5-turbo"; | |
async function prompt(input, options) { | |
openai.defaults.headers.common.Authorization = `Bearer ${options.apikey}` | |
const content = input.text.trim(); | |
const messages = [ | |
{ "role": "user", "content": "You answer based on the depth level required (from L1, being the shortest and pricise to the L5, the most descriptive and verbose). If level wasn't specified, answer for L1 only. DO NOT MENTION WHAT LEVEL YOU ARE ANSWERING WITH" }, | |
{ "role": "user", "content": content } | |
]; | |
const { data } = await openai.post("chat/completions", { | |
model: model, messages | |
}); | |
return data.choices[0].message.content.trim(); | |
}; | |
async function summarize(input, options) { | |
openai.defaults.headers.common.Authorization = `Bearer ${options.apikey}` | |
const content = "Summarize the following text as concise as possible: \n\n" + input.text.trim(); | |
const messages = [{ "role": "user", "content": content }]; | |
const { data } = await openai.post("chat/completions", { | |
model: model, messages | |
}); | |
return data.choices[0].message.content.trim(); | |
}; | |
exports.actions = [ | |
{ | |
title: "Execute the selected prompt", | |
after: "paste-result", | |
code: prompt, | |
icon: "symbol:wand.and.stars" | |
}, { | |
title: "Summarize the selected text", | |
after: "preview-result", | |
code: summarize, | |
icon: "symbol:arrow.down.right.and.arrow.up.left" | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment