-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (56 loc) · 1.58 KB
/
index.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
59
60
61
62
63
64
65
66
67
68
import axios from 'axios'
import redis from 'redis'
import dotenv from 'dotenv'
dotenv.config()
const API_KEY = process.env.API_KEY
const GPT_API_URL = process.env.GPT_API_URL
async function askGPT(messages) {
const response = await axios.post(GPT_API_URL, {
messages: messages,
model: 'gpt-3.5-turbo',
max_tokens: 100,
temperature: 0.7,
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + API_KEY,
}
})
return response.data.choices[0];
}
async function askGPTUntilComplete(question) {
const messages = [
{ role: 'user', content: question }
]
let response = { finish_reason: 'stop' }
do {
response = await askGPT(messages)
console.log(response.message.content);
messages.push(response.message)
} while (response.finish_reason !== 'stop')
}
const client = redis.createClient({
url: 'redis://localhost:6379'
})
client.connect().then(async (data) => {
const lastTime = await client.get('last_time');
const executeChatGPT = () => {
askGPTUntilComplete('Between Sequalize, Prisma and Typeorm which one is the best orm for nodeJs?').then(() => {
client.set('last_time', new Date().getTime())
client.quit()
})
}
if (!lastTime) {
executeChatGPT()
} else {
const now = new Date().getTime()
const savedDate = new Date()
savedDate.setTime(lastTime)
if (now - savedDate.getTime() < 25000) {
console.log('You just can make 3 request per minute to ChatGPT API')
client.quit()
return
}
executeChatGPT()
}
}).catch(error => console.log(error))