import { Configuration, OpenAIApi } from 'openai' const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY, }) const openai = new OpenAIApi(configuration) export default async function handler(req, res) { if (req.method !== 'POST') { return res.status(405).json({ message: 'Method not allowed' }) } try { const { prompt } = req.body const response = await openai.createCompletion({ model: "text-davinci-003", prompt: prompt, temperature: 0.7, max_tokens: 256, }) res.status(200).json({ result: response.data.choices[0].text }) } catch (error) { console.error('OpenAI API error:', error) res.status(500).json({ error: 'Failed to generate text' }) } }