Spaces:
Build error
Build error
File size: 738 Bytes
0321cd3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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' })
}
} |