7章

目的

AWSで文章と質問を与え,それに回答するbotを走らせる


準備

以前の記事↓でやったように,pullを使い,ローカルにDockerImageをダウンロードする.

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 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."

質問と実行

contextを使い,文が書けると,質問文を書いて実行する.

question="In what year did Einstein win the Nobel prize ?"

質問ができれば実行する.

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

実行すれば,下の写真の最下行のようなものが返ってくる.

ここの「score」が正解している確率.「start」と「end」が何文字目に答えがあるか.「answer」が答えとなっている.

別の質問をして,もう一度実行してみよう.

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

以下のような答えが返ってきて正しくできていることがわかる.


スタックのデプロイとタスクの実行

まずはスタックのデプロイから,ディレクトリを移動してやっていく.

cd handson/qabot

awsのシークレットキーを設定後,以下も入力,デプロイを実行する.

python3 -m venv .env
source .env/bin/activate
pip install -r requirements.txt
cdk deploy


次にタスクを実行する.今回は,handson/qabotにrun_task.pyがあるので,それを使って実行する.
ためしに以下を入力してみる

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

これで実行すると,写真のようなものが返ってくる.答えは桃太郎で質問の内容と一致していることがわかる.


タスクを同時に実行する

先程のrun_ask.pyには,複数同時に質問する事のできるask_manyをつけることができる.
質問の内容はproblems.jsonにある.

python3 run_ask.py ask_many

awsのec2→クラスター→タスクから見てみると,以下のような画面になっているはず.

ステータスがSTOPPEDになれば質問の回答を得るために以下を入力

python run_ask.py list_answers

すると多数の回答が返ってくる.


後片付け

run_task.pyで入力した質問を削除するために以下を入力.その後,使用したスタックも削除する.

python run_task.py clear
cdk destroy


空になっていることがわかる.


まとめ

7章ではqabotを使って問題文と質問を送り,その回答をする自然言語処理に触れた.
タスクを同時に実行しても,高い確率で正解を導いていることもわかった.