-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathchatgpt.js
82 lines (71 loc) · 1.97 KB
/
chatgpt.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import config from '../config/config.js'
import Moderations from './moderations.js'
import tokenCount from './tokenCount.js'
const chatGPT = {
sendMessage: null,
}
chatGPT.sendMessage = async function (prompt) {
const tokens = tokenCount(prompt)
const MAX_TOKENS = config.get("MAX_TOKEN")
if (tokens > MAX_TOKENS / 2) {
return `Please limit your prompt to a maximum of ${parseInt(MAX_TOKENS / 2)} tokens. Thank you.`
}
const messages = [
{
role: "system",
content: config.get("CONVERSATION_START_PROMPT") != "" ?
config.get("CONVERSATION_START_PROMPT") :
"You are helpful assistant"
},
{
role: "user",
content: prompt
}
]
const data = {
model: config.get("OPENAI_MODEL"),
messages,
max_tokens: MAX_TOKENS - tokens
}
let res = await fetch("https://api.openai.com/v1/chat/completions",
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.OPENAI_API_KEY}`
},
body: JSON.stringify(data)
})
res = await res.json()
if(res.error){
console.error(res)
}
return {
text: res.choices[0].message.content.trim(),
usage: res.usage,
tokens
}
}
export async function askQuestion(question, cb, opts = {}) {
try {
let redFlag = await Moderations(question)
if (redFlag) {
cb("Your prompt contains harmful content!")
return;
}
} catch (e) {
console.log(e)
cb(e)
return;
}
try {
const response = await chatGPT.sendMessage(question)
if (!response.text) {
throw "no response!"
}
cb(response.text)
} catch (e) {
cb("Oppss, something went wrong! (Error)")
console.error("chat error : " + e)
}
}