0

I am using Python to program a script for IBM Watson's Personality Insights service. I am using the results as training data for a Machine Learning project.

Since the service is so limited (100 calls/month), is it possible to get multiple personality insights with only one API call?

Phil D
  • 15
  • 3

2 Answers2

3

Jeff is correct about the API limit: You are not limited to 100 api calls/month; this is just the number of free calls you get per month.

However and replying your question: Yes, it is possible to compute multiple portraits. If you are using application/json as Content-Type, you will notice you are including a userid field for each content element. You can include content from different authors (userid's), just that you cannot get the output in as JSON since this one only supports a single author. You can use the CSV API and get multiple rows, one corresponding to each author in the input.

Here is sample code that may help:

import requests, json

data = { "contentItems" : [
  { 
    "userid" : "user1",
    "id" : "uuid1.1",
    "contenttype" : "text/plain",
    "language" : "en",
    "created" : 1393264847000,
    "content": "some text"
  },
  { 
    "userid" : "user1",
    "id" : "uuid1.2",
    "contenttype" : "text/plain",
    "language" : "en",
    "created" : 1393263869000,
    "content": "even more"
  },
  {
    "userid" : "user2",
    "id" : "uuid2",
    "contenttype" : "text/plain",
    "language" : "en",
    "created" : 1394826985000,
    "content": "this is a different author"
  }
] }

response = requests.post(
       "https://gateway.watsonplatform.net/personality-insights"+
       "/api/v2/profile", # Or append: "?headers=True",
   auth=("API_USERID", "API_PASSWORD"),
   headers={"Content-Type": "application/json", "Accept": "text/csv"},
   data = json.dumps(data)
)

print("HTTP %d:\n%s" % (response.status_code, response.content))

Two notes on this code:

  • running this exact code will get a HTTP 400, since it does not meet the minimum text requirements: you need to replace the content fields with your text -- more text!
  • multiple content items can belong to the same author - note that the first two above belong to user1 and the last one to user2
  • if you omit the Accept: "text/csv" header, it will default to the JSON API and return HTTP 400: "multiple authors found". Remember to use the CSV API for multiple authors.

This way you can batch some authors in a single API call. Keep in mind you need to stay under the request size limit (currently 20Mb) so you just need to be little more careful.

herchu
  • 936
  • 7
  • 24
  • 1
    This is exactly what I was looking for, thanks! I implemented the suggestion and it works as intended. Its too bad that you can only analyze multiple authors with the csv response option. Quick question, how do you get the headers with the csv? I tried putting 'headers': 'true' in the headers section, but this doesn't work. – Phil D Jun 15 '15 at 22:06
  • The `"?headers=true"` argument is a *query parameter*, so you need to add it to the URL, not the HTTP headers: something like `/api/v2/profile?headers=true` – herchu Jun 16 '15 at 13:29
  • Aaah, I see. Thanks so much! – Phil D Jun 16 '15 at 22:47
  • I think that this stopped being possible with V3: https://cloud.ibm.com/docs/services/personality-insights/release-notes.html#changes-to-json-objects-and-fields – xverges Dec 19 '18 at 16:40
1

You are not limited to 100 API calls a month, just over 100 you have to pay for the API calls.

Jeff Sloyer
  • 4,899
  • 1
  • 24
  • 48