This sample shows how two Cognitive Services, LUIS and Personalizer, can be integrated into a coffee recommendation chat bot using ASP.NET Core 2.
- Clone the samples repository
git clone https://github.com/Azure-Samples/cognitive-services-personalizer-samples.git
- Open ChatbotSample.sln in Visual Studio or VS Code
- Create both a Personalizer and a Language Understanding (LUIS) resource in the Azure portal. These will be used to power the reinforcement learning and NLP cababilities of the bot, respectively.
- NOTE: when creating a LUIS resource in the portal, you can choose between creating a prediction resource, an authoring resource, or both. Only an authoring resource is needed for this sample. More on the two here
- In appsettings.json, modify
PersonalizerServiceEndpoint
to be your resource's endpoint; this can be found in the quick start tab in the portal - Configure the PersonalizerEndpointKey (can also be found in the quick start tab) as an app secret in one of the following ways:
- If you are using VS Code, you can use the
dotnet user-secrets set "PersonalizerEndpointKey" "<Endpoint Key>"
command. - If you are using Visual Studio, you can right-click the project and select the Manage User Secrets menu option to configure the Personalizer keys. By doing this, Visual Studio will open a secrets.json file where you can add the keys as follows:
{ "PersonalizerEndpointKey": "<your endpoint key>", }
- If you are using VS Code, you can use the
-
Navigate to LUIS portal.
-
Click the
Sign in
button. -
Click on
My Apps
. -
Select the LUIS authoring resource you created earlier.
-
Click on the
Import new app
button. -
Click on the
Choose File
and select coffeebot.json from thecognitive-services-personalizer-samples/samples/ChatbotExample/CognitiveModels
folder. -
Click on the
Train
button on the top right of the page; this will train your app on the model inside of chatbot.json. -
Once this is done, press the
Publish
button and select "Production". NOTE: Once you publish your app on LUIS portal for the first time, it takes some time for the endpoint to become available, about 5 minutes of wait should be sufficient. -
Update nlp-with-luis.bot file with your AppId, AuthoringKey, Region and Version. You can find this information under the
Manage
tab.- The
AppID
can be found in "Application Information" - The
AuthoringKey
can be found in "Azure Resources" > "Authoring Resource" > "Primary key" - The
Region
is listed above the primary key (authoring key) - The
Version
is located next to the name of your app in the top left corner
You will have something similar to this in the services section of your .bot file to run this sample:
{ "type":"luis", "name":"<some name>", "appId":"<your app id>", "version":"<your version number>", "authoringKey":"<your authoring key>", "region":"<your region>", "id":"<some number>" },
- The
-
[Optional] Update the
appsettings.json
file undercognitive-services-personalizer-samples/samples/ChatbotExample/
with your botFileSecret. For Azure Bot Service bots, you can find the botFileSecret under application settings.
- Right click on the LuisBot project file > Properties > Debug and change the App URL to match the base URL of the endpoint listed in nlp-with-luis.bot
- Run the project (press
F5
key)
- Open the
launch.json
file generated for you inside./vscode
; inside ofConfigurations.env
, add an entry with key"ASPNETCORE_URLS: <your bot endpoint>
, where your bot endpoint is the base URL of the endpoint listed in nlp-with-luis.bot For example:Json "env": { "ASPNETCORE_ENVIRONMENT": "Development", "ASPNETCORE_URLS": "http://localhost:4034" },
- Bring up a terminal, navigate to
cognitive-services-personalizer-samples/samples/ChatbotExample/
folder. - Type
dotnet run
.
Microsoft Bot Framework Emulator is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel.
- Install the Bot Framework Emulator from here.
- Launch the Bot Framework Emulator
- File -> Open bot and navigate to
cognitive-services-personalizer-samples/samples/ChatbotExample/
folder - Select
nlp-with-luis.bot
file
The chatbot can understand human inputs by defined "Intents" and "utterances". Those definitions can be found in coffeebot.json. For example, if you type "what do you suggest", Luis will be able to categorize it as the Intent - "ChooseRank".
In this example, Luis Intents will be processed in LuisBot.cs and then it will call the Ranking API to get a suggestion, shown below:
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
var recognizerResult = await _services.LuisServices[LuisKey].RecognizeAsync(turnContext, cancellationToken);
var topIntent = recognizerResult?.GetTopScoringIntent();
if (topIntent != null && topIntent.HasValue && topIntent.Value.intent != "None")
{
Intents intent = (Intents)Enum.Parse(typeof(Intents), topIntent.Value.intent);
switch (intent)
{
...
case Intents.ChooseRank:
var response = await ChooseRankAsync(turnContext, _rlFeaturesManager.GenerateEventId());
await turnContext.SendActivityAsync($"What about {response.RewardActionId}");
break;
...
}
...
}
...
}
...
}
Follow this tutorial if you want to deploy your bot to Azure. Additionally, if you would like to register your bot with Azure Bot Service, you can follow the steps at this link.