Skip to content

Commit b28b2ec

Browse files
committed
feat: Grzesiu potrafi odpowiadać na pytania
1 parent f5222e4 commit b28b2ec

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/commands/grzesiu.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@ const openai = new OpenAIApi(configuration);
1212
const BANNED_PATTERNS = /[`\[\]{}\(\)]|http/g;
1313
const COOLDOWN = 60;
1414
const GRZESIU_DELAY = 1500;
15+
const GRZESIU_NAME = 'grzegorz';
1516

1617
const grzesiu: Command = {
1718
name: 'grzesiu',
1819
description: 'Użyj tego, gdy tęsknisz za Grzesiem',
19-
args: 'prohibited',
20+
args: 'optional',
2021
cooldown: COOLDOWN,
21-
async execute(msg) {
22-
const prompt = await generateGrzesiuPrompt();
22+
async execute(msg, args) {
23+
const username = msg.member?.displayName || msg.author.username;
24+
const prompt = await generateGrzesiuPrompt(username, args.join(' '));
2325

2426
const response = await openai.createCompletion('text-davinci-001', {
2527
prompt,
2628
temperature: 1,
2729
max_tokens: 64,
2830
top_p: 1,
29-
best_of: 4,
3031
frequency_penalty: 0,
3132
presence_penalty: 2,
33+
best_of: 4,
3234
});
3335

3436
if (!response.data.choices?.[0]?.text) {
@@ -38,7 +40,9 @@ const grzesiu: Command = {
3840
const messages = response.data.choices[0].text
3941
.split('\n')
4042
.map((l) => l.trim())
41-
.filter((l) => l.length > 0);
43+
.filter((l) => l.startsWith(`${GRZESIU_NAME}:`))
44+
.flatMap((l) => l.split(`${GRZESIU_NAME}:`))
45+
.filter((l) => l.trim().length > 0);
4246

4347
return messages.reduce(async (acc, message) => {
4448
await acc;
@@ -61,15 +65,23 @@ const getRandomIndices = (num: number, max: number) => {
6165
return [...set];
6266
};
6367

64-
const generateGrzesiuPrompt = async () => {
68+
const generateGrzesiuPrompt = async (username: string, question: string) => {
6569
const indices = getRandomIndices(100, grzesJson.length);
6670
const uniqueLines = [...new Set(indices.map((idx) => grzesJson[idx].trim()))].filter(
6771
(line) => !BANNED_PATTERNS.test(line) && line.length > 0,
6872
);
6973

70-
const prompt = uniqueLines.reduce((txt, line) => {
71-
const newTxt = txt + line + '\n';
72-
return newTxt.length <= MAX_TOKENS ? newTxt : txt;
74+
const getFullConvo = (txt: string, username: string, question: string) => {
75+
if (question) {
76+
return `${txt.trim()}\n${username}: ${question}\n${GRZESIU_NAME}:`;
77+
}
78+
return `${txt.trim()}\n${GRZESIU_NAME}:`;
79+
};
80+
81+
const txt = uniqueLines.reduce((txt, line) => {
82+
const newTxt = txt + `${GRZESIU_NAME}: ` + line + '\n';
83+
const fullConvo = getFullConvo(newTxt, username, question);
84+
return fullConvo.length <= MAX_TOKENS ? newTxt : txt;
7385
}, '');
74-
return prompt;
86+
return getFullConvo(txt, username, question);
7587
};

0 commit comments

Comments
 (0)