-
Notifications
You must be signed in to change notification settings - Fork 16
/
turbo.js
181 lines (137 loc) · 3.91 KB
/
turbo.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
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
import dotenv from "dotenv";
import { marked } from "marked";
import { Configuration, OpenAIApi } from "openai";
import { Telegraf } from "telegraf";
dotenv.config();
const chat_log = new Map();
let start = Date.now();
console.log("starting:", start);
const openai = new OpenAIApi(
new Configuration({
apiKey: process.env.APIKEY,
})
);
const bot = new Telegraf(process.env.TOKEN);
function clearMap() {
const millis = Date.now() - start;
if (Math.floor(millis / 1000) > 60 * 5) {
chat_log.clear();
start = Date.now();
console.log("chat log reloaded!");
}
}
bot.start((ctx) => {
ctx.reply(
"Hello, this is a bot that uses OpenAI.\nAsk anything using /ask followed by your question, if your directly texting the bot you don't need to use /ask, just ask your question."
);
});
async function askAI(question, userId) {
const userLog = chat_log.get(userId) ? chat_log.get(userId) : [];
const promptionList = [...userLog, { role: "user", content: question }];
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: promptionList,
});
const answer = completion.data.choices[0].message;
// console.log(answer);
if (answer) {
clearMap();
chat_log.set(userId, [...promptionList, answer]);
}
return answer.content;
}
async function generateImage(prompt) {
return await openai.createImage({
prompt: prompt,
n: 1,
size: "1024x1024",
});
}
async function editRequest(prompt) {
const response = await openai.createEdit({
model: "text-davinci-edit-001",
input: prompt,
instruction: "Fix the spelling and grammer mistakes",
});
const fix = response.data.choices[0].text;
return fix;
}
bot.command("ask", async (ctx) => {
const userId = ctx.update.message.from.id;
if (ctx.update.message.from.is_bot) {
return false;
}
const args = ctx.update.message.text.split(" ");
args.shift();
let question = args.join(" ");
if (question.length == 0) {
return ctx.reply("Type something after /ask to ask me stuff.", {
reply_to_message_id: ctx.message.message_id,
});
}
ctx.sendChatAction("typing");
try {
const completion = await askAI(question, userId);
ctx.reply(marked.parseInline(completion), {
reply_to_message_id: ctx.message.message_id,
parse_mode: "HTML",
});
} catch (error) {
console.log(error);
}
});
bot.command("image", async (ctx) => {
if (ctx.update.message.from.is_bot) {
return false;
}
const args = ctx.update.message.text.split(" ");
args.shift();
let question = args.join(" ");
if (question.length == 0) {
return ctx.reply("Type something after /image to get a pic.", {
reply_to_message_id: ctx.message.message_id,
});
}
ctx.sendChatAction("typing");
try {
const response = await generateImage(question);
ctx.replyWithPhoto(response.data.data[0].url, question);
} catch (error) {
console.log(error);
}
});
bot.command("fix", async (ctx) => {
if (ctx.update.message.from.is_bot) {
return false;
}
const args = ctx.update.message.text.split(" ");
args.shift();
let question = args.join(" ");
if (question.length == 0) {
return ctx.reply("Type something after /fix to correct your text.", {
reply_to_message_id: ctx.message.message_id,
});
}
ctx.sendChatAction("typing");
try {
const completion = await editRequest(question);
ctx.reply(marked.parseInline(completion), {
reply_to_message_id: ctx.message.message_id,
parse_mode: "HTML",
});
} catch (error) {
console.log(error);
}
});
bot.command("reload", async (ctx) => {
chat_log.clear();
ctx.sendChatAction("typing");
if (chat_log.size === 0) {
return ctx.reply("Conversation history reloaded!", {
reply_to_message_id: ctx.message.message_id,
});
}
});
bot.launch();
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));