0% found this document useful (0 votes)
80 views

Building Chatbots in Python Chapter3

This document discusses building chatbots in Python that can interact with databases and APIs. It covers using SQL to query databases from Python scripts, including how to prevent SQL injection. It also demonstrates parsing user messages to extract parameters, dynamically generating SQL queries based on those parameters, and selecting appropriate responses based on query results. Finally, it discusses implementing incremental slot filling, where each user message provides additional constraints, and handling negation of entities. The goal is to build chatbots that can understand natural language queries and safely access external data sources to respond to users.

Uploaded by

Fgpeqw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Building Chatbots in Python Chapter3

This document discusses building chatbots in Python that can interact with databases and APIs. It covers using SQL to query databases from Python scripts, including how to prevent SQL injection. It also demonstrates parsing user messages to extract parameters, dynamically generating SQL queries based on those parameters, and selecting appropriate responses based on query results. Finally, it discusses implementing incremental slot filling, where each user message provides additional constraints, and handling negation of entities. The goal is to build chatbots that can understand natural language queries and safely access external data sources to respond to users.

Uploaded by

Fgpeqw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

DataCamp Building Chatbots in Python

BUILDING CHATBOTS IN PYTHON

Virtual assistants and


accessing data

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

Bill's Burgers hi east 3

Moe's Plaice low north 3

Sushi Corner mid center 3

SELECT * from restaurants;

SELECT name, rating from restaurants;

SELECT name from restaurants WHERE area = 'center' AND pricerange = 'hi';
DataCamp Building Chatbots in Python

SQLite with Python


In [1]: import sqlite3

In [2]: conn = sqlite3.connect('hotels.db')

In [3]: c = conn.cursor()

In [4]: c.execute("SELECT * FROM hotels WHERE area='south' and pricerange='hi'")


Out[4]: <sqlite3.Cursor at 0x10cd5a960>

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

BUILDING CHATBOTS IN PYTHON

Let's practice!
DataCamp Building Chatbots in Python

BUILDING CHATBOTS IN PYTHON

Exploring a DB with natural


language

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

Parameters from text


In [1]: message = "a cheap hotel in the north"

In [2]: data = interpreter.parse(message)

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 [5]: for ent in data["entities"]:


...: params[ent["entity"]] = ent["value"]

In [6]: params
Out[6]: {'location': 'north', 'price': 'lo'}
DataCamp Building Chatbots in Python

Creating a query from parameters


In [7]: query = "select name FROM hotels"

In [8]: filters = ["{}=?".format(k) for k in params.keys()]

In [9]: filters
Out[9]: ['price=?', 'location=?']

In [10]: conditions = " and ".join(filters)

In [11]: conditions
Out[11]: 'price=? and location=?'

In [12]: final_q = " WHERE ".join([query, conditions])

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 [2]: results = c.fetchall()

In [3]: len(results)
Out[3]: 4

In [4]: index = min(len(results), len(responses)-1)

In [5]: responses[index]
Out[5]: '{} is one option, but I know others too :)'
DataCamp Building Chatbots in Python

BUILDING CHATBOTS IN PYTHON

Let's practice!
DataCamp Building Chatbots in Python

BUILDING CHATBOTS IN PYTHON

Incremental slot filling and


negation

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

"where should I go for dinner?" "what about Sally's Sushi Place?"

"no I don't like sushi" "ok, what about Joe's Steakhouse?"


DataCamp Building Chatbots in Python

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 [2]: indices = [1, 4]

In [3]: ents, negated_ents = [], []

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

BUILDING CHATBOTS IN PYTHON

Let's practice!

You might also like