-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
205 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,29 @@ | ||
# pywit | ||
|
||
`pywit` is a Python client to easily use the [Wit.ai](http://wit.ai) HTTP API. | ||
`pywit` is the Python SDK for [Wit.ai](http://wit.ai). | ||
|
||
## Install | ||
|
||
Using `pip`: | ||
```bash | ||
pip install wit | ||
``` | ||
|
||
## Usage | ||
|
||
```python | ||
import wit | ||
print(wit.message('MY_ACCESS_TOKEN', 'turn on the lights in the kitchen')) | ||
``` | ||
|
||
See below for more examples. | ||
|
||
## Install from source | ||
|
||
From source: | ||
```bash | ||
git clone https://github.com/wit-ai/pywit | ||
python setup.py install | ||
``` | ||
|
||
## API | ||
## Usage | ||
|
||
```python | ||
import wit | ||
See the `examples` folder for examples. | ||
|
||
if __name__ == '__main__': | ||
access_token = 'MY_ACCESS_TOKEN' | ||
## API | ||
|
||
# GET /message to extract intent and entities from user request | ||
response = wit.message('turn on the lights in the kitchen', access_token) | ||
print('/message -> {}'.format(response)) | ||
``` | ||
`pywit` provides a Wit class with the following methods: | ||
* `message` - the Wit message API | ||
* `converse` - the low-level Wit converse API | ||
* `run_actions` - a higher-level method to the Wit converse API | ||
|
||
See the [docs](https://wit.ai/docs) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from random import shuffle | ||
from wit import Wit | ||
|
||
# Joke example | ||
# See https://wit.ai/patapizza/example-joke | ||
|
||
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 | ||
|
||
all_jokes = { | ||
'chuck': [ | ||
'Chuck Norris counted to infinity - twice.', | ||
'Death once had a near-Chuck Norris experience.', | ||
], | ||
'tech': [ | ||
'Did you hear about the two antennas that got married? The ceremony was long and boring, but the reception was great!', | ||
'Why do geeks mistake Halloween and Christmas? Because Oct 31 === Dec 25.', | ||
], | ||
'default': [ | ||
'Why was the Math book sad? Because it had so many problems.' | ||
], | ||
} | ||
|
||
def say(session_id, msg): | ||
print(msg) | ||
|
||
def merge(context, entities): | ||
new_context = dict(context) | ||
if 'joke' in new_context: | ||
del new_context['joke'] | ||
category = first_entity_value(entities, 'category') | ||
if category: | ||
new_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, msg): | ||
print('Oops, I don\'t know what to do.') | ||
|
||
def select_joke(context): | ||
new_context = dict(context) | ||
jokes = all_jokes[new_context['cat'] or 'default'] | ||
shuffle(jokes) | ||
new_context['joke'] = jokes[0] | ||
return new_context | ||
|
||
actions = { | ||
'say': say, | ||
'merge': merge, | ||
'error': error, | ||
'select-joke': select_joke, | ||
} | ||
client = Wit(access_token, actions) | ||
|
||
session_id = 'my-user-id-42' | ||
client.run_actions(session_id, 'tell me a joke about tech', {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from wit import Wit | ||
|
||
# Weather example | ||
# See https://wit.ai/patapizza/example-weather | ||
|
||
access_token = 'YOUR_ACCESS_TOKEN' | ||
|
||
def say(session_id, msg): | ||
print(msg) | ||
|
||
def merge(context, entities): | ||
return context | ||
|
||
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', {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ | |
|
||
setup( | ||
name='wit', | ||
version='2.0', | ||
version='3.0', | ||
description='Wit SDK for Python', | ||
author='The Wit Team', | ||
author_email='[email protected]', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
from wit import ( | ||
message | ||
) | ||
from wit import Wit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters