{"id":557,"date":"2018-12-09T10:56:49","date_gmt":"2018-12-09T09:56:49","guid":{"rendered":"https:\/\/letoon-lg.com\/?p=557"},"modified":"2018-12-17T20:22:41","modified_gmt":"2018-12-17T19:22:41","slug":"the-first-python-call-to-watson-assistant-api","status":"publish","type":"post","link":"https:\/\/letoon-lg.com\/index.php\/2018\/12\/09\/the-first-python-call-to-watson-assistant-api\/","title":{"rendered":"Our Watson Assistant API call, in Python"},"content":{"rendered":"<p>Hi,<\/p>\n<p>Today, I propose you a quick 10 min tutorial, which will allow you to implement a simple Python script to execute an API call to Watson Assistant. Then, let&#8217;s start for a 10 min tutorial.<\/p>\n<p><!--more--><\/p>\n<h1>Create a Watson Assistant instance<\/h1>\n<p>Based on our <a href=\"https:\/\/letoon-lg.com\/index.php\/2018\/11\/29\/a-1st-watson-ass\u2026chatbot-in-15min\/\">AI corpus baseline<\/a>, we will start by creating a 1st instance of a Watson Assistant.<\/p>\n<p>Let&#8217;s come back to our Bluemix console:&nbsp;<a href=\"https:\/\/console.bluemix.net\/dashboard\/apps\">https:\/\/console.bluemix.net\/dashboard\/apps<\/a>&nbsp;and in the services list, select the previously created.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-569\" src=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-01-1024x302.png\" alt=\"\" width=\"660\" height=\"195\" srcset=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-01-1024x302.png 1024w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-01-300x89.png 300w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-01-768x227.png 768w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-01.png 1420w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<p>Click on&nbsp;<em><strong>Launch Tool<\/strong><\/em>, then on&nbsp;<em><strong>Assistants<\/strong> and finally on&nbsp;<strong>Create new<\/strong><\/em>. Enter a name and a description, and finally click on&nbsp;<em><strong>Create<\/strong><\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-568\" src=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-02-1024x337.png\" alt=\"\" width=\"660\" height=\"217\" srcset=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-02-1024x337.png 1024w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-02-300x99.png 300w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-02-768x253.png 768w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-02.png 1329w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-567\" src=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-03-1024x585.png\" alt=\"\" width=\"660\" height=\"377\" srcset=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-03-1024x585.png 1024w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-03-300x171.png 300w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-03-768x439.png 768w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-03.png 1274w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<p>Now, we will add the dialog skills we previously created. Click on&nbsp;<em><strong>Add Dialog Skill<\/strong><\/em>, then select&nbsp;<em><strong>Add existing skill<\/strong><\/em> and click on the skill&nbsp;<em><strong>Kodi Baseline<\/strong><\/em>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-573\" src=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-04-1024x488.png\" alt=\"\" width=\"660\" height=\"315\" srcset=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-04-1024x488.png 1024w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-04-300x143.png 300w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-04-768x366.png 768w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-04.png 1281w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-572\" src=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-05-1024x532.png\" alt=\"\" width=\"660\" height=\"343\" srcset=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-05-1024x532.png 1024w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-05-300x156.png 300w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-05-768x399.png 768w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-05.png 1250w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<p>The assistant instance is create. Now, select on&nbsp;<em><strong>View API Details<\/strong><\/em> and store somewhere the <em><strong>Assistant ID<\/strong><\/em>, the <em><strong>Assistant URL<\/strong><\/em> and the <em><strong>Password<\/strong> <\/em>of the Service Credentials. We will need to use them in our Python Script.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-571\" src=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-06-1024x490.png\" alt=\"\" width=\"660\" height=\"316\" srcset=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-06-1024x490.png 1024w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-06-300x143.png 300w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-06-768x367.png 768w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-06.png 1798w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-570\" src=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-07-1024x369.png\" alt=\"\" width=\"660\" height=\"238\" srcset=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-07-1024x369.png 1024w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-07-300x108.png 300w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-07-768x277.png 768w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-07.png 1564w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<h1>Watson Assistant API call in Python<\/h1>\n<p>And now, let&#8217;s come back to Eclipse, create a PyDev project, add a Python file named&nbsp;<em><strong>assistant.py<\/strong><\/em> and enter the following code, by replacing&nbsp;<em><strong>YOUR_API_KEY<\/strong><\/em> and&nbsp;<em><strong>YOUR_ASSISTANT_ID<\/strong><\/em> by yours, previously seen on your Bluemix console.<\/p>\n<p><em><span style=\"color: #0000ff;\">from __future__ import print_function<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">import json<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">from watson_developer_cloud import AssistantV2<\/span><\/em><\/p>\n<p><em><span style=\"color: #0000ff;\">#########################<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\"># Credentials<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">#########################<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">assistant = AssistantV2(<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">version=&#8217;2018-09-20&#8242;,<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">url=&#8217;YOUR_URL&#8217;, # e.g. https:\/\/gateway-fra.watsonplatform.net\/assistant\/api<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">iam_apikey=&#8217;YOUR_API_KEY&#8217;)<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">assistant_id=&#8217;YOUR_ASSISTANT_ID&#8217;<\/span><\/em><\/p>\n<p><em><span style=\"color: #0000ff;\">#########################<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\"># Sessions<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">#########################<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">session = assistant.create_session(assistant_id).get_result()<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">session_id=session[&#8220;session_id&#8221;]<\/span><\/em><\/p>\n<p><em><span style=\"color: #0000ff;\">#########################<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\"># Message<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">#########################<\/span><\/em><\/p>\n<p><em><span style=\"color: #0000ff;\">message = assistant.message(<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">assistant_id,<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">session_id,<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">input={&#8216;text&#8217;: &#8216;Kodi?&#8217;},<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">).get_result()<\/span><\/em><br \/>\n<em><span style=\"color: #0000ff;\">print(json.dumps(message, indent=2))<\/span><\/em><\/p>\n<p><em><span style=\"color: #0000ff;\">assistant.delete_session(assistant_id, session_id).get_result()<\/span><\/em><\/p>\n<p>Execute the code by clicking on Run As \/ Python Run in the contextual menu.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-575\" src=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-08.png\" alt=\"\" width=\"852\" height=\"745\" srcset=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-08.png 852w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-08-300x262.png 300w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-08-768x672.png 768w\" sizes=\"auto, (max-width: 852px) 100vw, 852px\" \/><\/p>\n<p>And look at the console result. Normally, you should see the API call result. It works!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-576\" src=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-09-1024x439.png\" alt=\"\" width=\"660\" height=\"283\" srcset=\"https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-09-1024x439.png 1024w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-09-300x129.png 300w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-09-768x329.png 768w, https:\/\/letoon-lg.com\/wp-content\/uploads\/2018\/12\/WAPython01-09.png 1129w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<p>The next stage will be to create a skeleton for our Kodi add-on, and try integrate in it our 1st Python script with the Watson API call&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, Today, I propose you a quick 10 min tutorial, which will allow you to implement a simple Python script to execute an API call to Watson Assistant. Then, let&#8217;s&#8230; <a class=\"read-more-link\" href=\"https:\/\/letoon-lg.com\/index.php\/2018\/12\/09\/the-first-python-call-to-watson-assistant-api\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[16,37],"tags":[26,25,39],"class_list":["post-557","post","type-post","status-publish","format-standard","hentry","category-artificial-intelligence","category-programming","tag-api","tag-python","tag-watson-assistant"],"_links":{"self":[{"href":"https:\/\/letoon-lg.com\/index.php\/wp-json\/wp\/v2\/posts\/557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/letoon-lg.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/letoon-lg.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/letoon-lg.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/letoon-lg.com\/index.php\/wp-json\/wp\/v2\/comments?post=557"}],"version-history":[{"count":12,"href":"https:\/\/letoon-lg.com\/index.php\/wp-json\/wp\/v2\/posts\/557\/revisions"}],"predecessor-version":[{"id":583,"href":"https:\/\/letoon-lg.com\/index.php\/wp-json\/wp\/v2\/posts\/557\/revisions\/583"}],"wp:attachment":[{"href":"https:\/\/letoon-lg.com\/index.php\/wp-json\/wp\/v2\/media?parent=557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/letoon-lg.com\/index.php\/wp-json\/wp\/v2\/categories?post=557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/letoon-lg.com\/index.php\/wp-json\/wp\/v2\/tags?post=557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}