Get startedGet started for free

Creating SQL from natural language

Now you'll write a respond() function that can handle messages like "I want an expensive hotel in the south of town" and respond appropriately according to the number of matching results in a database. This is an important functionality for any database-backed chatbot.

Your find_hotels() function from the previous exercises has already been defined for you, along with a Rasa NLU interpreter object, which can handle hotel queries, and a list of responses, which you can explore in the Shell.

This exercise is part of the course

Building Chatbots in Python

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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)
Edit and Run Code