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.

Login

Now give your Microsoft ID to log in.

Type your email

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.

Migrate later

Now the LUIS.AI home page will look like what is shown in the below figure.

Go to my apps

Go to the bottom right, you’ll see Profile details, Go to Settings

Settings

In this section, you’ll get information about the region, Primary key, Endpoint Url, Pricing Details and Linked Azure Resources Information.

Loading authoring resources

Our project flow is shown in the below screenshot.

Json

Visual paradigm online diagrams

Now it’s time to create a new project for LUIS.AI cognitive service's API.

Create a new project

Select ASP.Net Web Application using .Net Framework and click on theNext button.

ASP dot NET application

Give a Valid Project Name and Location Path to save source files and Click on the Create button.

Click on create

Select MVC and click on the Create button.

MVC

The default MVC template will be created as shown in the below figure.

Overview

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.

Visual c sharp

Go to the below link to learn more about cognitive services from Microsoft.

LUIS programmatic API

LUIS Programmatic API’s 2.0

https://westus.dev.cognitive.microsoft.com/docs/services/5890b47c39e2bb17b84a55ff/operations/5890b47c39e2bb052c5b9c2f

Add one model for the LUIS Endpoint location as shown below.

LUIS endpoint location

Global_Records class created log information for every transaction done by the user.

Transaction done by the user

The DBHelper.cs file was created for database operations the code is shown in the figure below.

DB helper

EndPointLocationsController created endpoint log information without using luis.ai API and maintaining it in our database.

End point locations

LUISAppMaster.cs class was created to maintain the LUIS application entities model. To access this file, download the solution file and go-to models.

LUIS app master

LUISAppMasterController.cs controller created for CRUD action methods for LUIS Applications.

Luis application

Our design is shown in the figure below.

Create

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
    Culture luis
  • 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).Luis app master controller
  • Usage Scenario: LUIS.AI API References Link
    Usage scenario
  • Domain: LUIS AI API References Link
    Domain luis

Database Script

CREATE TABLE [dbo].[LuisEndPointLocations] (  
    [LID]           INT            IDENTITY (1, 1) NOT NULL,  
    [LEndPoint]     NVARCHAR (MAX) NULL,  
    [LEndPointUrl]  NVARCHAR (MAX) NULL,  
    [LEndPointCode] NVARCHAR (MAX) NULL,  
    [Record_Type]   CHAR (1)       NULL,  
    [InsertedDate]  DATETIME       NULL,  
    [DeletedDate]   DATETIME       NULL,  
    [UpdatedDate]   DATETIME       NULL,  
    [IPAddress]     NVARCHAR (MAX) NULL,  
    PRIMARY KEY CLUSTERED ([LID] ASC)  
);  
Go  
CREATE TABLE [dbo].[LUISAppMaster] (  
    [AID]               INT            IDENTITY (1, 1) NOT NULL,  
    [AName]             NVARCHAR (MAX) NULL,  
    [ADescription]      NVARCHAR (MAX) NULL,  
    [ACulture]          NVARCHAR (MAX) NULL,  
    [AtokenizerVersion] NVARCHAR (MAX) NULL,  
    [AUsageScenario]    NVARCHAR (MAX) NULL,  
    [Adomain]           NVARCHAR (MAX) NULL,  
    [AinitialVersionId] NVARCHAR (MAX) NULL,  
    [AppID]             NVARCHAR (MAX) NULL,  
    [EndPointID]        INT            NULL,  
    [Record_Type]       NVARCHAR (MAX) NULL,  
    [InsertedDate]      DATETIME       NULL,  
    [DeletedDate]       DATETIME       NULL,  
    [UpdatedDate]       DATETIME       NULL,  
    [IPAddress]         NVARCHAR (MAX) NULL,  
    PRIMARY KEY CLUSTERED ([AID] ASC),  
    FOREIGN KEY ([EndPointID]) REFERENCES [dbo].[LuisEndPointLocations] ([LID])  
);  
Go  
/*          
Exec LocationEndpoint_CRUD '{      
  "LID": 1,      
  "Flag": "R",      
  "Record_Type": "N",      
  "InsertedDate": "12/2/2019 1:19:29 AM"      
}'      
Exec LocationEndpoint_CRUD '{  "Flag": "R",  "LEndPoint": "ManiTeja",  "LEndPointUrl": "Maniteja Vegi",  "LEndPointCode": "VMT", "LID":"2"}'          
*/          
CREATE Proc LocationEndpoint_CRUD          
(          
@json nvarchar(max)          
)          
as           
BEgin          
Declare @Flag char(1);          
Declare @LID int;          
Set @Flag =  JSON_VALUE(@json,'$.Flag') ;          
Set @LID =  JSON_VALUE(@json,'$.LID') ;          
--Select @Flag           
          
IF @Flag = 'I'          
BEGIN           
BEGIN TRY                    
BEGIN TRANSACTION                    
INSERT INTO LuisEndPointLocations Select * from OPENJSON(@json) with (          
LEndPoint nvarchar(max)            
,LEndPointUrl nvarchar(max)            
,LEndPointCode nvarchar(max)           
,Record_Type char(1)             
,InsertedDate datetime             
,DeletedDate datetime              
,UpdatedDate datetime             
,IPAddress nvarchar(max)            
);          
COMMIT TRANSACTION        
Select '0' as ErrorCode,'Data Saved Successfully' as ErrorMessage FOR JSON  PATH          
END TRY        
BEGIN CATCH        
ROLLBACK TRANSACTION        
SELECT                      
            ERROR_NUMBER() AS ErrorNumber                      
            ,ERROR_SEVERITY() AS ErrorSeverity                      
            ,ERROR_STATE() AS ErrorState                      
            ,ERROR_PROCEDURE() AS ErrorProcedure                      
            ,ERROR_LINE() AS ErrorLine                      
            ,ERROR_MESSAGE() AS ErrorMessage FOR JSON PATH;                      
END CATCH        
        
END          
IF @Flag = 'U'          
BEGIN          
BEGIN TRY                    
BEGIN TRANSACTION                    
update LuisEndPointLocations          
Set           
LEndPoint   = JSON_VALUE(@json,'$.LEndPoint')           
,LEndPointUrl   = JSON_VALUE(@json,'$.LEndPointUrl')           
,LEndPointCode  = JSON_VALUE(@json,'$.LEndPointCode')           
,Record_Type   = JSON_VALUE(@json,'$.Record_Type')           
,UpdatedDate   = JSON_VALUE(@json,'$.UpdatedDate')           
,IPAddress    = JSON_VALUE(@json,'$.IPAddress')           
where LID =  JSON_VALUE(@json,'$.LID') ;          
COMMIT TRANSACTION        
Select '0' as ErrorNumber,'Data Updated Successfully' as ErrorMessage FOR JSON  PATH          
END TRY        
BEGIN CATCH        
ROLLBACK TRANSACTION        
SELECT                      
            ERROR_NUMBER() AS ErrorNumber                      
            ,ERROR_SEVERITY() AS ErrorSeverity                      
            ,ERROR_STATE() AS ErrorState                      
            ,ERROR_PROCEDURE() AS ErrorProcedure                      
            ,ERROR_LINE() AS ErrorLine                      
            ,ERROR_MESSAGE() AS ErrorMessage FOR JSON PATH;                      
        
END CATCH        
END          
IF @Flag = 'D'          
BEGIN          
BEGIN TRY                    
BEGIN TRANSACTION                    
Update LuisEndPointLocations          
set Record_Type   = JSON_VALUE(@json,'$.Record_Type') ,          
DeletedDate   = JSON_VALUE(@json,'$.DeletedDate')           
where LID =  JSON_VALUE(@json,'$.LID') ;          
COMMIT TRANSACTION        
Select '0' as ErrorNumber,'Data Deleted Successfully' as ErrorMessage FOR JSON  PATH          
END TRY        
BEGIN CATCH        
ROLLBACK TRANSACTION        
END CATCH        
END          
IF @Flag = 'R'          
BEGIN          
IF @LID = 0            
BEGIN        
Select * from LuisEndPointLocations where Record_Type = 'L' for JSON PATH          
END        
ELSE        
BEGIN        
Select * from LuisEndPointLocations where LID =  JSON_VALUE(@json,'$.LID')  for JSON PATH                        
END        
END          
end     
  
Go        
/*              
Exec LUISAppMaster_CRUD '{      
  "AName": "Test",      
  "ADescription": "test",      
  "ACulture": "en-us",      
  "AtokenizerVersion": "1.0.0",      
  "AUsageScenario": "IoT",      
  "Adomain": "Booking & Reference",      
  "AinitialVersionId": "1",      
  "EndPointID": 1,      
  "AppID": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",      
  "Flag": "I",      
  "Record_Type": "L",      
  "InsertedDate": "12/1/2019 5:35:20 PM"      
}'                  
*/                  
CREATE Proc LUISAppMaster_CRUD                  
(                  
@json nvarchar(max)                  
)                  
as                   
BEgin                  
Declare @Flag char(1);                  
Declare @AID int;            
Set @Flag =  JSON_VALUE(@json,'$.Flag') ;                  
Set @AID =   JSON_VALUE(@json,'$.AID') ;                  
--Select @Flag                   
                  
IF @Flag = 'I'                  
BEGIN                  
BEGIN TRY              
BEGIN TRANSACTION              
INSERT INTO LUISAppMaster Select * from OPENJSON(@json) with (                 
AName nvarchar(max)                    
,ADescription nvarchar(max)                    
,ACulture nvarchar(max)                    
,AtokenizerVersion nvarchar(max)                    
,AUsageScenario nvarchar(max)                    
,Adomain nvarchar(max)                    
,AinitialVersionId nvarchar(max)                    
,AppID nvarchar(max)                
,EndPointID int      
,Record_Type nvarchar(max)                    
,InsertedDate datetime                
,DeletedDate datetime                
,UpdatedDate datetime                
,IPAddress nvarchar(max)          
);              
Select '0' as ErrorNumber,'Data Saved Successfully' as ErrorMessage FOR JSON  PATH                      
COMMIT TRANSACTION              
END TRY              
BEGIN CATCH              
ROLLBACK TRANSACTION              
 SELECT                
            ERROR_NUMBER() AS ErrorNumber                
            ,ERROR_SEVERITY() AS ErrorSeverity                
            ,ERROR_STATE() AS ErrorState                
            ,ERROR_PROCEDURE() AS ErrorProcedure                
            ,ERROR_LINE() AS ErrorLine                
            ,ERROR_MESSAGE() AS ErrorMessage FOR JSON PATH;                
END CATCH               
END                  
IF @Flag = 'U'                  
BEGIN                  
BEGIN TRY              
BEGIN TRANSACTION              
update LUISAppMaster                 
Set                   
AName    =   JSON_VALUE(@json,'$.AName')                 
,ADescription  =   JSON_VALUE(@json,'$.ADescription')                
,Record_Type   =   JSON_VALUE(@json,'$.Record_Type')                
,UpdatedDate   =   JSON_VALUE(@json,'$.UpdatedDate')                
,IPAddress   =   JSON_VALUE(@json,'$.IPAddress')                
where AID =  JSON_VALUE(@json,'$.AID') ;                  
Select '0' as ErrorNumber,'Data Updated Successfully' as ErrorMessage FOR JSON  PATH                  
COMMIT TRANSACTION              
END TRY              
BEGIN CATCH              
ROLLBACK TRANSACTION              
SELECT                
            ERROR_NUMBER() AS ErrorNumber                
            ,ERROR_SEVERITY() AS ErrorSeverity                
            ,ERROR_STATE() AS ErrorState                
            ,ERROR_PROCEDURE() AS ErrorProcedure                
            ,ERROR_LINE() AS ErrorLine                
            ,ERROR_MESSAGE() AS ErrorMessage FOR JSON PATH;                
END CATCH              
END                  
IF @Flag = 'D'                  
BEGIN                  
BEGIN TRY              
BEGIN TRANSACTION              
Update LUISAppMaster                  
set Record_Type   = JSON_VALUE(@json,'$.Record_Type') ,                  
DeletedDate   = JSON_VALUE(@json,'$.DeletedDate')                   
where AID =  JSON_VALUE(@json,'$.AID') ;         
Select '0' as ErrorNumber,'Data Deleted Successfully' as ErrorMessage FOR JSON  PATH                  
COMMIT TRANSACTION              
END TRY              
BEGIN CATCH              
ROLLBACK TRANSACTION              
SELECT                
           ERROR_NUMBER() AS ErrorNumber                
            ,ERROR_SEVERITY() AS ErrorSeverity                
 ,ERROR_STATE() AS ErrorState                
            ,ERROR_PROCEDURE() AS ErrorProcedure                
            ,ERROR_LINE() AS ErrorLine                
            ,ERROR_MESSAGE() AS ErrorMessage FOR JSON PATH;                
END CATCH              
END                  
IF @Flag = 'R'                  
BEGIN               
IF @AID != 0            
BEGIN            
Select * from LUISAppMaster where AID =  JSON_VALUE(@json,'$.AID')  for JSON PATH                  
END            
ELSE          
BEGIN            
Select * from LUISAppMaster where AID = 'L' for JSON PATH                  
END          
END                  
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 the LUIS.AI portal, run DB Script into your database, and run the application.

Happy Coding


Similar Articles