forked from Corey-Stowe/Backend_api_GPT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
130 lines (123 loc) · 3.81 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//config//
const apikey = "your API keys";
const orgkey = "your 0RG keys";
const port = 80;
//////////////////////////
const express = require('express')
const fetch = require('node-fetch')
const query = require('readline-sync')
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
//gpt-3.5-turbo
async function getOpenAIResponse(user) {
try {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apikey,
'OpenAI-Organization': orgkey
},
body: JSON.stringify({
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": user
}
]
})
});
const data = await response.json();
const jsonString = JSON.stringify(data);
const send = jsonString.substring(jsonString.indexOf('"content":') + 11, jsonString.indexOf('"}', jsonString.indexOf('"content":')) + 1);
return send;
} catch (error) {
console.error(error);
return "Sorry, something went wrong!";
}
}
//text-davinci-003
async function getOpenAIResponsev3(user) {
try {
const response = await fetch('https://api.openai.com/v1/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apikey
},
body: JSON.stringify({
prompt: user,
max_tokens: 2000,
n: 1,
temperature: 0,
model: 'text-davinci-003',
})
});
const data = await response.json();
const jsonString = JSON.stringify(data);
const send = jsonString.slice(jsonString.indexOf('{"text":"'));
return send;
} catch (error) {
console.error(error);
return "Sorry, something went wrong!";
}
}
// create image
async function generateImage(user) {
try {
const response = await fetch('https://api.openai.com/v1/images/generations', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apikey,
'OpenAI-Organization': orgkey
},
body: JSON.stringify({
"model": "image-alpha-001",
"prompt": "<img src='" + user + "'>",
"num_images": 1,
"size": "1024x1024",
"response_format": "url"
})
});
const data = await response.json();
return data.data[0].url;
} catch (error) {
console.error(error);
return "Sorry, something went wrong!";
}
}
//key main api
app.all('/', async (req, res) => {
const user = req.body.user || req.query.user;
if (!user) {
return res.status(500).send("mày đéo hỏi tao cái gì sao tao biết trả lời ?");
}
const authkey = req.body.authkey || req.query.authkey;
const ver = req.body.ver || req.query.ver;
console.log("Got request: " + user + "|" + ver + "|" + authkey);
if (authkey !== "pk_live_51IkykwJJUWs67pm7krR6XyF") {
return res.status(401).send(`{"error": "invalid authkey"}`);
}
if (ver == "gpt-3.5-turbo") {
const send = await getOpenAIResponse(user);
console.log(send);
res.send("{" + `"msg req data": "${user}` + `","msg rep data": "${send},` + `" Backend api by stowe"` + "}");
} else if (ver == "text-davinci-003") {
const send = await getOpenAIResponsev3(user);
console.log(send);
res.send("{" + `"msg req data": "${user}"` + `"msg rep data": "${send}` + "Backend api by stowe" + "}");
} else if (ver == "image-alpha-001") {
const send = await generateImage(user);
console.log(send);
res.send(send);
}
else {
res.send("missing param <br> what version do you want ?");
}
});
app.listen(port, () => {
console.log(`App was running at http://localhost:${port}`)
})