始める無料で始める

文脈に応じた質問への対応

ユーザーがつまずいたとき、ボットに助けを求めることがあります。

この演習では、コーヒーボットが各ステップの説明をユーザーに伝えられるようにします。 前の演習と同様に、ユーザーが受け取る回答は、フローのどの段階にいるかによって変わります。

この演習はコースの一部です

Python でチャットボットを作る

コースを見る

演習の手順

  • policy_rules に2つのルールを追加して、INIT または CHOOSE_COFFEE の状態で "ask_explanation" インテントを処理できるようにしましょう。
  • send_messages() 関数の中で、statemsg を引数として send_message() 関数を呼び出し、新しい state を定義します。設定が完了したら、'回答を送信' をクリックしてメッセージを送信し、ボットの応答を確認しましょう。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Define the states
INIT=0 
CHOOSE_COFFEE=1
ORDERED=2

# Define the policy rules dictionary
policy_rules = {
    (____, "____"): (INIT, "I'm a bot to help you order coffee beans"),
    (____, "order"): (CHOOSE_COFFEE, "ok, Colombian or Kenyan?"),
    (____, "specify_coffee"): (ORDERED, "perfect, the beans are on their way!"),
    (____, "____"): (CHOOSE_COFFEE, "We have two kinds of coffee beans - the Kenyan ones make a slightly sweeter coffee, and cost $6. The Brazilian beans make a nutty coffee and cost $5.")    
}

# Define send_messages()
def send_messages(messages):
    state = INIT
    for msg in messages:
        state = ____(____)

# Send the messages
send_messages([
    "what can you do for me?",
    "well then I'd like to order some coffee",
    "what do you mean by that?",
    "kenyan"
])
コードを編集して実行