Python client library to quickly get started with the various Watson APIs services.
Table of Contents
- Before you begin
- Installation
- Examples
- Discovery v2 only on CP4D
- Running in IBM Cloud
- Authentication
- Python version
- Changes for v1.0
- Changes for v2.0
- Changes for v3.0
- Changes for v4.0
- Migration
- Configuring the http client
- Disable SSL certificate verification
- Setting the service url
- Sending request headers
- Parsing HTTP response information
- Getting the transaction ID
- Using Websockets
- Cloud Pak for Data(CP4D)
- Logging
- Dependencies
- License
- Contributing
- Featured Projects
Watson API endpoint URLs at watsonplatform.net are changing and will not work after 26 May 2021. Update your calls to use the newer endpoint URLs. For more information, see https://cloud.ibm.com/docs/watson?topic=watson-endpoint-change.
IBM Watson™ Personality Insights is discontinued. For a period of one year from 1 December 2020, you will still be able to use Watson Personality Insights. However, as of 1 December 2021, the offering will no longer be available.
As an alternative, we encourage you to consider migrating to IBM Watson™ Natural Language Understanding, a service on IBM Cloud® that uses deep learning to extract data and insights from text such as keywords, categories, sentiment, emotion, and syntax to provide insights for your business or industry. For more information, see About Natural Language Understanding.
IBM Watson™ Visual Recognition is discontinued. Existing instances are supported until 1 December 2021, but as of 7 January 2021, you can't create instances. Any instance that is provisioned on 1 December 2021 will be deleted.
IBM Watson™ Compare and Comply is discontinued. Existing instances are supported until 30 November 2021, but as of 1 December 2020, you can't create instances. Any instance that exists on 30 November 2021 will be deleted. Consider migrating to Watson Discovery Premium on IBM Cloud for your Compare and Comply use cases. To start the migration process, visit https://ibm.biz/contact-wdc-premium.
- You need an IBM Cloud account. We now only support
python 3.5and above
To install, use pip or easy_install:
pip install --upgrade ibm-watsonor
easy_install --upgrade ibm-watsonNote the following: a) Versions prior to 3.0.0 can be installed using:
pip install --upgrade watson-developer-cloudb) If you run into permission issues try:
sudo -H pip install --ignore-installed six ibm-watsonFor more details see #225
c) In case you run into problems installing the SDK in DSX, try
!pip install --upgrade pip
Restarting the kernel
For more details see #405
The examples folder has basic and advanced examples. The examples within each service assume that you already have service credentials.
Discovery v2 is only available on Cloud Pak for Data.
If you run your app in IBM Cloud, the SDK gets credentials from the VCAP_SERVICES environment variable.
Watson services are migrating to token-based Identity and Access Management (IAM) authentication.
- With some service instances, you authenticate to the API by using IAM.
- In other instances, you authenticate by providing the username and password for the service instance.
To find out which authentication to use, view the service credentials. You find the service credentials for authentication the same way for all Watson services:
- Go to the IBM Cloud Dashboard page.
- Either click an existing Watson service instance in your resource list or click Create resource > AI and create a service instance.
- Click on the Manage item in the left nav bar of your service instance.
On this page, you should be able to see your credentials for accessing your service instance.
There are three ways to supply the credentials you found above to the SDK for authentication.
With a credential file, you just need to put the file in the right place and the SDK will do the work of parsing and authenticating. You can get this file by clicking the Download button for the credentials in the Manage tab of your service instance.
The file downloaded will be called ibm-credentials.env. This is the name the SDK will search for and must be preserved unless you want to configure the file path (more on that later). The SDK will look for your ibm-credentials.env file in the following places (in order):
- The top-level directory of the project you're using the SDK in
- Your system's home directory
As long as you set that up correctly, you don't have to worry about setting any authentication options in your code. So, for example, if you created and downloaded the credential file for your Discovery instance, you just need to do the following:
discovery = DiscoveryV1(version='2019-04-30')And that's it!
If you're using more than one service at a time in your code and get two different ibm-credentials.env files, just put the contents together in one ibm-credentials.env file and the SDK will handle assigning credentials to their appropriate services.
If you would like to configure the location/name of your credential file, you can set an environment variable called IBM_CREDENTIALS_FILE. This will take precedence over the locations specified above. Here's how you can do that:
export IBM_CREDENTIALS_FILE="<path>"where <path> is something like /home/user/Downloads/<file_name>.env.
Simply set the environment variables using _ syntax. For example, using your favourite terminal, you can set environment variables for Assistant service instance:
export ASSISTANT_APIKEY="<your apikey>"
export ASSISTANT_AUTH_TYPE="iam"