Chapter7 Hand-on#3:AWSで自動質問回答ロボットを走らせる

今回は学習済みの言語モデルを用いて,自動質問回答システムを作成する。

自動質問回答システムとは,文章(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 public for 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

結果は以下のようになった。

結果の見方は以下に示す。

{'score': 「回答の自信度(0~1)」,
'start': 「回答の文字の始まり」,
'end': 「回答の文字の終わり」」,
'answer': '「回答」'}

違う質問もしてみる。

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

結果は以下のようになった。


スタックのデプロイ

以下のコマンドを実行して,スタックのデプロイを行う

# プロジェクトのディレクトリに移動
$ cd handson/qa-bot#

# venvを作成し,依存ライブラリのインストールを行う
$ python3 -m venv .env
$ source .env/bin/activate
$ pip install -r requirements.txt

# デプロイを実行
$ cdk deploy

AWSコンソールからデプロイしたスタックが確認できる

確認したら,以下のコマンドを実行する。

$ 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?"

すると,以下のような回答が帰ってくる。

先程と同様に,0~1の信頼度で回答が帰ってくる。


タスクの同時実行

まず,次のコマンドを実行する。 これにより,複数の質問を一度に送信できる。

$ python run_task.py ask_many

EC2コンソールからタスクの一覧を確認し,すべてのステータスが「STOPPED」になったことを確認した上で,以下のコマンドを実行する。

$ python run_task.py list_answers

以下のような出力が得られた。 今回は数個程度の質問だけだったが,このシステムは数百の質問に対応することができる。

最後に,スタックを削除して終了。

$ cdk destroy