-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (52 loc) · 1.56 KB
/
server.js
File metadata and controls
61 lines (52 loc) · 1.56 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
52
53
54
55
56
57
58
59
60
61
const express = require('express');
const axios = require('axios');
const cors = require('cors');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
// Serve static files
app.use(express.static('./'));
// POST endpoint for OpenAI conversation
app.post('/chat', async (req, res) => {
try {
// Extract message from request body
const { message } = req.body;
if (!message) {
return res.status(400).json({ error: 'Message is required' });
}
// Call OpenAI API
const response = await axios.post(
'https://api.openai.com/v1/chat/completions',
{
model: 'gpt-3.5-turbo',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: message }
],
max_tokens: 500
},
{
headers: {
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
'Content-Type': 'application/json'
}
}
);
// Extract and return the assistant's message
const assistantMessage = response.data.choices[0].message.content;
res.json({ reply: assistantMessage });
} catch (error) {
console.error('Error calling OpenAI API:', error.response?.data || error.message);
res.status(500).json({
error: 'Failed to get response from OpenAI',
details: error.response?.data || error.message
});
}
});
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});