0
0

More than 3 years have passed since last update.

Amazon Lexでnot authorized to performでJupyterNotebookで呼び出せない

Posted at

はじめに

使ってわかったAWSのAIという本を学習している中で、Amazon Lexというチャットボットを作成できるサービスがありました。

本書が作成されと時にはAmazon Lexは日本語版に対応していなかったようですが、2021年現在は日本語版に対応していますので、せっかくならと日本語版で動かしてみました。
本書通りに動かしたところ途中でエラーになり、躓いたので修正箇所についてまとめたいと思います。

問題

Amazon Lexで本書通りにbuildまで行いました。
そして、JupyterNotebookで以下のコードを実行しました。

import boto3
import json

client = boto3.client('lex-runtime', region_name='us-east-1')

text = 'book a car'

response = client.post_text(
    botName='BookTrip',
    botAlias='Dev',
    userId='user01',
    inputText=text
)

print(response)

こちらは本書通りになります。すると以下のようなエラーがでました。

---------------------------------------------------------------------------
NotFoundException                         Traceback (most recent call last)
<ipython-input-12-b2d86ae9a50e> in <module>()
     10     botAlias='Dev',
     11     userId='user01',
---> 12     inputText=text
     13 )
     14 

/opt/conda/lib/python3.6/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
    355                     "%s() only accepts keyword arguments." % py_operation_name)
    356             # The "self" in this scope is referring to the BaseClient.
--> 357             return self._make_api_call(operation_name, kwargs)
    358 
    359         _api_call.__name__ = str(py_operation_name)

/opt/conda/lib/python3.6/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
    659             error_code = parsed_response.get("Error", {}).get("Code")
    660             error_class = self.exceptions.from_code(error_code)
--> 661             raise error_class(parsed_response, operation_name)
    662         else:
    663             return parsed_response

NotFoundException: An error occurred (NotFoundException) when calling the PostText operation: There is no alias named Dev for the bot named BookTrip. Choose another alias.

本書通りの設定ではできませんでした。ですので修正をします。

解決方法

日本語で動かすにはいくつか変更点があります。

1. リージョンを日本に変更する

client = boto3.client('lex-runtime', region_name='us-east-1')

を以下のように修正します。

client = boto3.client('lex-runtime', region_name='ap-northeast-1')

2. textを変更する

日本語なので、日本語で質問する必要がありますので以下のように編集します。

text = 'book a car'
text = 'レンタカー予約'

3. botNameを変更する

botNameは変更いらないかと思いましたが、確認したところ初期のBotNameはBookTripではなくBookTrip_jaJPと最後に_jpJPがつくようです。

botName='BookTrip',

を以下のように修正します。

botName='BookTrip_jaJP',

また、自分で名前を付けている場合はAWSコンソールから確認できます。

bandicam 2021-05-23 23-18-09-769.jpg

最終コード

最後は以下のようになり、無事動きました。

import boto3
import json

client = boto3.client('lex-runtime', region_name='ap-northeast-1')

text = 'レンタカー予約'

response = client.post_text(
    botName='BookTrip_jaJP',
    botAlias='Dev',
    userId='user01',
    inputText=text
)

print(response)

おわりに

本通りに動かしているのであれば英語ですので、エラーになることはないと思いますが、日本語あるなら日本語でやってみようという方に向けてまとめました。

今後は精度なども確認していきたいところです。

参考サイト

0
0
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
0
0