dscrdbt/utils/newsProcessor.js

80 lines
2.5 KiB
JavaScript

const OpenAI = require('openai');
// Initialize OpenAI client
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// Function to generate Instagram caption
async function generateCaption(articleData) {
try {
const prompt = `Act as a community manager for a technology and software community. Your job is to create content for the community's social media.
Give me the copy for the Instagram post.
Here is the information:
URL: ${articleData}
The copy for the Instagram post should be informative and educational, written in a friendly and approachable tone that invites conversation and interaction.
Try to relate the information to technology and software.
Make sure to include hashtags following Instagram and marketing best practices.`;
const completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: prompt }],
max_tokens: 500,
temperature: 0.7,
});
return completion.choices[0].message.content.trim();
}
catch (error) {
throw new Error(`Failed to generate caption: ${error.message}`);
}
}
// Function to generate image copy
async function generateImage(articleData) {
try {
const prompt = `Act as a community manager for a technology and software community. Your job is to create content for the community's social media.
Give me the copy for the image.
Here is the information:
URL: ${articleData}
The copy for the image should be engaging and attention-grabbing to encourage people to click and read the post.`;
const completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: prompt }],
max_tokens: 100,
temperature: 0.7,
});
return completion.choices[0].message.content.trim();
}
catch (error) {
throw new Error(`Failed to generate image copy: ${error.message}`);
}
}
// Function to generate image using DALL-E
async function generatePostImage(imageText) {
try {
const prompt = `Create a modern, professional social media image that represents: ${imageText}. The image should be suitable for a technology and software community, with a clean and engaging design.`;
const response = await openai.images.generate({
model: 'dall-e-3',
prompt: prompt,
n: 1,
quality: 'standard',
size: '1024x1024',
});
return response.data[0].url;
}
catch (error) {
throw new Error(`Failed to generate image with DALL-E: ${error.message}`);
}
}
module.exports = {
generateCaption,
generateImage,
generatePostImage,
};