自然言語からSQLを生成する
次は、"I want an expensive hotel in the south of town" のようなメッセージを処理し、データベースの検索結果の件数に応じて適切な返答を生成する respond() 関数を作成しましょう。これは、データベースと連携するチャットボットにとって重要な機能です。
前の演習で作成した find_hotels() 関数はすでに定義されています。また、ホテルに関する問い合わせを処理できる Rasa NLU の interpreter オブジェクトと、responses のリストも用意されています。シェルで内容を確認してみましょう。
この演習はコースの一部です
Python でチャットボットを作る
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Define respond()
def respond(message):
# Extract the entities
entities = ____.____(____)["____"]
# Initialize an empty params dictionary
params = {}
# Fill the dictionary with entities
for ent in entities:
params[ent["entity"]] = str(ent["value"])
# Find hotels that match the dictionary
results = ____
# Get the names of the hotels and index of the response
names = [r[0] for r in results]
n = ____(len(results),3)
# Select the nth element of the responses array
return ____[n].____(*names)