roastify
Version:
A Node.js library that generates sarcastic and roast-style responses for API failures using OpenAI.
43 lines (34 loc) • 1.5 kB
JavaScript
const { OpenAI } = require("openai");
const generateRoast = async (apiKey, type = "general", language = "english") => {
if (!apiKey) {
throw new Error("OpenAI API key is required.");
}
const openai = new OpenAI({ apiKey });
const prompt = `Generate a short, witty roast in the style of sarcastic humor. It should be clever, slightly insulting but still funny from this request failure context of ${type} using language ${language}. Avoid anything too harsh or offensive. Make it under 15 word`;
try {
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo-0125",
messages: [{ role: "system", content: prompt }],
max_tokens: 60,
temperature: 0.8,
});
return response.choices[0]?.message?.content || "API failed, and so did your luck.";
} catch (error) {
console.error("OpenAI Error:", error);
return "Even the AI couldn't handle this request. That's how bad it is.";
}
};
// Middleware factory function
const roastMiddleware = (apiKey, language = "english") => {
if (!apiKey) {
throw new Error("OpenAI API key must be provided when initializing roastMiddleware.");
}
return async (req, res, next) => {
res.sendRoast = async (type = "general") => {
const roastMessage = await generateRoast(apiKey, type, language);
res.status(400).json({ message: roastMessage });
};
next();
};
};
module.exports = { roastMiddleware, generateRoast };