-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.mjs
55 lines (47 loc) · 1.51 KB
/
index.mjs
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
import { ChatGPTAPIBrowser } from 'chatgpt'
import express from 'express'
const api = new ChatGPTAPIBrowser({ email: process.env.OPENAI_EMAIL, password: process.env.OPENAI_PASSWORD })
await api.initSession()
async function say(what) {
try {
const { response, conversationId, messageId } = await api.sendMessage(what) || {}
return {
answer: response,
sameConversationUrlComponent: `&conversationId=${conversationId}&parentMessageId=${messageId}`
}
} catch (e) {
console.log(e)
return 'Error'
}
}
async function old_say({ what, conversationId, parentMessageId }) {
try {
const { response, messageId } = await api.sendMessage(what, {
conversationId,
parentMessageId
}) || {}
return {
answer: response,
sameConversationUrlComponent: `&conversationId=${conversationId}&parentMessageId=${messageId}`
}
} catch (e) {
console.log(e)
return 'Error'
}
}
const app = express()
app.get('/', async (req, res) => {
const { q: what, conversationId, parentMessageId } = req.query
if (conversationId && parentMessageId) {
const result = await old_say({ what, conversationId, parentMessageId })
res.send(result)
return
}
const result = await say(req.query.q)
res.send(result)
})
app.listen(8661, () => {
console.log('Listening on port 8661')
console.log('New Conversation Example:\n http://localhost:8661/?q=Hello')
console.log('Old Conversation Example:\n http://localhost:8661/?q=Hello&conversationId=123&parentMessageId=456')
})