Skip to content

DOTOCA/pywit

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pywit

pywit is the Python SDK for Wit.ai.

Install

Using pip:

pip install wit

From source:

git clone https://github.com/wit-ai/pywit
pip install .

Usage

See the examples folder for examples.

API

Versioning

The default API version is 20160516. You can target a specific version by setting the env variable WIT_API_VERSION.

Overview

pywit provides a Wit class with the following methods:

  • message - the Wit message API
  • speech - the Wit speech API
  • interactive - starts an interactive conversation with your bot

Wit class

The Wit constructor takes the following parameters:

  • access_token - the access token of your Wit instance

A minimal example looks like this:

from wit import Wit

client = Wit(access_token)
client.message('set an alarm tomorrow at 7am')

.message()

The Wit message API.

Takes the following parameters:

  • msg - the text you want Wit.ai to extract the information from
  • verbose - (optional) if set, calls the API with verbose=true

Example:

resp = client.message('what is the weather in London?')
print('Yay, got Wit.ai response: ' + str(resp))

.speech()

The Wit speech API.

Takes the following parameters:

  • audio_file - a file handler opened in binary mode
  • verbose - (optional) if set, calls the API with verbose=true
  • headers - (optional) the dict of headers (e.g. "Content-Type")

Example:

resp = None
with open('test.wav', 'rb') as f:
  resp = client.speech(f, None, {'Content-Type': 'audio/wav'})
print('Yay, got Wit.ai response: ' + str(resp))

.interactive()

Starts an interactive conversation with your bot.

Example:

client.interactive()

See the docs for more information.

.run_actions()

DEPRECATED See our blog post for a migration plan.

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 session
  • message - the text received from the user
  • context - the dict representing the session state
  • max_steps - (optional) the maximum number of actions to execute (defaults to 5)
  • verbose - (optional) if set, calls the API with verbose=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))

.converse()

DEPRECATED See our blog post for a migration plan.

The low-level Wit converse API.

Takes the following parameters:

  • session_id - a unique identifier describing the user session
  • message - the text received from the user
  • context - the dict representing the session state
  • reset - (optional) whether to reset the last turn
  • verbose - (optional) if set, sets the API parameter verbose to true

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.

Logging

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)
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, logger=custom_logger)

See the logging module and logging.config docs for more information.

About

Python library for Wit.ai

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%