Pythonによる日本語チャットボットの構築 in 2022 〜ChatterBotインストール編〜

はじめに

Pythonでチャットボットを作りたくなった。

ChatterBotが便利に使えそうだということで、インストールログを残しておく。

動作環境

  • Python3.9.13
  • macOS BigSur 11.6

インストール

まず仮想環境を準備する。

python3 -m venv chatbot

chatbotディレクトリができるので、移動する。その後、binディレクトリにactivateコマンドがあるので、sourceで仮想環境を起動する。

cd chatbot
source bin/activate

以下の手順を実行する。

git clone  https://github.com/feignbird/ChatterBot-spacy_fixed
pip3 install ./ChatterBot-spacy_fixed
pip3 install chatterbot-corpus
pip3 uninstall pyYAML
pip3 install pyYAML==5.3.1
python -m spacy download en_core_web_sm

pip3 install chatterbot-corpus実行時、chatterbot-corpusが要求するpyyamlのバージョンが低く、一時的にpyyaml(実行時はver 5.3.1)がアンインストールされ、ver 3.13がインストールされる。以下はその際のメッセージ抜粋。

Installing collected packages: PyYAML, chatterbot-corpus
  Attempting uninstall: PyYAML
    Found existing installation: PyYAML 5.3.1
    Uninstalling PyYAML-5.3.1:
      Successfully uninstalled PyYAML-5.3.1
  Running setup.py install for PyYAML ... done
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
chatterbot 1.1.0a7 requires pyyaml<5.4,>=5.3, but you have pyyaml 3.13 which is incompatible.
Successfully installed PyYAML-3.13 chatterbot-corpus-1.2.0

しかしながら、これはchatterbot-corpusのメンテがほぼされていないためであって、pyyamlは新しくしてもOKなのである。 そこで古いpyyamlをアンインストールし、改めて新しいものを入れるわけである。

以上のコマンド群が走れば、あとは日本語用のコーパス(会話テンプレート)をセットすれば完了する。

chatterbot-corpusのリポジトリをzipでダウンロードする。 github.com

ダウンロードされたファイルはchatterbot-corpus-master.zipである。 これを適当なところで解凍する。そして chatterbot_corpus-master/chatterbot_corpus/data/から、japaneseディレクトリをコピーする。 その後、venvの仮想環境フォルダ(chatbot)以下の chatbot/lib/python3.9/site-packages/chatterbot_corpus/data/ にコピーする。

テスト

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot('test')
trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train("chatterbot.corpus.japanese")
response = chatbot.get_response("こんにちは")
print(response)

これを適当な名前で保存する(test_chat.pyとか)。

そして実行する。

python3 test_chat.py

すると以下の実行結果が得られる。

Training ai.yml: [####################] 100%
Training botprofile.yml: [####################] 100%
Training computers.yml: [####################] 100%
Training conversations.yml: [####################] 100%
Training emotion.yml: [####################] 100%
Training food.yml: [####################] 100%
Training gossip.yml: [####################] 100%
Training greetings.yml: [####################] 100%
Training health.yml: [####################] 100%
Training history.yml: [####################] 100%
Training humor.yml: [####################] 100%
Training literature.yml: [####################] 100%
Training money.yml: [####################] 100%
Training movies.yml: [####################] 100%
Training politics.yml: [####################] 100%
Training psychology.yml: [####################] 100%
Training science.yml: [####################] 100%
Training sports.yml: [####################] 100%
Training trivia.yml: [####################] 100%
お元気ですか?

最後の「お元気ですか?」の部分はランダムに変わる(「こんにちは」に変わったりなど)。その返答の候補たちはgreetings.yamlに書いてある。つまりこれらのyamlファイルを調整することで返答は作り込める。

さらに進んで

上記のテストスクリプトは実行のたびにtraining(訓練)が走るので、非効率である。しかしながらchatbotを「保存」するということはできない。厳密には保存という概念ではなく、データベースの「更新」なのである。これがChatterBotの仕様である。

テストスクリプトを実行した後、db.sqlite3というファイルが保存されていることに気づく。これが先ほど訓練したchatbotのデータベースである。ファイル名はデフォルトの状態であり、例えば以下のようにすれば保存ファイルを変更できる。

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
 
chatbot = ChatBot(
    'test',
    database_uri='sqlite:///database.db'
)

trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train("chatterbot.corpus.japanese")

データベース名はdatabase.dbである。このデータベースを再度読み込んでchatbotを構築する際には、

from chatterbot import ChatBot
 
chatbot = ChatBot(
    'test',
    database_uri='sqlite:///database.db',
    # read_only=True  # 読み込み専用、上書きしないときにはこれを指定する
)

response = chatbot.get_response("こんにちは")
print(response)

を使う。データベースを更新済なので、再度の訓練は不要である。当然、新たにyamlファイルを更新すれば、訓練してデータベースを更新する必要はある。

ChatterBotにはまだいくつか訓練するためのクラスが存在しており、必ずしもyamlの編集・更新が必要というわけではないらしい。

おわりに

ひとまずインストールは完了した。

ChatterBotを「賢く」するには、何らかの方法でデータベースを更新する必要がある(自動・半自動・手動)。 それはまた記事を改めて書くことにする。

参考

インストール全般はこちらのスレッドを参考にした。 github.com

言語モデルを導入して賢くしていくには、以下の記事が参考になろう。 zerofromlight.com