LoginSignup
10
7

Discord.pyとハギングフェイスを用いてローカルLLMで動くDiscord_botを作る

Posted at

Discord.pyの使い方はこちらの動画を参考にさせていただきました。

目次

・Discor Developer Portal の使い方、設定
・Discord.py のインストール
・Discord bot の記述
・LLMが使用できるようにする

Discor Developer Portal の使い方、設定

まずはDiscor Developer Portalにアクセスします。
そうすると以下の画面が出てくるはずなので、右上のnew applicationを選択し、名前を入力します(この名前はボットの表示名になるわけではないです。)

Discord Developer Portal — My Applications - Sidekick 2024_04_07 13_50_22.png

Discord Developer Portal — My Applications - Sidekick 2024_04_07 13_51_19.png

その後、settingsbotページを開き、Privileged Gateway Intentsの3つの項目をすべてオンにし、下にホップアップで出てくるsave changesを押します。

Discord Developer Portal — My Applications - Sidekick 2024_04_07 13_55_19.png

終わったら、Oauth2タグに移動し、SCOPESではBOTをオンにし、BOT PERMISSIONSではAdministratorをオンにします。

Discord Developer Portal — My Applications - Sidekick 2024_04_07 14_26_35.png

オンにしたら、下のURLにアクセスし、サーバーに追加します。

Discord Developer Portal — My Applications - Sidekick 2024_04_07 14_26_47.png

その後、botタグに移動しreset tokenを押します。パスコードを入力後、出てきたトークンをコピーし、保存しておきます。

Discord Developer Portal — My Applications - Sidekick 2024_04_07 14_46_48.png

Discord.py のインストール

次は環境にDiscord.pyをインストールします。

Discord.pyがPython 3.12 では動かない場合があるので、動かなかったらPython 3.11をインストールして、実行してみてください。

環境の用意

venvを用いて仮想環境を作ります。

$ python -m venv env 
$ ./env/Script/activate

仮想環境では動かない場合があるので、自分は本番環境でやりました。

Discord.pyをインストール

$ pip install Discord.py

その後、パイソンファイル(自分の場合はDiscordbot.py)をつくり、以下の内容を記述します。一番下のTOKENのところに先ほどコピーしたトークンを入力します。

Discordbot.py
import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())


#起動したときに起こるイベント
@bot.event
async def on_ready():
    print("準備完了")
    await bot.tree.sync()

#/hello
@bot.tree.command(name="hello", description="hello.")
async def ping(interaction: discord.Interaction):
    await interaction.response.send_message("hello ! ")


bot.run("TOKEN")

これを実行し、ボットのいるサーバーで"/hello"と入力すると、hello ! とかえって来るはずです。

Discord _ アカウントへのアクセスを許可します - Sidekick 2024_04_07 15_15_12.png

LLMが使用できるようにする

LLMが使用できるようにします。今回使用するのは自分が開発したArrowSmart_1.7b_instructionを使います。

ライブラリをインストール

LLMを使えるようにするため、ライブラリをインストールします。

$ pip install transformers
$ pip install pipline
$ pip install protobuf
$ pip install accelerate
$ pip install sentencepiece
$ pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

もしNvidiaのGPUがない場合は一番下のpip install を以下のものに変えます。

$ pip install torch

discordbot.pyの内容を変更

内容を以下のものに変更します。def con が推論するための関数です。

Discordbot.py
import discord
from discord.ext import commands
from discord import app_commands

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline

model_path = "DataPilot/ArrowSmart_1.7b_instruction"

model = AutoModelForCausalLM.from_pretrained(model_path,device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_path)

generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)

torch.cuda.empty_cache()

bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())

def con(in_text):
    text = generator(
        f"ユーザー: {in_text} システム: ",
        max_length = 500,
        do_sample = True,
        temperature = 0.7,
        top_p = 0.9,
        top_k = 0,
        repetition_penalty = 1.1,
        num_beams = 1,
        pad_token_id = tokenizer.pad_token_id,
        num_return_sequences = 1,
        truncation=True
    )
    json_text = text[0]
    result_text = json_text["generated_text"]
    output_text = result_text.replace('ユーザー: '+str(in_text)+' システム:', '')
    output_text = result_text.replace('<0x0A>','')
    return output_text


#起動したときに起こるイベント
@bot.event
async def on_ready():
    print("準備完了")
    await bot.tree.sync()


#/hello
@bot.tree.command(name="hello", description="hello.")
async def ping(interaction: discord.Interaction):
    await interaction.response.send_message("hello ! ")


@bot.tree.command(name="say", description="LLMが返信します。")
@app_commands.describe(saying_msg="送信したい文章を入れてください。")
async def say(interaction: discord.Interaction, saying_msg: str):
    await interaction.response.defer()  # ここで処理中であることを伝える
    res = con(saying_msg)
    await interaction.followup.send(f"{res}") 


bot.run("TOKEN")

このプログラムを実行後、ボットがいるサーバー上で"/sayと打ち、メッセージを送信するとLLMが考えた返信が返ってきます。

初回ロード時にはモデルを読み込むため、時間がかかりますが正常です。

終わり

これで、自作のLLMをdiscord上で友達に見せられますし、簡単にボットを実装することができます。

10
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
7