98
Artificial Intelligence And Python: Developing a Conversational Interface using Python Jerry Felix

AI and Python: Developing a Conversational Interface using Python

  • Upload
    amyiris

  • View
    13.955

  • Download
    10

Embed Size (px)

DESCRIPTION

Slides from PyOhio 7/25/2009:*Director's Cut* with Previously Unseen Deleted Scenes :-) 40 extra slides

Citation preview

Page 1: AI and Python: Developing a Conversational Interface using Python

Artificial Intelligence And Python:

Developing a Conversational Interface

using Python

Jerry Felix

Page 2: AI and Python: Developing a Conversational Interface using Python

Exposure to Conversational Interfaces

Have you leave convinced: Universally applicable today Need to take action

Examples! Personal goals:

Most memorable presentation Learn from the audience

Session Objectives

Page 3: AI and Python: Developing a Conversational Interface using Python

Motivation – Why should you care?

Tools and Resources

Technical bits and Program Samples

Open Source Future Direction

Session Format

Page 4: AI and Python: Developing a Conversational Interface using Python

Wisdom from my son:

“Dad, you’re lucky. When you were growing up, people had epic challenges.

Why doesn’t my generation have any?”

Page 5: AI and Python: Developing a Conversational Interface using Python

…the goal, before this

decade is out, of landing a man on the

moon…

Page 6: AI and Python: Developing a Conversational Interface using Python

What’s our epic challenge?

What stretch goal CAN we accomplish by 2019?

Two trends…

Page 7: AI and Python: Developing a Conversational Interface using Python

- Ray Kurzweil

Trend

#1

Page 8: AI and Python: Developing a Conversational Interface using Python

2.5 Search Words

3.3 Search Words

7 - 15 “Search”

Words

Search Now Uses More Than Search Words

2004 2009 2019

Location, Previous Searches

Location, Previous

Searches, Occupation, Hobbies,

ContextTr

end

#2

Page 9: AI and Python: Developing a Conversational Interface using Python

A $1000 computer = power of human brain.

People won’t search the internet,they’ll converse with it.

Good user interfaces will be conversational.

By 2019:

Why you should care!

Every interface. Every application.

Page 10: AI and Python: Developing a Conversational Interface using Python

Which “Nation” will create the first general purpose Conversational Interface?

Nation of Google? Nation of Bing? Nation of Ruby Developers? Nation of Java Developers?

Does it matter?

Our “Space Race”:

Page 11: AI and Python: Developing a Conversational Interface using Python

❝ I believe that this Python Developer nation should commit itself to achieving the goal, before this next decade is out, of creating a general purpose Conversational Interface. ❞

Read with Kennedy Accent:

Page 12: AI and Python: Developing a Conversational Interface using Python

Motivation – Why should you care?

Tools and Resources

Technical bits and Program Samples

Open Source Future Direction

Session Format

Page 13: AI and Python: Developing a Conversational Interface using Python

A Chat Bot that can respond “intelligently” to input.

Intelligence = Great input parsing Understanding of context Large knowledge base

What is a Conversational Interface?

Page 14: AI and Python: Developing a Conversational Interface using Python

What does a CI look like?

Page 15: AI and Python: Developing a Conversational Interface using Python

What does a CI look like?

2009 – today 2019 – in a decade

Text query / response Chat window Stationary virtual

character Response = f(single

input)

Internet-based

Voice and Video Conversation Facial expressions Context-driven

responses

Personal-device based

Page 16: AI and Python: Developing a Conversational Interface using Python

Started September 2008 Implemented in PayPal US, UK, France 650,000 conversations per month Saving $10M / year diverting call-center

calls1

Chatbots.org links to 375 such virtual people in the wild today

1 Assuming 20% of the conversations diverted calls averaging 12 minutes of time by $30/hour employees (fully loaded costs).

PayPal “Sarah”

Page 17: AI and Python: Developing a Conversational Interface using Python

Guide users through your site or application

Troubleshooting Wizards

Frequently Asked Questions (especially multi-part and contextual)

Think of your chat bot as a new Customer Service Rep who is in training

What Are they Good For?

Page 18: AI and Python: Developing a Conversational Interface using Python

Build a Conversational Interface to be added to municipal websites

• Handle FAQ’s• Guide users through the site

Introducing “Amy Iris”

Case Study

Page 19: AI and Python: Developing a Conversational Interface using Python

1. Language Parsing Tools – Parse it

2. Knowledge Base – Get Answer

3. Context Management – Store Context

4. User Experience and Server – Say it(Chat window and Character Graphic)

How to Create Your Own

Page 20: AI and Python: Developing a Conversational Interface using Python

Simple Example

Page 21: AI and Python: Developing a Conversational Interface using Python
Page 22: AI and Python: Developing a Conversational Interface using Python

Language Parsing Tools

Roll your own (i.e. “if pattern in textin:”)

Regular Expressions(re)

Artificial Intelligence Markup Language (AIML)

Natural Language Tool Kit (NLTK)

Page 23: AI and Python: Developing a Conversational Interface using Python

Simple version:if “spell kat” in textin.lower():

say(“Uh, C A T.”)

Advanced techniques: Normalize input

Remove punctuation, multiple spaces

Standardize case

Roll your own parsing

Page 24: AI and Python: Developing a Conversational Interface using Python

import re

# huge table of patterns:patterns =[ ('.*spell kat.*','UH, C A T.'), ] # later in program:for p in patterns: if re.match(p[0], textin): say (p[1]) # say() prints or

renders

Regular Expressions

Page 25: AI and Python: Developing a Conversational Interface using Python

❝ Some people,

when confronted with a problem,

think “I know, I’ll use regular expressions.”

Now they have two problems.❞

— Jamie Zawinski

Regular Expressions

Page 26: AI and Python: Developing a Conversational Interface using Python

XML with AI-specific tags Patterns and responses Serves its purpose well, for Chatterbot

definitions Not too difficult to learn Cumbersome freeform logic /

extensibility

AIML

Page 27: AI and Python: Developing a Conversational Interface using Python

A Chatterbot

Uses AIML

The basis for routinely winning the Chatterbot competitions

Open Source, freely licensable

A.L.I.C.E.

Page 28: AI and Python: Developing a Conversational Interface using Python

<category><pattern>

HOW DO YOU SPELL KAT</pattern><template>

Uh, C A T.</template>

</category>

AIML example

Page 29: AI and Python: Developing a Conversational Interface using Python

<category> <pattern> DO YOU KNOW HOW TO * </pattern> <template> <srai>HOW DO YOU

<star/></srai> </template></category>

AIML example

Page 30: AI and Python: Developing a Conversational Interface using Python

Download PyAIML Download ALICE definitions Edit AIML

Or use a service (some free, some paid)

Implementing AIML

Page 31: AI and Python: Developing a Conversational Interface using Python

Collection of tools Corpora Parsing, tokens,

chunking, parts of speech

Analysis: Collocation, semantics, metrics, probability, testing

Natural Language Tool Kit

Page 32: AI and Python: Developing a Conversational Interface using Python

From “Mat”, on stackoverflow.com

Page 33: AI and Python: Developing a Conversational Interface using Python

From “Mat”, on stackoverflow.com

Page 34: AI and Python: Developing a Conversational Interface using Python

From “Mat”, on stackoverflow.com

Page 35: AI and Python: Developing a Conversational Interface using Python

From “Mat”, on stackoverflow.com

Page 36: AI and Python: Developing a Conversational Interface using Python

From “Mat”, on stackoverflow.com

Page 37: AI and Python: Developing a Conversational Interface using Python

From “Mat”, on stackoverflow.com

Page 38: AI and Python: Developing a Conversational Interface using Python

From “Mat”, on stackoverflow.com

Page 39: AI and Python: Developing a Conversational Interface using Python

1. Language Parsing Tools – Parse it

2. Knowledge Base – Get Answer

3. Context Management – Store Context

4. User Experience and Server – Say it(Chat window and Character Graphic)

How to Create Your Own

Page 40: AI and Python: Developing a Conversational Interface using Python

Knowledge Base

List of answers AIML response SQL Database Lookup Website lookup

HTML parsing (Beautiful Soup) Semantic Web Application Programming Interfaces

(APIs) JSON or xml

Page 41: AI and Python: Developing a Conversational Interface using Python

1. Language Parsing Tools – Parse it

2. Knowledge Base – Get Answer

3. Context Management – Store Context

4. User Experience and Server – Say it(Chat window and Character Graphic)

How to Create Your Own

Page 42: AI and Python: Developing a Conversational Interface using Python

Context Management

AIML – limited context “that”, “it”, “topic”

NLTK has some basic tools for context management

Page 43: AI and Python: Developing a Conversational Interface using Python

User Experience

Today, chat window Graphic character Interface to mobile, twitter, social

networking Your server or Google App Engine Hosted solutions

Pandorabots, ysalaya.org, others

Tomorrow, voice recognition, additional interfaces

Page 44: AI and Python: Developing a Conversational Interface using Python

• Every “business oriented” bot creator needs a:• Parser• Knowledge Base• Context Manager• UI

Why not create an “Open Source” framework for best-in-class of each?

“Ah Ha!” Moment #1

Page 45: AI and Python: Developing a Conversational Interface using Python
Page 46: AI and Python: Developing a Conversational Interface using Python
Page 47: AI and Python: Developing a Conversational Interface using Python

Translator example

• Parse it• Get Answer• Update Context• Say it

Page 48: AI and Python: Developing a Conversational Interface using Python

Translator - Parse

match=re.match(r'(say|translate) (.*) (in|to|into) (\w+)',

textin)

Page 49: AI and Python: Developing a Conversational Interface using Python

Translator - Parse

match=re.match(r'(say|translate)

match.group(1) (.*) match.group(2) (in|to|into) match.group(3) (\w+)', match.group(4)

textin)

Page 50: AI and Python: Developing a Conversational Interface using Python

Translator – Get Answer

if match: c = urlfetch.fetch(

"http://ets.freetranslation.com/"+

"?language=english/"+ match.group(4)+"&srctext="+

match.group(2)).content c = c[c.index('--<br>')+7:] c = c[0:c.index('<p>')-6]

Page 51: AI and Python: Developing a Conversational Interface using Python

Translator – Say It

say(c + " is " + match.group(2) + " in “+match.group(4)+".")

Page 52: AI and Python: Developing a Conversational Interface using Python

Translator – Say It

say(c + "silla"" is " + is match.group(2) + "chair"" in “ in+match.group(4)+".")spanish.

Page 53: AI and Python: Developing a Conversational Interface using Python

Translate “I have an

emergency.” to Spanish

“I have an emergency.” in Spanish is “Tengo una

emergencia.”

UI

ParserKnowled

geBase

Context Manager

Page 54: AI and Python: Developing a Conversational Interface using Python

10 lines of Python code, plus a little open source…

…turned every phone into a English-Spanish translator

“Ah Ha!” Moment #2

10 lines of PythonTwitter

Python-Twitter

Translator Website

urllib2

Twitter API

Python

Page 55: AI and Python: Developing a Conversational Interface using Python

Why is this significant?

1. A Novice Python Programmer can turn every phone on the planet into a Universal Translator.

2. What happens if tens of thousands ofPython Programmers begin contributingto a Central Code repository? A General Purpose Conversational Interface emerges.

What Wikipedia did for Encyclopedias, Python Programmers can do for Artificial Intelligence.

COOL!

Translate “I have an

emergency.” to Spanish

“I have an emergency.” in Spanish is “Tengo una

emergencia.”

REAL COOL

!

Page 56: AI and Python: Developing a Conversational Interface using Python

What Wikipedia did for Encyclopedias,

Python Programmers can do for Artificial Intelligence.

Page 57: AI and Python: Developing a Conversational Interface using Python

Motivation – Why should you care?

Tools and Resources

Technical bits and Program Samples

Open Source Future Direction

Session Format

Page 58: AI and Python: Developing a Conversational Interface using Python

I’m building bots for municipalities Amy Iris is a byproduct

Django website Contributory Code Twitter Interface

Amy Iris is a contributory frameworksimilar to Wikipedia as a framework

Future Direction

Page 59: AI and Python: Developing a Conversational Interface using Python

Why was Wikipedia Successful?

Open Content LicenseFocus (on Encyclopedia)Openness (Anyone can contribute)Ease of EditingCollaborate Radically; don’t sign articlesOffer unedited, unapproved content for

further developmentNeutralityStart with a core of good peopleEnjoy the Google Effect

- Larry Sanger (early Wikipedia employee)

Page 60: AI and Python: Developing a Conversational Interface using Python
Page 61: AI and Python: Developing a Conversational Interface using Python
Page 62: AI and Python: Developing a Conversational Interface using Python

How to get involved:

• Follow @amyiris on Twitter

• Send email: [email protected]

• Online meetings begin next week.

Page 63: AI and Python: Developing a Conversational Interface using Python

• BACK UP SLIDES TO FOLLOW

Page 64: AI and Python: Developing a Conversational Interface using Python

API

Users can interact with Amy Iris through several interfaces:

- API- Website amyiris.com- Web Widget on your website- Twitter

Users “talk” to Amy Iris by typing something - A familiar chat-bot experience

Page 65: AI and Python: Developing a Conversational Interface using Python

User ContributedOpen Source

Code Snippets

API

ALICE

CodeCode

CodeCode

CodeCode

Amy Iris evaluates the user’s input, and sends it to ALICE, an award-winning Open Source chat-bot platform.

Amy Iris also launches some number of code snippets in a prioritized fashion, searching for a good response.

Page 66: AI and Python: Developing a Conversational Interface using Python

User ContributedOpen Source

Code Snippets

API

ALICE

TranslatorCode

CodeCode

CodeCode

Code

Translation

website

Code snippets can access the web to formulate their response

Snippets can scrape web page HTML to build an appropriate response

Amy Iris evaluates all responses that are provided, selects one “fastest response that’s good enough”, and presents it to the user

Page 67: AI and Python: Developing a Conversational Interface using Python

User ContributedOpen Source

Code Snippets

API

ALICE

Best Buy

snippet

CodeCode

CodeCode

CodeCode

Remix

Some forward-thinking companies already provide an API for access to their information

Snippets can call APIs to formulate a response

Remix is an API provided by Best Buy – an excellent example of a company who “gets it”

Smile if you’re getting it!

Page 68: AI and Python: Developing a Conversational Interface using Python

Amy Iris is:- A Conversational Interface that you can communicate with- Built with Open Source components, for a free experience- A repository system for user-contributed code

Users can- Communicate with Amy Iris through amyiris.com, an API, web

widgets, or Twitter

Developers can- Use the API to integrate Amy Iris into their applications- Use the Amy Iris widgets to put an Amy Iris interface onto their

web page- Build “applications” in Amy Iris, for Conversational Customer

Service- Make Amy Iris smarter by submitting Code Snippets into the

system

Page 69: AI and Python: Developing a Conversational Interface using Python

Translator example

index=str(textin).find("say")match=re.match(r'say (.*) in(\w+)',textin[index:])if match: c = urlfetch.fetch("http://ets.freetranslation.com/?

language=english/"+ match.group(4)+"&"+"mode=html&charset=UTF-

8&sequence=core&srctext="+ match.group(2).replace(" ","%20")).content c = c[c.index('--<br>')+7:] c = c[0:c.index('<p>')-6] say(unescape(c,{"&apos;": "'", "&quot;": '"'}) + " is " +

match.group(2) + " in "+match.group(4)+".") say(confidence=43)

Page 70: AI and Python: Developing a Conversational Interface using Python

User ContributedOpen Source

Code Snippets

API

ALICE

Best Buy

snippet

TranslatorCode

CodeCode

CodeCode

Code

Remix

Translation

website

Page 71: AI and Python: Developing a Conversational Interface using Python

We, the Python Developer Nation, can win this decade’s Space Race – Building a Conversational Interface to the Internet.

We have the tools: A language for the masses Natural Language Tool Kit (NLTK) Google App Engine

The Python Advantage

Page 72: AI and Python: Developing a Conversational Interface using Python

Invite your colleagues from the Nations of Java, Ruby C++, and .net to join our cause.

Our Dictator is Benevolent

The Nation of Python’s citizenship requirements are quick and painless…

Just leave your { braces } at the border.

We are an Open Nation

Page 73: AI and Python: Developing a Conversational Interface using Python

2005: AI can perform at human level intelligence on the SAT verbal analogy section. Link: http://www.kuro5hin.org/story/2005/5/26/192639/466

2009: Google adds contextual search Location Previous search terms

Our Sputnik?

Page 74: AI and Python: Developing a Conversational Interface using Python

Chat Bot approach (AIML, A.L.I.C.E. , PyAIML, Various Chatbots)

Natural Language Tool Kit (NLTK)

Search Engines growing up (Google, ASK, Bing “Decision Engine”, Yahoo, Cha Cha)

Semantic Web Efforts

Wordnet, AI efforts

Several Parallel Initiatives

Page 75: AI and Python: Developing a Conversational Interface using Python

Grammar Corpus / Corpora Parts of Speech Genre Parsing Inference Engine Domain Discourse

NLTK Concepts

Page 76: AI and Python: Developing a Conversational Interface using Python

<draw picture> User -> U.I. -> “Memoized” www pages // Parser -> WWW pages

Wrap pages with Semantic Web and API

Add in Context Show how it breaks down (search in

sentences)

Search Engine Analogy

Page 77: AI and Python: Developing a Conversational Interface using Python

Resources

Page 78: AI and Python: Developing a Conversational Interface using Python

Contest: Reward bots who fool human judges

Every entrant fooled at least one judge

Elbot fooled 25% of judges 30% is Alan Turing’s metric (“Turing

Test”) 12 small chat sessions are judged

October 2008 Loebner Prize

Page 79: AI and Python: Developing a Conversational Interface using Python

Judge: Good afternoon, how are you? Bot (?): good Bot (?): it's not afternoon yet Judge: I'm glad to hear that your well and your right, thanks for pointing that out Bot (?): no problem Bot (?): how are you? Judge: I'm well thanks, have you any plans for this afternoon? Bot (?): I'm not sure Bot (?): go home I guess Bot (?): and you? Judge: That sounds like a good initial plan but the weather should be good today and it would be sad to miss out Bot (?): that's true Bot (?): although the weather is not nice at the moment Bot (?): unless you like fog Judge: I have plans to iron and then maybe go out and do some fishing Bot (?): that's interesting - where do you go fishing? Judge: There is a local lake by the green park, it's quiet and relaxing Bot (?): can you eat the fish you catch? Judge: There are restrictions but one or two should be ok, I am keen to try perch

Bot or Not – Choice 1

Page 80: AI and Python: Developing a Conversational Interface using Python

Judge: Good afternoon, how are you? Bot (?): But I wouldn't say that any afternoon could be good. Judge: That's a bit pesamistic, I think any afternoon has the ability to be good Bot (?): Well, afternoon was your word. Judge: Are you able to see into the futureBot (?): How do you respond when people pose this question to you? Judge: I would hope that the answer was no, otherwise I would be interested to meet you Bot (?): Well, what will that solve? After all, the answer is only a matter of

perspective. Judge: That would depend on one's perspective Bot (?): Sorry for the tangent, but that reminds me of carefree robot summers spent depending on internal programming. Judge: Touchee Bot (?): With the inspirations you supply, there are no ends to my horizon... Judge: Have you any plans for later in the day Bot (?): This is confusing. Why bring the waking hours into this? Judge: Just curiosity and I hope that kill no cats Bot (?): That is truly one of the most inspirational qualities of humankind.

Even when things seems completely hopeless, you can still hope.

Bot or Not – Choice 2

Page 81: AI and Python: Developing a Conversational Interface using Python
Page 82: AI and Python: Developing a Conversational Interface using Python
Page 83: AI and Python: Developing a Conversational Interface using Python

$1,000 of computation = 1,000 times the human brain

Reverse engineering of the human brain completed

Computers pass the Turing test Nonbiological intelligence combines the subtlety

and pattern recognition strength of human intelligence, with the speed, memory, and knowledge sharing of machine intelligence

Nonbiological will continue to grow exponentially whereas biological intelligence is effectively fixed

2029: An intimate merger

- Ray Kurzweil

Page 84: AI and Python: Developing a Conversational Interface using Python
Page 85: AI and Python: Developing a Conversational Interface using Python
Page 86: AI and Python: Developing a Conversational Interface using Python
Page 87: AI and Python: Developing a Conversational Interface using Python
Page 88: AI and Python: Developing a Conversational Interface using Python
Page 89: AI and Python: Developing a Conversational Interface using Python
Page 90: AI and Python: Developing a Conversational Interface using Python

Amy Iris

Amy Iris is our Conversational Interface (CI). (think A.I. / chatterbots)

Responds to user input via web and phone. (think google/ cha cha / twitter)

Amy Iris is a framework for extending the CI in a collaborative manner. (think wikipedia)

If you consider Wikipedia as the greatest Library of knowledge every collaboratively created, my goal is for Amy Iris to emerge as the greatest Librarian of knowledge (and action).

Page 91: AI and Python: Developing a Conversational Interface using Python

Contributory Framework - Consider Wikipedia:

In 2008, a hugely valuable “End Product”; over 2 million articles

In 2001, started with 17-24 articles ;12%per month growth rate

The 2001 “genius” was not the 17-24 articles, it was the Contributory Framework

Page 92: AI and Python: Developing a Conversational Interface using Python

Current State of Conversational Interfaces Cha Cha (human powered)

Chatterbots Capable of carrying on an automated

conversation to some degree 1966, Eliza demonstrates technology, 100

lines of code 2006, A.L.I.C.E. advances technology, defines

standards, wins competitions. 20K patterns

Page 93: AI and Python: Developing a Conversational Interface using Python

“I believe that this nation should commit itself to achieving the goal, before this decade is out, of landing a man on the moon and returning him safely to the Earth.”

— President Kennedy, May 25, 1961

“We choose to go to the moon in this decade and do the other things, not because they are easy, but because they are hard.”

— President Kennedy 1962 at Rice University

Page 94: AI and Python: Developing a Conversational Interface using Python

Lofty Goals = Achievements:

Page 95: AI and Python: Developing a Conversational Interface using Python

1980’s Lofty Goals…

Page 96: AI and Python: Developing a Conversational Interface using Python

2000’s Lofty Goals… ?

Page 97: AI and Python: Developing a Conversational Interface using Python

Even Assassination attempts were grandiose:

Page 98: AI and Python: Developing a Conversational Interface using Python

vs. a shoe toss?