Skip to content

Commit

Permalink
fixing missing type + renaming weather to quickstart, adding template
Browse files Browse the repository at this point in the history
  • Loading branch information
patapizza committed Apr 13, 2016
1 parent 6727898 commit 86f4eed
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
43 changes: 43 additions & 0 deletions examples/quickstart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from wit import Wit

# Quickstart example
# See https://wit.ai/l5t/Quickstart

access_token = 'YOUR_ACCESS_TOKEN'

def first_entity_value(entities, entity):
if entity not in entities:
return None
val = entities[entity][0]['value']
if not val:
return None
return val['value'] if isinstance(val, dict) else val

def say(session_id, msg):
print(msg)

def merge(context, entities):
new_context = dict(context)
loc = first_entity_value(entities, 'location')
if loc:
new_context['loc'] = loc
return new_context

def error(session_id, msg):
print('Oops, I don\'t know what to do.')

def fetch_weather(context):
new_context = dict(context)
new_context['forecast'] = 'sunny'
return new_context

actions = {
'say': say,
'merge': merge,
'error': error,
'fetch-weather': fetch_weather,
}
client = Wit(access_token, actions)

session_id = 'my-user-id-42'
client.run_actions(session_id, 'weather in London', {})
10 changes: 1 addition & 9 deletions examples/weather.py → examples/template.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from wit import Wit

# Weather example
# See https://wit.ai/patapizza/example-weather

access_token = 'YOUR_ACCESS_TOKEN'

def say(session_id, msg):
Expand All @@ -14,17 +11,12 @@ def merge(context, entities):
def error(session_id, msg):
print('Oops, I don\'t know what to do.')

def fetch_forecast(context):
context['forecast'] = 'cloudy'
return context

actions = {
'say': say,
'merge': merge,
'error': error,
'fetch-forecast': fetch_forecast,
}
client = Wit(access_token, actions)

session_id = 'my-user-id-42'
client.run_actions(session_id, 'weather in London', {})
client.run_actions(session_id, 'your message', {})
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

setup(
name='wit',
version='3.0',
version='3.1',
description='Wit SDK for Python',
author='The Wit Team',
author_email='help@wit.ai',
Expand Down
2 changes: 1 addition & 1 deletion wit/wit.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def run_actions(
if max_steps <= 0:
raise WitError('max iterations reached')
rst = self.converse(session_id, message, context)
if not rst['type']:
if 'type' not in rst:
raise WitError('couldn\'t find type in Wit response')
if rst['type'] == 'stop':
return context
Expand Down

0 comments on commit 86f4eed

Please sign in to comment.