Skip to content

Commit

Permalink
v3.4.0 - unifying action parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
patapizza committed Apr 15, 2016
1 parent 2adbbb9 commit 88898bf
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 51 deletions.
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
## v3.4.0

Unifying action parameters

### breaking

- the `say` action now takes 3 parameters: `session_id`, `context`, `msg`
- the `error` action now takes 3 parameters: `session_id`, `context`, `e`

## v3.3.0

Updating action parameters

### breaking

- the `merge` action now takes 4 parameters: `session_id`, `context`, `entities`, `msg`
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ You need to provide at least an implementation for the special actions `say`, `m

A minimal `actions` dict looks like this:
```python
def say(session_id, msg):
def say(session_id, context, msg):
print(msg)

def merge(session_id, context, entities, msg):
return context

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

actions = {
'say': say,
Expand Down
33 changes: 13 additions & 20 deletions examples/joke.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

access_token = 'YOUR_ACCESS_TOKEN'


def first_entity_value(entities, entity):
if entity not in entities:
return None
Expand All @@ -29,36 +28,30 @@ def first_entity_value(entities, entity):
],
}


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


def merge(session_id, context, entities, msg):
new_context = dict(context)
if 'joke' in new_context:
del new_context['joke']
if 'joke' in context:
del context['joke']
category = first_entity_value(entities, 'category')
if category:
new_context['cat'] = category
context['cat'] = category
sentiment = first_entity_value(entities, 'sentiment')
if sentiment:
new_context['ack'] = 'Glad you liked it.' if sentiment == 'positive' else 'Hmm.'
elif 'ack' in new_context:
del new_context['ack']
return new_context


def error(session_id, context):
print('Oops, I don\'t know what to do.')
context['ack'] = 'Glad you liked it.' if sentiment == 'positive' else 'Hmm.'
elif 'ack' in context:
del context['ack']
return context

def error(session_id, context, e):
print(str(e))

def select_joke(session_id, context):
new_context = dict(context)
jokes = all_jokes[new_context['cat'] or 'default']
jokes = all_jokes[context['cat'] or 'default']
shuffle(jokes)
new_context['joke'] = jokes[0]
return new_context
context['joke'] = jokes[0]
return context

actions = {
'say': say,
Expand Down
19 changes: 6 additions & 13 deletions examples/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

access_token = 'YOUR_ACCESS_TOKEN'


def first_entity_value(entities, entity):
if entity not in entities:
return None
Expand All @@ -14,27 +13,21 @@ def first_entity_value(entities, entity):
return None
return val['value'] if isinstance(val, dict) else val


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


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


def error(session_id, context):
print('Oops, I don\'t know what to do.')
context['loc'] = loc
return context

def error(session_id, context, e):
print(str(e))

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

actions = {
'say': say,
Expand Down
9 changes: 3 additions & 6 deletions examples/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

access_token = 'YOUR_ACCESS_TOKEN'


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


def merge(session_id, context, entities, msg):
return context


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

actions = {
'say': say,
Expand Down
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.3.0',
version='3.4.0',
description='Wit SDK for Python',
author='The Wit Team',
author_email='[email protected]',
Expand Down
13 changes: 5 additions & 8 deletions wit/wit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
WIT_API_HOST = os.getenv('WIT_URL', 'https://api.wit.ai')
DEFAULT_MAX_STEPS = 5


class WitError(Exception):
pass


def req(access_token, meth, path, params, **kwargs):
rsp = requests.request(
meth,
Expand All @@ -28,7 +26,6 @@ def req(access_token, meth, path, params, **kwargs):
raise WitError('Wit responded with an error: ' + json['error'])
return json


def validate_actions(actions):
learn_more = 'Learn more at https://wit.ai/docs/quickstart'
if not isinstance(actions, dict):
Expand All @@ -43,7 +40,6 @@ def validate_actions(actions):
'\' action should be a function.')
return actions


class Wit:
access_token = None
actions = {}
Expand Down Expand Up @@ -77,12 +73,12 @@ def __run_actions(self, session_id, message, context, max_steps,
if 'say' not in self.actions:
raise WitError('unknown action: say')
print('Executing say with: {}'.format(rst['msg']))
self.actions['say'](session_id, rst['msg'])
self.actions['say'](session_id, dict(context), rst['msg'])
elif rst['type'] == 'merge':
if 'merge' not in self.actions:
raise WitError('unknown action: merge')
print('Executing merge')
context = self.actions['merge'](session_id, context,
context = self.actions['merge'](session_id, dict(context),
rst['entities'], user_message)
if context is None:
print('WARN missing context - did you forget to return it?')
Expand All @@ -91,15 +87,16 @@ def __run_actions(self, session_id, message, context, max_steps,
if rst['action'] not in self.actions:
raise WitError('unknown action: ' + rst['action'])
print('Executing action {}'.format(rst['action']))
context = self.actions[rst['action']](session_id, context)
context = self.actions[rst['action']](session_id, dict(context))
if context is None:
print('WARN missing context - did you forget to return it?')
context = {}
elif rst['type'] == 'error':
if 'error' not in self.actions:
raise WitError('unknown action: error')
print('Executing error')
self.actions['error'](session_id, context)
self.actions['error'](session_id, dict(context),
WitError('Oops, I don\'t know what to do.'))
else:
raise WitError('unknown type: ' + rst['type'])
return self.__run_actions(session_id, None, context, max_steps - 1,
Expand Down

0 comments on commit 88898bf

Please sign in to comment.