Skip to content

Instantly share code, notes, and snippets.

@mzbac
Created September 22, 2023 07:42
Show Gist options
  • Save mzbac/8884f3ebbf4e924b57fa2a5a1c57d1d3 to your computer and use it in GitHub Desktop.
Save mzbac/8884f3ebbf4e924b57fa2a5a1c57d1d3 to your computer and use it in GitHub Desktop.
function escapeShellArg(str) {
return "'" + str.replace(/'/g, "'\\''") + "'";
}
const removeBackticks = (str) => {
// remove leading backticks
str = str.replace(/^(```\n|```)/g, '');
// remove tailing backticks and everything after
const index = str.lastIndexOf('```');
if (index !== -1) {
return str.slice(0, index);
}
return str;
}
function run(input, parameters) {
const apiEndpoint = "https://api.openai.com/v1/chat/completions";
const prompt = `Please correct, polish, or translate the text delimited by triple backticks to standard English.
Text=\`\`\`she no went to market\`\`\`
Output: She didn't go to the market.
Text=\`\`\`${input}\`\`\`
Output:`
const requestData = {
"messages": [
{
"role": "user",
"content": prompt
}
],
"model": "gpt-3.5-turbo",
"max_tokens": 2000,
"temperature": 0.1,
"top_p": 1,
"frequency_penalty": 1.3,
"presence_penalty": 1.3
};
const curlCommand = `curl -X POST -H 'Content-Type: application/json' -H "Authorization: Bearer <your openai apikey>" -d ${escapeShellArg(
JSON.stringify(requestData)
)} '${apiEndpoint}'`;
// Instantiate the Application object
const app = Application.currentApplication();
app.includeStandardAdditions = true;
const apiResultJSON = app.doShellScript(curlCommand);
// Parse the JSON response and extract the result
const apiResultObject = JSON.parse(apiResultJSON);
const result = apiResultObject.choices[0].messages.content
return result.trim();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment