-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
58 lines (46 loc) · 1.65 KB
/
main.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const executePythonScript = require('./pythonExecutor'); // Adjust the path accordingly
const express = require('express');
const bodyParser = require('body-parser');
const { Configuration, OpenAIApi } = require('openai'); // Make sure to install the 'openai' package
const app = express();
require('dotenv').config()
const port = 3000;
const configuration = new Configuration({
apiKey: process.env.API_KEY
});
const openai = new OpenAIApi(configuration);
app.use(bodyParser.json());
// Serve static files from the 'public' directory
app.use(express.static('public'));
// POST endpoint to get a response from ChatGPT
app.post('/ask', async (req, res) => {
const { prompt } = req.body;
if (!prompt) {
return res.status(400).json({ error: 'Prompt is required.' });
}
try {
const response = await askChatGpt(prompt);
res.json({ response });
} catch (error) {
res.json({ response: `Something went wrong: ${error.message}`, error: true});
}
});
app.post('/ask-python', async (req, res) => {
const { prompt } = req.body;
if (!prompt) {
return res.status(400).json({ error: 'Prompt is required.' });
}
const response = await executePythonScript('chatbot.py', [prompt] )
res.json({ response: response.response })
})
// Function to get a response from ChatGPT
async function askChatGpt(content) {
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{"role": "system", "content": content}, {role: "user", content: content}],
});
return completion.data.choices[0].message.content ?? '[no content]';
}
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});