-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (35 loc) · 1.25 KB
/
server.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
import * as dotenv from 'dotenv';
dotenv.config();
import { Configuration, OpenAIApi } from 'openai';
const configuration = new Configuration({
apiKey: process.env.OPENAI,
});
const openai = new OpenAIApi(configuration);
import express from 'express';
import cors from 'cors';
const app = express();
app.use(cors());
app.use(express.json());
let conversation = []; // Initialize an empty conversation array to store the messages.
app.post('/chat', async (req, res) => {
try {
console.log(req.body);
const messages = req.body.messages;
console.log(messages);
conversation = messages; // Here's the updated line
const aiResponse = await openai.createChatCompletion({
model: 'gpt-3.5-turbo-16k',
messages: conversation,
});
const response = aiResponse.data.choices[0].message;
console.log(response);
conversation.push({ role: 'assistant', content: response.content });
res.send({ response });
} catch (error) {
console.error(error);
res
.status(500)
.send(error?.response.data.error.message || 'Something went wrong');
}
});
app.listen(8089, () => console.log('make conversation on http://localhost:8089/chat'));