-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathask.ts
270 lines (249 loc) · 8.66 KB
/
ask.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { Handler, Context, Callback } from "aws-lambda";
import ChatGPTMessage from "./src/chatgpt/chatgpt";
import ImageGPT from "./src/model/imageGPT";
import BotMessage from "./src/chatgpt/message";
import { getT, initLanguages, getLanguage } from "./src/lang/lang";
import { context as contextChatGPT } from "./src/chatgpt/context";
import { getAIfunctions } from "./src/chatgpt/functions";
import { BotMintData } from "./src/model/namesData";
import { startDeployment, generateFilename } from "./src/nft/nft";
import { copyAIImageToS3 } from "./src/imageHandler";
import { FileData } from "./src/model/fileData";
import { FilesTable } from "./src/table/files";
import HistoryTable from "./src/table/history";
import { getFormattedDateTime } from "./src/nft/nft";
import callLambda from "./src/lambda/lambda";
import { splitMarkdown } from "./src/chatgpt/split";
import Users from "./src/table/users";
import axios from "axios";
import { MAX_IMAGES, MAX_TOKENS } from "./src/model/userData";
const FILES_TABLE = process.env.FILES_TABLE!;
const HISTORY_TABLE = process.env.HISTORY_TABLE!;
const CHATGPT_TOKEN = process.env.CHATGPT_TOKEN!;
const CHATGPTPLUGINAUTH = process.env.CHATGPTPLUGINAUTH!;
const LIMIT_MESSAGE =
"You have reached the limit of bot use. To increase the limit, please contact [email protected]";
// "You have reached the limit of bot use. To increase the limit, send 10 MINA to B62qqRwyFetYpTRbFYtKTTJBYEgErzsuFZL2JWoG7PEHrkYtibLsRT8 and then send your name and payment transaction hash to [email protected]";
const chatgpt: Handler = async (
event: any,
context: Context,
callback: Callback
) => {
try {
//console.log("event", event);
//const body = JSON.parse(event.body);
console.log("ChatGPT ask request:", event);
let result: ImageGPT = <ImageGPT>{
image: "",
answerType: "text",
text: "Authentification failed",
};
if (event && event.auth && event.id && event.auth === CHATGPTPLUGINAUTH) {
await initLanguages();
const language = await getLanguage(event.id);
const users = new Users(process.env.DYNAMODB_TABLE!);
const user = await users.getItem(event.id);
if (user === undefined) {
console.error("User not found");
return 200;
}
let allowed_images = MAX_IMAGES;
if (user.allowed_images !== undefined)
allowed_images = user.allowed_images;
let allowed_tokens = MAX_TOKENS;
if (user.allowed_tokens !== undefined)
allowed_tokens = user.allowed_tokens;
if (user.images_created === undefined) user.images_created = 0;
if (user.total_tokens === undefined) user.total_tokens = 0;
if (
user.images_created >= allowed_images ||
user.total_tokens >= allowed_tokens
) {
console.error("ask: User reached the limit", {
id: user.id,
username: user.username,
allowed_images,
allowed_tokens,
images_created: user.images_created,
total_tokens: user.total_tokens,
user,
});
const bot = new BotMessage(event.id, language);
await sleep(1000);
await bot.message(LIMIT_MESSAGE);
await sleep(1000);
return 200;
}
const chat = new ChatGPTMessage(
CHATGPT_TOKEN,
language,
contextChatGPT,
await getAIfunctions(event.id)
);
result = await chat.message(event.id);
const bot = new BotMessage(event.id, language);
if (result.answerType === "text") {
if (result.text.length < 4000) await bot.message(result.text, false);
else {
const parts = splitMarkdown(result.text);
for (const part of parts) {
await bot.message(part, false);
await sleep(2000);
}
}
}
if (result.answerType === "image")
console.error("Image is not supported in this version");
console.log("ChatGPT result:", result);
await sleep(1000);
}
return 200;
} catch (error) {
console.error("catch", (<any>error).toString());
return 200;
}
};
const image: Handler = async (
event: any,
context: Context,
callback: Callback
) => {
try {
//console.log("event", event);
console.log("ChatGPT ask request:", event);
let result: ImageGPT = <ImageGPT>{
image: "",
answerType: "text",
text: "Authentification failed",
};
if (event && event.auth && event.auth === CHATGPTPLUGINAUTH) {
if (event.message && event.id) {
await initLanguages();
const language = await getLanguage(event.id);
const users = new Users(process.env.DYNAMODB_TABLE!);
const user = await users.getItem(event.id);
if (user === undefined) {
console.error("User not found");
return 200;
}
let allowed_images = MAX_IMAGES;
if (user.allowed_images !== undefined)
allowed_images = user.allowed_images;
let allowed_tokens = MAX_TOKENS;
if (user.allowed_tokens !== undefined)
allowed_tokens = user.allowed_tokens;
if (user.images_created === undefined) user.images_created = 0;
if (user.total_tokens === undefined) user.total_tokens = 0;
if (
user.images_created >= allowed_images ||
user.total_tokens >= allowed_tokens
) {
console.error("image: User reached the limit", {
id: user.id,
username: user.username,
allowed_images,
allowed_tokens,
images_created: user.images_created,
total_tokens: user.total_tokens,
user,
});
const bot = new BotMessage(event.id, language);
await sleep(1000);
await bot.message(LIMIT_MESSAGE);
await sleep(1000);
return 200;
}
const chat = new ChatGPTMessage(
CHATGPT_TOKEN,
language,
contextChatGPT
);
result = await chat.image(
event.message,
event.id,
//event.username,
false,
event.ai === "true" ? true : false
);
}
console.log("Image result", result);
if (event.id && result.image !== "") {
await initLanguages();
const language = await getLanguage(event.id);
const T = getT(language);
const timeNow = Date.now();
//const filename = generateFilename(timeNow) + ".jpg";
const filename = "image-" + timeNow + ".png";
const file = await copyAIImageToS3({
id: event.id,
filename,
url: result.image,
ai: true,
});
if (file === undefined) {
console.error("Image is undefined");
return 200;
}
const fileTable = new FilesTable(FILES_TABLE);
await fileTable.create(file);
const history = new HistoryTable(HISTORY_TABLE, event.id);
const urlBase = process.env.STORAGE_URL;
if (!urlBase) throw new Error("STORAGE_URL is not defined");
const url = urlBase + event.id + "/" + filename;
const image = `https://res.cloudinary.com/minanft/image/fetch/h_300,q_100,f_auto/${url}`;
console.log("Image URL", url);
axios
.get(image, {
responseType: "arraybuffer",
})
.then((response: any) => {
console.log("cloudinary ping - ai");
})
.catch((e: any) => console.error("cloudinary ping error - ai", e));
await history.addImage(
`image is generated by and received the user, file: ${JSON.stringify(
file
)}`,
image
);
const bot = new BotMessage(event.id, language);
await bot.image(url, {});
await sleep(1000);
await callLambda(
"ask",
JSON.stringify({
id: event.id,
auth: CHATGPTPLUGINAUTH,
})
);
} else if (event.id && result.image === "") {
await initLanguages();
const language = await getLanguage(event.id);
const T = getT(language);
const history = new HistoryTable(HISTORY_TABLE, event.id);
await history.add(
T("image.not.generated", { message: result.text }),
false
);
await callLambda(
"ask",
JSON.stringify({
id: event.id,
auth: CHATGPTPLUGINAUTH,
})
);
}
}
console.log("ChatGPT ask reply:", result.answerType, result.text);
await sleep(1000);
//}
return 200;
} catch (error) {
console.error("catch", (<any>error).toString());
return 200;
}
};
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export { chatgpt, image };