-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.js
35 lines (35 loc) · 925 Bytes
/
node.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
/**
* GET chat messages
*/
router.post('/chat', async function (req, res, next) {
const payload = req.body
const openaiUrl = 'https://v1/chat/completions' //可以自己搭建一个反代理地址
const keys = [
'sk-',
'sk-',
'sk-',
'sk-',
'sk-',
]
const headers = {
'Content-Type': 'application/json',
'Transfer-Encoding': 'chunked',
Authorization: `Bearer ${keys[Math.floor(Math.random() * keys.length)]}`,
}
const data = {
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: payload.message }],
}
try {
const response = await axios.post(openaiUrl, data, { headers })
res.writeHead(200, {
'Content-Type': 'application/json',
'Transfer-Encoding': 'chunked',
})
res.write(JSON.stringify(response.data.choices))
res.end()
} catch (error) {
console.error(error)
next(error)
}
})