pywit is the Python SDK for Wit.ai.
Using pip:
pip install witFrom source:
git clone https://github.com/wit-ai/pywit
python setup.py installSee the examples folder for examples.
The default API version is 20160516.
You can target a specific version by setting the env variable WIT_API_VERSION.
pywit provides a Wit class with the following methods:
message- the Wit message APIconverse- the low-level Wit converse APIrun_actions- a higher-level method to the Wit converse APIinteractive- starts an interactive conversation with your bot
The Wit constructor takes the following parameters:
access_token- the access token of your Wit instanceactions- (optional if you only usemessage()) the dictionary with your actions
actions has action names as keys and action implementations as values.
A minimal example looks like this:
from wit import Wit
def send(request, response):
print('Sending to user...', response['text'])
def my_action(request):
print('Received from user...', request['text'])
actions = {
'send': send,
'my_action': my_action,
}
client = Wit(access_token=access_token, actions=actions)The Wit message API.
Takes the following parameters:
msg- the text you want Wit.ai to extract the information fromverbose- (optional) if set, calls the API withverbose=true
Example:
resp = client.message('what is the weather in London?')
print('Yay, got Wit.ai response: ' + str(resp))A higher-level method to the Wit converse API.
run_actions resets the last turn on new messages and errors.
Takes the following parameters:
session_id- a unique identifier describing the user sessionmessage- the text received from the usercontext- the dict representing the session statemax_steps- (optional) the maximum number of actions to execute (defaults to 5)verbose- (optional) if set, calls the API withverbose=true
Example:
session_id = 'my-user-session-42'
context0 = {}
context1 = client.run_actions(session_id, 'what is the weather in London?', context0)
print('The session state is now: ' + str(context1))
context2 = client.run_actions(session_id, 'and in Brussels?', context1)
print('The session state is now: ' + str(context2))The low-level Wit converse API.
Takes the following parameters:
session_id- a unique identifier describing the user sessionmessage- the text received from the usercontext- the dict representing the session statereset- (optional) whether to reset the last turnverbose- (optional) if set, sets the API parameterverbosetotrue
Example:
resp = client.converse('my-user-session-42', 'what is the weather in London?', {})
print('Yay, got Wit.ai response: ' + str(resp))See the docs for more information.
Starts an interactive conversation with your bot.
Example:
client.interactive()See the docs for more information.
Default logging is to STDOUT with INFO level.
You can set your logging level as follows:
from wit import Wit
import logging
client = Wit(token, actions)
client.logger.setLevel(logging.WARNING)You can also specify a custom logger object in the Wit constructor:
from wit import Wit
client = Wit(access_token=access_token, actions=actions, logger=custom_logger)See the logging module and logging.config docs for more information.