Building Chatbots in Python Chapter3
Building Chatbots in Python Chapter3
Alan Nichol
Co-founder and CTO, Rasa
DataCamp Building Chatbots in Python
Virtual assistants
Common chatbot use cases:
Scheduling a meeting
Booking a flight
Searching for a restaurant
Require information about the outside world
Need to interact with databases or APIs
DataCamp Building Chatbots in Python
Basic SQL
name pricerange area rating
SELECT name from restaurants WHERE area = 'center' AND pricerange = 'hi';
DataCamp Building Chatbots in Python
In [3]: c = conn.cursor()
In [5]: c.fetchall()
Out[5]: [('Grand Hotel', 'hi', 'south', 5)]
DataCamp Building Chatbots in Python
SQL injection
# Bad Idea
query = "SELECT name from restaurant where area='{}'".format(area)
c.execute(query)
# Better
t = (area,price)
c.execute('SELECT * FROM hotels WHERE area=? and price=?', t)
DataCamp Building Chatbots in Python
Let's practice!
DataCamp Building Chatbots in Python
Alan Nichol
Co-founder and CTO, Rasa
DataCamp Building Chatbots in Python
Example messages
"Show me a great hotel"
"I'm looking for a cheap hotel in the south of town"
"Anywhere so long as it's central"
DataCamp Building Chatbots in Python
In [3]: data
Out[3]:
{'entities': [{'end': '7', 'entity': 'price', 'start': 2, 'value': 'lo'},
{'end': 26, 'entity': 'location', 'start': 21, 'value': 'north'}],
'intent': {'confidence': 0.9, 'name': 'hotel_search'}}
In [4]: params = {}
In [6]: params
Out[6]: {'location': 'north', 'price': 'lo'}
DataCamp Building Chatbots in Python
In [9]: filters
Out[9]: ['price=?', 'location=?']
In [11]: conditions
Out[11]: 'price=? and location=?'
In [13]: final_q
Out[13]: 'SELECT name FROM hotels WHERE price=? and location=?'
DataCamp Building Chatbots in Python
Responses
In [1]: responses = [
"I'm sorry :( I couldn't find anything like that",
"what about {}?",
"{} is one option, but I know others too :)"
]
In [3]: len(results)
Out[3]: 4
In [5]: responses[index]
Out[5]: '{} is one option, but I know others too :)'
DataCamp Building Chatbots in Python
Let's practice!
DataCamp Building Chatbots in Python
Alan Nichol
Co-founder and CTO, Rasa
DataCamp Building Chatbots in Python
Incremental filters
DataCamp Building Chatbots in Python
Basic Memory
In [1]: def respond(message, params):
...: # update params with entities in message
...: # run query
...: # pick response
...: return response, params
# initialise params
In [2]: params = {}
# message comes in
In [3]: response, params = respond(message, params)
DataCamp Building Chatbots in Python
Negation
Negated entities
assume that "not" or "n't" just before an entity means user wants to exclude this
normal entities in green, negated entities in purple
DataCamp Building Chatbots in Python
Catching negations
In [1]: doc = nlp('not sushi, maybe pizza?')
In [4]: start = 0
...: for i in indices:
...: phrase = "{}".format(doc[start:i])
...: if "not" in phrase or "n't" in phrase:
...: negated_ents.append(doc[i])
...: else:
...: ents.append(doc[i])
...: start = i
DataCamp Building Chatbots in Python
Let's practice!