-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (42 loc) · 1.43 KB
/
Copy pathserver.js
File metadata and controls
51 lines (42 loc) · 1.43 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// server.js
const express = require('express');
const bodyParser = require('body-parser');
const fetch = require('node-fetch');
const app = express();
const PORT = 3000;
// Replace with your OpenAI API key
const OPENAI_API_KEY = 'YOUR_OPENAI_API_KEY';
app.use(bodyParser.json());
app.use(express.static('public'));
app.post('/api/generate-steps', async (req, res) => {
const { task } = req.body;
// Send the task to OpenAI's API
try {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${OPENAI_API_KEY}`
},
body: JSON.stringify({
model: 'gpt-4',
messages: [
{ role: 'system', content: 'You are an assistant who helps break tasks into step-by-step instructions.' },
{ role: 'user', content: `Break down the following task into steps: ${task}` }
],
max_tokens: 150
})
});
const data = await response.json();
const stepsText = data.choices[0].message.content;
// Split steps based on line breaks or numbering
const steps = stepsText.split('\n').filter(step => step.trim());
res.json({ steps });
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Error generating steps' });
}
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});