LUIS.AI Integration with C#.NET and MVC using .NET Framework

Introduction 

 
LUIS.AI will help build the Language Understanding (LU) Model as per your requirements. For example, if a user asks a question or conveys the conversation with a Bot or IoT device, the questions will be treated as an input. For LUIS.AI will be treated as Utterances (User Inputs). The LUIS.AI Model takes questions or input text and provides Intents, extracts the text from Input, generates Entities and produces the JSON data.
 
Key Points
  1. 1000 Endpoint Hits only with a free subscription.
  2. LUIS bot isn't working because when you use all  1000 free endpoint queries or exceed your pricing tier's monthly transactions quota, you receive an HTTP 403 error status code.
  3. LUIS bot isn't working or you get 403 and 429 error status codes when you exceed the transactions per second or transactions per month for your pricing tier.
  4. The update command is only to rename the application name and description of the LUIS application.
  5. App ID is mandatory to save in log information(table row).
  6. After the creation of an application, we need to make the endpoint public (This option will be available in Application Information).
  7. We can't see endpoint hits in the app's Dashboard(LUIS.AI portal) If you want to check endpoint hits we need to migrate with azure services.
  8. Only 500 applications per Azure authoring resource will be supported.
  9. We can give collaborators access to our teammates by using Azure Active Directory or Role-based access control(RBAC).
Go here and click on Sign-In.
 
 
Now give your Microsoft ID to log in.
 
 
Once you’re successfully logged in to your LUIS.AI, it will provide a note to migrate (link-up) the LUIS.AI cognitive services to your Azure resources. If you have your Azure subscription to link-up, you may proceed to click on the migrate button, or else you can click on the migrate later button.
 
 
Now the LUIS.AI home page will look like what is shown in the below figure.
 
 
Go to the bottom right, you’ll see Profile details, Go to Settings 
 
 
In this section, you’ll get information about the region, Primary key, Endpoint Url, Pricing Details and Linked Azure Resources Information.
 
 
Our project flow is shown in the below screenshot:
 
 
 
Now it’s time to create a new project for LUIS.AI cognitive service's API.
 
 
Select ASP.Net Web Application using .Net Framework and click on the Next button.
 
 
Give a Valid Project Name and Location Path to save source files and Click on Create button.
 
 
Select MVC and click on the Create button.
 
 
The default MVC template will be created as shown in the below figure:
 
 
Here, I’m adding a local SQL database for maintaining all log information about the created LUIS.AI applications. The complete script will be available in the project file or GIT resource.
 
 
Go to the below link to learn more about cognitive services from Microsoft.
 
 

LUIS Programmatic API’s 2.0

 
https://westus.dev.cognitive.microsoft.com/docs/services/5890b47c39e2bb17b84a55ff/operations/5890b47c39e2bb052c5b9c2f
 
Add one model for LUIS Endpoint location as shown in the below figure.
 
 
Global_Records class created log information for each and every transaction done by the user.
 
 
DBHelper.cs file created for database operations and code shown in the below figure.
 
 
EndPointLocationsController created endpoint log information without using luis.ai API and maintaining it in our database.
 
 
LUISAppMaster.cs class created to maintain the LUIS application entities model. To access this file, download the solution file and go-to models.
 
 
LUISAppMasterController.cs controller created for CRUD action methods for LUIS Applications.
 
 
Our design should be shown in the below figure:
 
 
For the above design, we require information about prebuilt LUIS.AI cultures, Token Version, Usage Scenario, Domain Name, EndPoint ID
 
Cultures: LUIS.AI API Link
 
 
Token Versions: LUIS AI API LINK
 
Based on the selected cultures, a tokenizer version number will be generated (In our developer language cascading dropdown list with selected culture).
 
 
Usage Scenario: LUIS.AI API References Link
 
 
Domain: LUIS AI API References Link
 
 
Database Script
  1. CREATE TABLE [dbo].[LuisEndPointLocations] (  
  2.     [LID]           INT            IDENTITY (1, 1) NOT NULL,  
  3.     [LEndPoint]     NVARCHAR (MAXNULL,  
  4.     [LEndPointUrl]  NVARCHAR (MAXNULL,  
  5.     [LEndPointCode] NVARCHAR (MAXNULL,  
  6.     [Record_Type]   CHAR (1)       NULL,  
  7.     [InsertedDate]  DATETIME       NULL,  
  8.     [DeletedDate]   DATETIME       NULL,  
  9.     [UpdatedDate]   DATETIME       NULL,  
  10.     [IPAddress]     NVARCHAR (MAXNULL,  
  11.     PRIMARY KEY CLUSTERED ([LID] ASC)  
  12. );  
  13. Go  
  14. CREATE TABLE [dbo].[LUISAppMaster] (  
  15.     [AID]               INT            IDENTITY (1, 1) NOT NULL,  
  16.     [AName]             NVARCHAR (MAXNULL,  
  17.     [ADescription]      NVARCHAR (MAXNULL,  
  18.     [ACulture]          NVARCHAR (MAXNULL,  
  19.     [AtokenizerVersion] NVARCHAR (MAXNULL,  
  20.     [AUsageScenario]    NVARCHAR (MAXNULL,  
  21.     [Adomain]           NVARCHAR (MAXNULL,  
  22.     [AinitialVersionId] NVARCHAR (MAXNULL,  
  23.     [AppID]             NVARCHAR (MAXNULL,  
  24.     [EndPointID]        INT            NULL,  
  25.     [Record_Type]       NVARCHAR (MAXNULL,  
  26.     [InsertedDate]      DATETIME       NULL,  
  27.     [DeletedDate]       DATETIME       NULL,  
  28.     [UpdatedDate]       DATETIME       NULL,  
  29.     [IPAddress]         NVARCHAR (MAXNULL,  
  30.     PRIMARY KEY CLUSTERED ([AID] ASC),  
  31.     FOREIGN KEY ([EndPointID]) REFERENCES [dbo].[LuisEndPointLocations] ([LID])  
  32. );  
  33. Go  
  34. /*          
  35. Exec LocationEndpoint_CRUD '{      
  36.   "LID": 1,      
  37.   "Flag""R",      
  38.   "Record_Type""N",      
  39.   "InsertedDate""12/2/2019 1:19:29 AM"      
  40. }'      
  41. Exec LocationEndpoint_CRUD '{  "Flag": "R",  "LEndPoint": "ManiTeja",  "LEndPointUrl": "Maniteja Vegi",  "LEndPointCode": "VMT", "LID":"2"}'          
  42. */          
  43. CREATE Proc LocationEndpoint_CRUD          
  44. (          
  45. @json nvarchar(max)          
  46. )          
  47. as           
  48. BEgin          
  49. Declare @Flag char(1);          
  50. Declare @LID int;          
  51. Set @Flag =  JSON_VALUE(@json,'$.Flag') ;          
  52. Set @LID =  JSON_VALUE(@json,'$.LID') ;          
  53. --Select @Flag           
  54.           
  55. IF @Flag = 'I'          
  56. BEGIN           
  57. BEGIN TRY                    
  58. BEGIN TRANSACTION                    
  59. INSERT INTO LuisEndPointLocations Select * from OPENJSON(@json) with (          
  60. LEndPoint nvarchar(max)            
  61. ,LEndPointUrl nvarchar(max)            
  62. ,LEndPointCode nvarchar(max)           
  63. ,Record_Type char(1)             
  64. ,InsertedDate datetime             
  65. ,DeletedDate datetime              
  66. ,UpdatedDate datetime             
  67. ,IPAddress nvarchar(max)            
  68. );          
  69. COMMIT TRANSACTION        
  70. Select '0' as ErrorCode,'Data Saved Successfully' as ErrorMessage FOR JSON  PATH          
  71. END TRY        
  72. BEGIN CATCH        
  73. ROLLBACK TRANSACTION        
  74. SELECT                      
  75.             ERROR_NUMBER() AS ErrorNumber                      
  76.             ,ERROR_SEVERITY() AS ErrorSeverity                      
  77.             ,ERROR_STATE() AS ErrorState                      
  78.             ,ERROR_PROCEDURE() AS ErrorProcedure                      
  79.             ,ERROR_LINE() AS ErrorLine                      
  80.             ,ERROR_MESSAGE() AS ErrorMessage FOR JSON PATH;                      
  81. END CATCH        
  82.         
  83. END          
  84. IF @Flag = 'U'          
  85. BEGIN          
  86. BEGIN TRY                    
  87. BEGIN TRANSACTION                    
  88. update LuisEndPointLocations          
  89. Set           
  90. LEndPoint   = JSON_VALUE(@json,'$.LEndPoint')           
  91. ,LEndPointUrl   = JSON_VALUE(@json,'$.LEndPointUrl')           
  92. ,LEndPointCode  = JSON_VALUE(@json,'$.LEndPointCode')           
  93. ,Record_Type   = JSON_VALUE(@json,'$.Record_Type')           
  94. ,UpdatedDate   = JSON_VALUE(@json,'$.UpdatedDate')           
  95. ,IPAddress    = JSON_VALUE(@json,'$.IPAddress')           
  96. where LID =  JSON_VALUE(@json,'$.LID') ;          
  97. COMMIT TRANSACTION        
  98. Select '0' as ErrorNumber,'Data Updated Successfully' as ErrorMessage FOR JSON  PATH          
  99. END TRY        
  100. BEGIN CATCH        
  101. ROLLBACK TRANSACTION        
  102. SELECT                      
  103.             ERROR_NUMBER() AS ErrorNumber                      
  104.             ,ERROR_SEVERITY() AS ErrorSeverity                      
  105.             ,ERROR_STATE() AS ErrorState                      
  106.             ,ERROR_PROCEDURE() AS ErrorProcedure                      
  107.             ,ERROR_LINE() AS ErrorLine                      
  108.             ,ERROR_MESSAGE() AS ErrorMessage FOR JSON PATH;                      
  109.         
  110. END CATCH        
  111. END          
  112. IF @Flag = 'D'          
  113. BEGIN          
  114. BEGIN TRY                    
  115. BEGIN TRANSACTION                    
  116. Update LuisEndPointLocations          
  117. set Record_Type   = JSON_VALUE(@json,'$.Record_Type') ,          
  118. DeletedDate   = JSON_VALUE(@json,'$.DeletedDate')           
  119. where LID =  JSON_VALUE(@json,'$.LID') ;          
  120. COMMIT TRANSACTION        
  121. Select '0' as ErrorNumber,'Data Deleted Successfully' as ErrorMessage FOR JSON  PATH          
  122. END TRY        
  123. BEGIN CATCH        
  124. ROLLBACK TRANSACTION        
  125. END CATCH        
  126. END          
  127. IF @Flag = 'R'          
  128. BEGIN          
  129. IF @LID = 0            
  130. BEGIN        
  131. Select * from LuisEndPointLocations where Record_Type = 'L' for JSON PATH          
  132. END        
  133. ELSE        
  134. BEGIN        
  135. Select * from LuisEndPointLocations where LID =  JSON_VALUE(@json,'$.LID')  for JSON PATH                        
  136. END        
  137. END          
  138. end     
  139.   
  140. Go        
  141. /*              
  142. Exec LUISAppMaster_CRUD '{      
  143.   "AName""Test",      
  144.   "ADescription""test",      
  145.   "ACulture""en-us",      
  146.   "AtokenizerVersion""1.0.0",      
  147.   "AUsageScenario""IoT",      
  148.   "Adomain""Booking & Reference",      
  149.   "AinitialVersionId""1",      
  150.   "EndPointID": 1,      
  151.   "AppID""XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",      
  152.   "Flag""I",      
  153.   "Record_Type""L",      
  154.   "InsertedDate""12/1/2019 5:35:20 PM"      
  155. }'                  
  156. */                  
  157. CREATE Proc LUISAppMaster_CRUD                  
  158. (                  
  159. @json nvarchar(max)                  
  160. )                  
  161. as                   
  162. BEgin                  
  163. Declare @Flag char(1);                  
  164. Declare @AID int;            
  165. Set @Flag =  JSON_VALUE(@json,'$.Flag') ;                  
  166. Set @AID =   JSON_VALUE(@json,'$.AID') ;                  
  167. --Select @Flag                   
  168.                   
  169. IF @Flag = 'I'                  
  170. BEGIN                  
  171. BEGIN TRY              
  172. BEGIN TRANSACTION              
  173. INSERT INTO LUISAppMaster Select * from OPENJSON(@json) with (                 
  174. AName nvarchar(max)                    
  175. ,ADescription nvarchar(max)                    
  176. ,ACulture nvarchar(max)                    
  177. ,AtokenizerVersion nvarchar(max)                    
  178. ,AUsageScenario nvarchar(max)                    
  179. ,Adomain nvarchar(max)                    
  180. ,AinitialVersionId nvarchar(max)                    
  181. ,AppID nvarchar(max)                
  182. ,EndPointID int      
  183. ,Record_Type nvarchar(max)                    
  184. ,InsertedDate datetime                
  185. ,DeletedDate datetime                
  186. ,UpdatedDate datetime                
  187. ,IPAddress nvarchar(max)          
  188. );              
  189. Select '0' as ErrorNumber,'Data Saved Successfully' as ErrorMessage FOR JSON  PATH                      
  190. COMMIT TRANSACTION              
  191. END TRY              
  192. BEGIN CATCH              
  193. ROLLBACK TRANSACTION              
  194.  SELECT                
  195.             ERROR_NUMBER() AS ErrorNumber                
  196.             ,ERROR_SEVERITY() AS ErrorSeverity                
  197.             ,ERROR_STATE() AS ErrorState                
  198.             ,ERROR_PROCEDURE() AS ErrorProcedure                
  199.             ,ERROR_LINE() AS ErrorLine                
  200.             ,ERROR_MESSAGE() AS ErrorMessage FOR JSON PATH;                
  201. END CATCH               
  202. END                  
  203. IF @Flag = 'U'                  
  204. BEGIN                  
  205. BEGIN TRY              
  206. BEGIN TRANSACTION              
  207. update LUISAppMaster                 
  208. Set                   
  209. AName    =   JSON_VALUE(@json,'$.AName')                 
  210. ,ADescription  =   JSON_VALUE(@json,'$.ADescription')                
  211. ,Record_Type   =   JSON_VALUE(@json,'$.Record_Type')                
  212. ,UpdatedDate   =   JSON_VALUE(@json,'$.UpdatedDate')                
  213. ,IPAddress   =   JSON_VALUE(@json,'$.IPAddress')                
  214. where AID =  JSON_VALUE(@json,'$.AID') ;                  
  215. Select '0' as ErrorNumber,'Data Updated Successfully' as ErrorMessage FOR JSON  PATH                  
  216. COMMIT TRANSACTION              
  217. END TRY              
  218. BEGIN CATCH              
  219. ROLLBACK TRANSACTION              
  220. SELECT                
  221.             ERROR_NUMBER() AS ErrorNumber                
  222.             ,ERROR_SEVERITY() AS ErrorSeverity                
  223.             ,ERROR_STATE() AS ErrorState                
  224.             ,ERROR_PROCEDURE() AS ErrorProcedure                
  225.             ,ERROR_LINE() AS ErrorLine                
  226.             ,ERROR_MESSAGE() AS ErrorMessage FOR JSON PATH;                
  227. END CATCH              
  228. END                  
  229. IF @Flag = 'D'                  
  230. BEGIN                  
  231. BEGIN TRY              
  232. BEGIN TRANSACTION              
  233. Update LUISAppMaster                  
  234. set Record_Type   = JSON_VALUE(@json,'$.Record_Type') ,                  
  235. DeletedDate   = JSON_VALUE(@json,'$.DeletedDate')                   
  236. where AID =  JSON_VALUE(@json,'$.AID') ;         
  237. Select '0' as ErrorNumber,'Data Deleted Successfully' as ErrorMessage FOR JSON  PATH                  
  238. COMMIT TRANSACTION              
  239. END TRY              
  240. BEGIN CATCH              
  241. ROLLBACK TRANSACTION              
  242. SELECT                
  243.            ERROR_NUMBER() AS ErrorNumber                
  244.             ,ERROR_SEVERITY() AS ErrorSeverity                
  245.  ,ERROR_STATE() AS ErrorState                
  246.             ,ERROR_PROCEDURE() AS ErrorProcedure                
  247.             ,ERROR_LINE() AS ErrorLine                
  248.             ,ERROR_MESSAGE() AS ErrorMessage FOR JSON PATH;                
  249. END CATCH              
  250. END                  
  251. IF @Flag = 'R'                  
  252. BEGIN               
  253. IF @AID != 0            
  254. BEGIN            
  255. Select * from LUISAppMaster where AID =  JSON_VALUE(@json,'$.AID')  for JSON PATH                  
  256. END            
  257. ELSE          
  258. BEGIN            
  259. Select * from LUISAppMaster where AID = 'L' for JSON PATH                  
  260. END          
  261. END                  
  262. end  
Our LUIS.AI project is done. If you want to check the output, you need to download the source code and replace the Primary key (Authoring Key) from th LUIS.AI portal, run DB Script into your database, and run the application. 
 
Happy Coding... 

Similar Articles