{"id":252,"date":"2022-01-11T16:46:12","date_gmt":"2022-01-11T16:46:12","guid":{"rendered":"https:\/\/blog.citrus-lime.com\/crmc\/?p=252"},"modified":"2022-01-11T16:46:13","modified_gmt":"2022-01-11T16:46:13","slug":"calling-a-power-platform-ai-builder-model-via-data","status":"publish","type":"post","link":"https:\/\/blog.citrus-lime.com\/crmc\/calling-a-power-platform-ai-builder-model-via-data\/","title":{"rendered":"Calling a Power Platform AI Builder Model via Data"},"content":{"rendered":"\n<p>We\u2019ve got our Model setup in the Power Platform AI Builder and we\u2019ve experimented with a Flow in Power Automate to query the Model and Predict an outcome.<\/p>\n\n\n\n<p>As a next step we might want to start querying our model outside of Power Automate to start running experiments or test scenarios past our model \u2013 particularly if we want to start visualising the model graphically or showing \u2018what-if\u2019 style predictions.<\/p>\n\n\n\n<p>We could run different Predict Actions in Power Automate in bulk to generate data from our model \u2013 but this would be quite wasteful.<\/p>\n\n\n\n<p>Instead we might want to invoke our Model in real-time to generate a Prediction Outcome.<\/p>\n\n\n\n<p>We can do this by invoking our Model via the Dynamics oData API.<\/p>\n\n\n\n<p>This can be done by calling the Predict Action against our Model, and supplying our input values to generate the prediction.<\/p>\n\n\n\n<p>The documentation from Microsoft for this call is fairly non-existent as yet so this took some trial and error to get right \u2013 but it can be done.<\/p>\n\n\n\n<p>Specifically by calling the following command as a REST Endpoint using a POST Message:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><a href=\"https:\/\/[mycrm].crm[region].dynamics.com\/api\/data\/v9.1\/msdyn_aimodels([GUID%20Id%20of%20my%20Model])\/Microsoft.Dynamics.CRM.Predict\"><strong>https:\/\/[mycrm].crm[region].dynamics.com\/api\/data\/v9.1\/msdyn_aimodels([GUID Id of my Model])\/Microsoft.Dynamics.CRM.Predict<\/strong><\/a><\/p><\/blockquote>\n\n\n\n<p>Supplying the details of our Prediction Input as the Body of the Message:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>{<br>&nbsp; &#8220;version&#8221;: &#8220;2.0&#8221;,<br>&nbsp; &#8220;requestv2&#8221;: {<br>&nbsp;&nbsp;&nbsp; &#8220;@odata.type&#8221;: &#8220;Microsoft.Dynamics.CRM.opportunity&#8221;,<br>&nbsp;&nbsp;&nbsp; &#8220;new_winnability&#8221;: 100<br>&nbsp; }<br>}<\/strong><\/p><\/blockquote>\n\n\n\n<p>This tests the model to return the Predicted Outcome when a single Input is supplied \u2013 in my case, just [Winnability] as 100.<\/p>\n\n\n\n<p>The Model will then return its prediction for that scenario:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>{<\/strong><\/p><p><strong>&#8220;responsev2&#8221;:{<\/strong><\/p><p><strong>&nbsp; &#8220;@odata.type&#8221;: &#8220;#Microsoft.Dynamics.CRM.expando&#8221;,<\/strong><\/p><p><strong>&nbsp; &#8220;operationStatus&#8221;: &#8220;Success&#8221;,<\/strong><\/p><p><strong>&nbsp; &#8220;predictionOutput&#8221;:{<\/strong><\/p><p><strong>&nbsp;&nbsp;&nbsp; &#8220;@odata.type&#8221;: &#8220;#Microsoft.Dynamics.CRM.expando&#8221;,<\/strong><\/p><p><strong>&nbsp;&nbsp;&nbsp; &#8220;Explanation&#8221;: &#8220;[{\\&#8221;entityName\\&#8221;: \\&#8221;new_projects\\&#8221;, \\&#8221;attributeName\\&#8221;: \\&#8221;new_projecttype\\&#8221;, \\&#8221;weight\\&#8221;: 0.13992829903618048}, {\\&#8221;entityName\\&#8221;: \\&#8221;opportunity\\&#8221;, \\&#8221;attributeName\\&#8221;: \\&#8221;new_customercontractstatus\\&#8221;, \\&#8221;weight\\&#8221;: 0.11845726947582477}, {\\&#8221;entityName\\&#8221;: \\&#8221;new_projects\\&#8221;, \\&#8221;attributeName\\&#8221;: \\&#8221;new_name\\&#8221;, \\&#8221;weight\\&#8221;: 0.05761718620521564}]&#8221;,<\/strong><\/p><p><strong>&nbsp;&nbsp;&nbsp; &#8220;Likelihood&#8221;: 0.6319,<\/strong><\/p><p><strong>&nbsp;&nbsp;&nbsp; &#8220;Prediction&#8221;: 2<\/strong><\/p><p><strong>&nbsp;&nbsp;&nbsp; }<\/strong><\/p><p><strong>&nbsp; }<\/strong><\/p><p><strong>}<\/strong><\/p><\/blockquote>\n\n\n\n<p>This then provides a way of querying our model using Custom Development or JSON Calls outside of Power Automate.<\/p>\n\n\n\n<p>By building a series of JSON Calls, we could use this approach to build real-time scenarios comparing different probabilities for scenarios in the Model.<\/p>\n\n\n\n<p>This can most obviously be seen using a JSON Client such as Talend to pass requests to our Model and evaluate the responses.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/license.citruslime.com\/cs\/blogs\/crmcs\/image_33F093E4.png\"><img decoding=\"async\" src=\"https:\/\/license.citruslime.com\/cs\/blogs\/crmcs\/image_thumb_4F951FDA.png\" alt=\"image\" title=\"image\" \/><\/a><\/figure>\n\n\n\n<p>At the time of writing, I found the Microsoft Documentation on this action to be \u2018evolving\u2019 as took pulling a few resources on oData to get the syntax of this call right \u2013 so hopefully this article helps point in the right direction!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Further Reading<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Predict Action<\/strong><\/p><p><a href=\"https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-engagement\/web-api\/predict?view=dynamics-ce-odata-9\"><strong>https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-engagement\/web-api\/predict?view=dynamics-ce-odata-9<\/strong><\/a><\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Predict Response<\/strong><\/p><p><a href=\"https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-engagement\/web-api\/predictresponse?view=dynamics-ce-odata-9\"><strong>https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-engagement\/web-api\/predictresponse?view=dynamics-ce-odata-9<\/strong><\/a><\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Getting Started with the Power Platform AI Builder<\/strong><\/p><p><a href=\"https:\/\/www.crmcs.co.uk\/content\/getting-started-with-the-power-platform-ai-builder.aspx\"><strong>https:\/\/www.crmcs.co.uk\/content\/getting-started-with-the-power-platform-ai-builder.aspx<\/strong><\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>We\u2019ve got our Model setup in the Power Platform AI Builder and we\u2019ve experimented with a Flow in Power Automate to query the Model and Predict an outcome. As a next step we might want to start querying our model outside of Power Automate to start running experiments or test scenarios past our model \u2013<\/p>\n","protected":false},"author":43,"featured_media":43,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_price":"","_stock":"","_tribe_ticket_header":"","_tribe_default_ticket_provider":"","_tribe_ticket_capacity":"0","_ticket_start_date":"","_ticket_end_date":"","_tribe_ticket_show_description":"","_tribe_ticket_show_not_going":false,"_tribe_ticket_use_global_stock":"","_tribe_ticket_global_stock_level":"","_global_stock_mode":"","_global_stock_cap":"","_tribe_rsvp_for_event":"","_tribe_ticket_going_count":"","_tribe_ticket_not_going_count":"","_tribe_tickets_list":"[]","_tribe_ticket_has_attendee_info_fields":false,"footnotes":""},"categories":[3],"tags":[],"class_list":{"0":"post-252","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-crm"},"featured_image_src":"https:\/\/blog.citrus-lime.com\/crmc\/wp-content\/uploads\/sites\/30\/2021\/11\/Untitled-design-41.png","author_info":{"display_name":"jadesmith","author_link":"https:\/\/blog.citrus-lime.com\/crmc\/author\/jadesmith\/"},"_links":{"self":[{"href":"https:\/\/blog.citrus-lime.com\/crmc\/wp-json\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.citrus-lime.com\/crmc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.citrus-lime.com\/crmc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.citrus-lime.com\/crmc\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.citrus-lime.com\/crmc\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":1,"href":"https:\/\/blog.citrus-lime.com\/crmc\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":253,"href":"https:\/\/blog.citrus-lime.com\/crmc\/wp-json\/wp\/v2\/posts\/252\/revisions\/253"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.citrus-lime.com\/crmc\/wp-json\/wp\/v2\/media\/43"}],"wp:attachment":[{"href":"https:\/\/blog.citrus-lime.com\/crmc\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.citrus-lime.com\/crmc\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.citrus-lime.com\/crmc\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}