Virtual assistants and accessing data

Building Chatbots in Python

Alan Nichol

Co-founder and CTO, Rasa

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
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';
Building Chatbots in Python

SQLite with Python

import sqlite3

conn = sqlite3.connect('hotels.db')
c = conn.cursor()
c.execute("SELECT * FROM hotels WHERE area='south' and pricerange='hi'")
<sqlite3.Cursor at 0x10cd5a960>
c.fetchall()
[('Grand Hotel', 'hi', 'south', 5)]
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)
Building Chatbots in Python

Let's practice!

Building Chatbots in Python

Preparing Video For Download...