Skip to content

Instantly share code, notes, and snippets.

@alanzchen
Last active March 21, 2024 15:34
Show Gist options
  • Save alanzchen/57f69ed4c09cb2cf0111a8fcdb6ee6c0 to your computer and use it in GitHub Desktop.
Save alanzchen/57f69ed4c09cb2cf0111a8fcdb6ee6c0 to your computer and use it in GitHub Desktop.
PopClip Actions using ChatGPT.
// #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'
// }]
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": content }];
const { data } = await openai.post("chat/completions", {
model: model, messages
});
return data.choices[0].message.content.trim();
};
async function rewrite(input, options) {
openai.defaults.headers.common.Authorization = `Bearer ${options.apikey}`
const content = "Rewrite this using an academic tone: \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();
};
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: "Rewrite using an academic tone",
after: "copy-result",
code: rewrite,
icon: "symbol:pencil.and.outline"
}, {
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