node-ejs-renderer/openai.js
2024-06-09 13:55:01 -04:00

22 lines
590 B
JavaScript

const { OpenAI } = require('openai');
const openai = new OpenAI({
// apiKey: process.env['OPENAI_API_KEY'],
apiKey: '',
});
async function getLearningContent(prompt) {
try {
const chatCompletion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: prompt }],
});
return chatCompletion.choices[0].message.content.trim();
} catch (error) {
console.error('Error fetching content from OpenAI:', error);
throw new Error('Failed to fetch learning content');
}
}
module.exports = { getLearningContent };