ECSを用いた質問回答ボットの作成

 
今回はDocker imageであるtomomano/qabotを用いて自動質問回答ボットをAWS内の機能の一つであるECSで動作させました。
 
また、Fargateを用いることにより、一度に多数の質問に回答できるようにしました。



 

自動質問回答システム

 
まず、AWS環境とは別に、WSL内で自動質問回答システムを動作させました。
 
今回させていただくDocker imageは以下のコマンドによりダウンロードすることができます。

$ sudo docker pull tomomano/qabot:latest

 
次に使用する文章と質問をそれぞれ以下のように変数contextとquestionに代入しました。

$ context="Albert Einstein (14 March 1879 - 18 April 1955) was a German-born theoretical physicist who developed the theory of relativity, one of the two pillars of modern physics
(alongside quantum mechanics). His work is also known for its influence on the philosophy of science. He is best known to the general publicfor his mass-energy equivalence
formula E = mc2, which has been dubbed the world's most famous equation. He received the 1921 Nobel Prize in Physics for his services to theoretical physics, and especially
for his discovery of the law of the photoelectric effect, a pivotal step in the development of quantum theory."
$ question="In what year did Einstein win the Nobel prize ?"

 
変数の準部ができたので以下のコマンドによりコンテナを実行します。

$ sudo docker run tomomano/qabot "${context}" "${question}" foo --no_save

 
実行結果は以下の画像のようになりました。
 

 
ボットは'1921'と回答しており、正しい返答をしていることがわかります。
  
また、このボットには自信度としてscoreが0から1の値を出力します。
 
今回、ボットは0.9852628111839294と出力しているのでこの回答に強い自信があることも見て取れます。


 
次は以下のように、先ほどより難しい質問を与えました。

$ question="Why did Einstein win the Nobel prize ?"
$ sudo docker run tomomano/qabot "${context}" "${question}" foo --no_save

 
実行結果は以下のようになりました。
 

 
今回も正しい回答を得ることができましたが、scoreの値が0.5760034918785095となっており先ほどの質問より自信度が小さくなりました。




 

スタックのデプロイ

 
ここまでで実行してきた質問回答システムをAWSのECS上に実装します。
 
スタックのデプロイは以下のコマンドより行うことができます。
 
デプロイにはDocker imageであるtomomano/labcのhandson/qa-botを使用しています。

$ cd handson/qa-bot
$ python3 -m venv .env
$ source .env/bin/activate
$ pip install -r requirements.txt
$ cdk deploy

 
また、デプロイの詳しい手順は以下の記事にまとめています。
 
nissin-geppox.hatenablog.com


 
デプロイが成功するとターミナル上に以下のような出力が表示されます。
 

 
さらに、AWSのECSコンソール上では青に色を変えた文字列と同じ名前のタスク定義が追加されました。
 




 

タスクの実行

 
作成したECSクラスターには以下のコマンドにより文章と質問をリクエストすることができます。

$ python run_task.py ask "A giant peach was flowing in the river. She picked it up and brought it home. Later, a healthy baby was born from the peach. She named the baby Momotaro." "What is the name of the baby?"

 
このリクエストに対し、以下の画像のように1つのタスクが起動しました。
 

 
また、ターミナル上の結果は以下のようになりました。
 




 

複数のタスクの実行

 
以下のコマンドにより複数のFargateインスタンスを起動し、同時に多数の質問を処理することができます。
 

$ python run_task.py ask_many

 
このリクエストに対し、以下の画像のように9つのタスクが起動しました。
 

 
また、ターミナル上の結果は以下のようになりました。
 




 

タスクの削除

 
最後に以下のコマンドでタスクを削除しました。

$ cdk destroy