Skip to content

Instantly share code, notes, and snippets.

@cdpath
Created March 3, 2023 08:34
Show Gist options
  • Save cdpath/81998af22ddb3aec79260974799d7e1d to your computer and use it in GitHub Desktop.
Save cdpath/81998af22ddb3aec79260974799d7e1d to your computer and use it in GitHub Desktop.
PopClip OpenAI Code Reviewer
// #popclip extension for ChatGPT
// name: ChatGPT Code Reviewer
// icon: "square filled CR"
// 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'
// }]
async function chat (input, options, lang) {
const openai = require("axios").create({
baseURL: "https://api.openai.com/v1",
headers: { Authorization: `Bearer ${options.apikey}` },
});
let messages
switch (lang) {
case "en":
messages = [
{"role": "system", "content": "I want you to act as a code reviewer. I will provide you code snippets, please review them for any lexical or logic errors. Once you have finished reviewing the code, provide me with the improved code, and do not include extra declarations or comments."},
{"role": "user", "content": input.text},
]
break;
case "zh":
messages = [
{"role": "system", "content": "你是我的结对编程对象,检查接收到的代码的语法错误和逻辑错误,对其进行修正,向我提供修改后的代码。"},
{"role": "user", "content": `修改下面的代码,提高可读性,直接输出修改后的代码,不需要额外的声明:\n${input.text}`}
]
break;
}
const { data } = await openai.post("/chat/completions", {
model: "gpt-3.5-turbo",
messages,
});
const result = data.choices[0].message;
return input.text.trimEnd() + "\n\n" + result.content.trim();
};
exports.actions = [{
title: "ChatGPT: CR en",
after: "paste-result",
code: async (input, options) => chat(input, options, "en"),
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment