aboutsummaryrefslogtreecommitdiff
path: root/bot/src/plugins/chatbot.js
blob: 705f267a1c8fd78ccd98ce480cf1c6b5ef9ea6cf (plain) (blame)
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
import { ollamaGlobal } from '../storage/consts.js';
import { ollama } from '../utils/ollama.js';
import { AttachmentBuilder } from 'discord.js';
import { guildSettings } from '../models/guild-settings.js';
import 'dotenv/config';

export async function ChatBot(msg, args) {
    const guildSetting = await guildSettings.findOne({ where: { guildID: msg.guild.id } });

    if (!guildSetting.ollamaEnabled) return;
    if (!ollamaGlobal) return msg.reply('Sorry, the LLM chatbot feature has been turned off.');
    const loadingMessage = await msg.reply('Thinking...');

    try {
        const response = await ollama.chat({
            model: process.env.OLLAMA_MODEL,
            messages: [{ role: 'user', content: args }],
        });

        let content = response.message.content;

        if (content.length > 2000) {
            const attachment = new AttachmentBuilder(Buffer.from(content, 'utf-8'), { name: 'output.txt' });
            return await loadingMessage.edit({ files: [attachment] });
        } else {
            return await loadingMessage.edit({ content });
        }

    } catch (err) {
        console.error(err);
        await loadingMessage.edit(`Something went wrong. [Submit an issue at the AleeBot repository.](<https://github.com/Alee14/AleeBot/issues>)\nMessage:\n\`\`\`${err.stack}\`\`\``);
    }
}