|
|
const express = require('express'); |
|
|
const axios = require('axios'); |
|
|
const cors = require('cors'); |
|
|
const path = require('path'); |
|
|
require('dotenv').config(); |
|
|
|
|
|
const app = express(); |
|
|
const port = process.env.PORT || 8000; |
|
|
|
|
|
app.use(cors()); |
|
|
app.use(express.json()); |
|
|
app.use(express.static(path.join(__dirname, 'public'))); |
|
|
|
|
|
|
|
|
app.get('/', (req, res) => { |
|
|
res.sendFile(path.join(__dirname, 'public', 'index.html')); |
|
|
}); |
|
|
|
|
|
|
|
|
app.post('/api/generate', async (req, res) => { |
|
|
const { prompt } = req.body; |
|
|
|
|
|
try { |
|
|
const response = await axios.post( |
|
|
'https://api.groq.com/v1/chat/completions', |
|
|
{ |
|
|
model: 'mixtral-8x7b-32768', |
|
|
messages: [{ role: 'user', content: prompt }], |
|
|
max_tokens: 1000, |
|
|
temperature: 0.7, |
|
|
top_p: 0.9 |
|
|
}, |
|
|
{ |
|
|
headers: { |
|
|
'Authorization': `Bearer ${process.env.GROQ_API_KEY}`, |
|
|
'Content-Type': 'application/json' |
|
|
} |
|
|
} |
|
|
); |
|
|
|
|
|
const content = response.data.choices[0].message.content; |
|
|
res.json({ content }); |
|
|
} catch (error) { |
|
|
console.error('Error calling Groq API:', error); |
|
|
res.status(500).json({ error: 'Failed to generate content' }); |
|
|
} |
|
|
}); |
|
|
|
|
|
app.listen(port, () => { |
|
|
console.log(`Server running at http://localhost:${port}`); |
|
|
}); |