Search code examples
pythonibm-cloudibm-watsonpersonality-insights

Can one get multiple results from one API call in IBM Watson?


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?


Solution

  • 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.