-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbriefRoutes.js
36 lines (30 loc) · 1.2 KB
/
briefRoutes.js
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
29
30
31
32
33
34
35
36
// briefRoutes.js
const express = require('express');
const router = express.Router();
const axios = require('axios');
router.post('/', async (req, res) => {
const { product, targetAudience, advertisingChannel } = req.body;
// Ensure all necessary data is provided
if (!product || !targetAudience || !advertisingChannel) {
return res.status(400).json({ error: 'Missing necessary data for brief creation.' });
}
// Define the prompt for the GPT-3 model
const prompt = `Create an advertising brief for a ${product} targeting ${targetAudience} on ${advertisingChannel}.`;
try {
const response = await axios.post('https://api.openai.com/v1/engines/davinci-codex/completions', {
prompt,
max_tokens: 200,
}, {
headers: {
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
'Content-Type': 'application/json'
}
});
// Respond with the generated text
res.json({ brief: response.data.choices[0].text.trim() });
} catch (error) {
// Respond with error information
res.status(500).json({ error: error.toString() });
}
});
module.exports = router;