Skip to content

Instantly share code, notes, and snippets.

@polluterofminds
Last active October 28, 2022 14:00
Show Gist options
  • Save polluterofminds/d4ad191d92724dc7446984351dde534e to your computer and use it in GitHub Desktop.
Save polluterofminds/d4ad191d92724dc7446984351dde534e to your computer and use it in GitHub Desktop.
Email Rejections API
require("dotenv");
const axios = require("axios");
exports.handler = async (event, context) => {
try {
const totalEmailsToFetch = Math.floor(Math.random() * 3) + 1
const data = JSON.stringify({
model: "text-davinci-002",
prompt: "Write a funny email rejection from a mobile App Marketplace that gives a silly reason for the app's rejection.",
temperature: 0.7,
max_tokens: 256,
n: totalEmailsToFetch
});
const config = {
method: "post",
url: "https://api.openai.com/v1/completions",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.OPEN_AI_KEY}`,
},
data: data,
};
const res = await axios(config);
if(!res.data.choices) {
throw new Error("No text completion choices available");
}
return {
statusCode: 200,
body: JSON.stringify(res.data.choices),
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*',
},
}
} catch (err) {
console.log(err.response.data);
return { statusCode: 500, body: err.toString() };
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment