Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QnA ASP Net Core sample's #12 and #22 (with AppInsights) #325

Merged
merged 5 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add cmullins feedback
  • Loading branch information
daveta committed Sep 6, 2018
commit 73067cf38b71d8d027ecf2b01d4194871ec743cc
2 changes: 1 addition & 1 deletion csharp_dotnetcore/11.QnAMaker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace QnABot
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// This is an ASP.NET Core app that creates a webserver.
Expand Down
11 changes: 1 addition & 10 deletions csharp_dotnetcore/11.QnAMaker/QnABot.bot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "QnABot",
"description": "Qna Maker Bot Sample",
"description": "",
"secretKey": "",
"services": [
{
Expand All @@ -10,15 +10,6 @@
"appId": "",
"appPassword": "",
"endpoint": "http://localhost:3978/api/messages"
},
{
"type": "qna",
"id": "",
"name": "QnaBot",
"kbId": "<Fill In Here>",
"subscriptionKey": "",
"endpointKey": "<Fill in Here>",
"hostname": "<Fill In Here>"
}
]
}
15 changes: 7 additions & 8 deletions csharp_dotnetcore/11.QnAMaker/QnAMaker/BotServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
using System.Collections.Generic;
using Microsoft.Bot.Builder.AI.QnA;

namespace QnABot
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// Represents the bot's references to external services.
/// Represents references to external services.
///
/// For example, the QnaMaker service is kept here (singletons). This external service is configured
/// For example, the QnA Maker service is kept here as a singleton. This external service is configured
/// using the <see cref="Microsoft.Bot.Configuration.BotConfiguration"/> class
/// (based on the contents of your ".bot" file).
/// </summary>
[Serializable]
public class BotServices
{
/// <summary>
Expand All @@ -27,12 +26,12 @@ public BotServices(Dictionary<string, QnAMaker> qnaServices)
}

/// <summary>
/// Gets the (potential) set of QnA Services used.
/// Gets the set of QnA Maker services used.
/// Given there can be multiple <see cref="QnAMaker"/> services used in a single bot,
/// QnA is represented as a Dictionary. This is also modeled in the
/// ".bot" file since the elements are named (string).
/// This sample only uses a single <see cref="QnAMaker"/> instance.
/// QnA Maker instances are represented as a Dictionary. This is also modeled in the
/// ".bot" file using named elements.
/// </summary>
/// <remarks>The QnA Maker services collection should not be modified while the bot is running.</remarks>
/// <value>
/// A <see cref="QnAMaker"/> client instance created based on configuration in the .bot file.
/// </value>
Expand Down
30 changes: 16 additions & 14 deletions csharp_dotnetcore/11.QnAMaker/QnAMaker/QnABot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;

namespace QnABot
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// Represents a bot that can process incoming activities.
/// For each interaction from the user, an instance of this class is called.
/// For each interaction from the user, an instance of this class is created and
/// the OnTurnAsync method is called.
/// This is a Transient lifetime service. Transient lifetime services are created
/// each time they're requested. For each Activity received, a new instance of this
/// class is created. Objects that are expensive to construct, or have a lifetime
Expand All @@ -19,10 +19,10 @@ namespace QnABot
public class QnABot : IBot
{
/// <summary>
/// Key in the Bot config (.bot file) for the QnaMaker instance.
/// In the .bot file, multiple instances of QnaMaker can be configured.
/// Key in the bot config (.bot file) for the QnA Maker instance.
/// In the ".bot" file, multiple instances of QnA Maker can be configured.
/// </summary>
public static readonly string QnAMakerKey = "QnaBot";
public static readonly string QnAMakerKey = "QnABot";

/// <summary>
/// Services configured from the ".bot" file.
Expand All @@ -32,20 +32,20 @@ public class QnABot : IBot
/// <summary>
/// Initializes a new instance of the <see cref="QnABot"/> class.
/// </summary>
/// <param name="services">Services configured from the ".bot" file.</param>
/// <param name="services">A <see cref="BotServices"/> configured from the ".bot" file.</param>
public QnABot(BotServices services)
{
_services = services ?? throw new System.ArgumentNullException(nameof(services));
if (!_services.QnAServices.ContainsKey(QnAMakerKey))
{
throw new System.ArgumentException($"Invalid configuration. Please check your '.bot' file for a QnA service named '{QnAMakerKey}'.");
throw new System.ArgumentException($"Invalid configuration. Please check your '.bot' file for a QnA service named '{QnAMakerKey}'.");
}
}

/// <summary>
/// Every Conversation turn for our QnA Bot will call this method.
/// There are no dialogs used, since it's "single turn" processing, meaning a single
/// request and response, with no stateful conversation.
/// Every conversation turn for our QnA bot will call this method.
/// There are no dialogs used, the sample only uses "single turn" processing,
/// meaning a single request and response, with no stateful conversation.
/// </summary>
/// <param name="context">A <see cref="ITurnContext"/> containing all the data needed
/// for processing this conversation turn. </param>
Expand All @@ -56,16 +56,18 @@ public QnABot(BotServices services)
{
if (context.Activity.Type == ActivityTypes.Message)
{
// Check QnAMaker model
// Check QnA Maker model
var response = await _services.QnAServices[QnAMakerKey].GetAnswersAsync(context);

if (response != null && response.Length > 0)
{
await context.SendActivityAsync(response[0].Answer);
}
else
{
await context.SendActivityAsync("No QnA Maker answers were found. This example uses a QnA Maker Knowledge Base that focuses on smart light bulbs. To see QnA Maker in action, ask the bot questions like \"Why won't it turn on?\" or say something like \"I need help.\"");
var msg = @"No QnA Maker answers were found. This example uses a QnA Maker Knowledge Base that focuses on smart light bulbs.
To see QnA Maker in action, ask the bot questions like 'Why won't it turn on?' or 'I need help'.";

await context.SendActivityAsync(msg);
}
}
}
Expand Down
39 changes: 21 additions & 18 deletions csharp_dotnetcore/11.QnAMaker/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
This sample shows how to integrate QnA Maker in a simple bot with ASP.Net Core 2.

# Concepts introduced in this sample
The [QnA Maker Service](https://www.qnamaker.ai) enables you to build, train and publish a simple question and answer bot based on FAQ URLs, structured documents or editorial content in minutes.
The [QnA Maker Service](https://www.qnamaker.ai) enables you to build, train and publish a simple question
and answer bot based on FAQ URLs, structured documents or editorial content in minutes.

In this sample, we demonstrate how to use the QnA Maker service to answer questions based on a FAQ text file as input.

Expand All @@ -13,33 +14,35 @@ git clone https://github.com/Microsoft/botbuilder-samples.git
```

## Prerequisites
- Follow instructions [here](https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/how-to/set-up-qnamaker-service-azure) to create a QnA Maker service.
- Follow instructions [here](https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/how-to/migrate-knowledge-base) to import the [sample.qna](sample.qna) to your newly created QnA Maker service.
- Update [QnABot.bot](QnABot.bot) with your kbid (KnowledgeBase Id) and endpointKey in the "qna" services section. You can find this information under "Settings" tab for your QnA Maker Knowledge Base at [QnAMaker.ai](https://www.qnamaker.ai)
- (Optional) Follow instructions [here](https://github.com/Microsoft/botbuilder-tools/tree/master/packages/QnAMaker) to set up the Qna Maker CLI to deploy the model.


## Visual studio
- Navigate to the samples folder (`BotBuilder-Samples\csharp_dotnetcore\11.QnAMaker`) and open QnABot.csproj in Visual studio
- Follow instructions [here](https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/how-to/set-up-qnamaker-service-azure)
to create a QnA Maker service.
- Follow instructions [here](https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/how-to/migrate-knowledge-base) to
import the [sample.qna](sample.qna) to your newly created QnA Maker service.
- Update [QnABot.bot](QnABot.bot) with your kbid (KnowledgeBase Id) and endpointKey in the "qna" services section. You can find this
information under "Settings" tab for your QnA Maker Knowledge Base at [QnAMaker.ai](https://www.qnamaker.ai)
- (Optional) Follow instructions [here](https://github.com/Microsoft/botbuilder-tools/tree/master/packages/QnAMaker) to set up the
QnA Maker CLI to deploy the model.

## Visual Studio
- Navigate to the samples folder (`BotBuilder-Samples\csharp_dotnetcore\11.QnAMaker`) and open QnABot.csproj in Visual Studio .
- Hit F5

## Visual studio code
## Visual Studio Code
- Open `BotBuilder-Samples\csharp_dotnetcore\11.QnAMaker` sample folder.
- Bring up a terminal, navigate to `BotBuilder-Samples\csharp_dotnetcore\11.QnAMaker` folder
- type 'dotnet run'
- Bring up a terminal, navigate to `BotBuilder-Samples\csharp_dotnetcore\11.QnAMaker` folder.
- Type 'dotnet run'.

## Testing the bot using Bot Framework Emulator
[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-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](https://aka.ms/botframeworkemulator).
- Install the Bot Framework Emulator from [here](https://aka.ms/botframeworkemulator).

### Connect to bot using Bot Framework Emulator **V4**
- Launch Bot Framework Emulator
- File -> Open bot and navigate to `BotBuilder-Samples\csharp_dotnetcore\11.QnAMaker` folder
- Select QnABot.bot file
- Launch the Bot Framework Emulator
- File -> Open bot and navigate to `BotBuilder-Samples\csharp_dotnetcore\11.QnAMaker` folder.
- Select the QnABot.bot file.

# Further reading

- [Azure Bot Service Introduction](https://docs.microsoft.com/en-us/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
- [Azure Bot Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
- [QnA Maker documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/overview/overview)
- [QnA Maker command line tool](https://github.com/Microsoft/botbuilder-tools/tree/master/packages/QnAMaker)
35 changes: 13 additions & 22 deletions csharp_dotnetcore/11.QnAMaker/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace QnABot
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// The Startup class configures services and the app's request pipeline.
/// </summary>
public class Startup
{
/// <summary>
Expand All @@ -30,15 +27,13 @@ public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();

Configuration = builder.Build();
}

/// <summary>
/// Gets the configuration that represents a set of key/value application configuration properties.
/// Gets the configuration that represents a set of key/value configuration properties.
/// </summary>
/// <value>
/// The <see cref="IConfiguration"/> that represents a set of key/value application configuration properties.
Expand All @@ -48,16 +43,14 @@ public Startup(IHostingEnvironment env)
/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// </summary>
/// <param name="services">Specifies the contract for a collection of service descriptors.</param>
/// <seealso cref="IServiceCollection"/>
/// <seealso cref="https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/dependency-injection"/>
/// <param name="services">Specifies the contract for a <see cref="IServiceCollection"/> of service descriptors.</param>
/// <seealso cref="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1"/>
public void ConfigureServices(IServiceCollection services)
{
// Load the connected services from .bot file
// BUGBUG: This needs to change to common bot file, once emulator understand appInsights type.
// Load the connected services from .bot file.
var botConfig = BotConfiguration.Load(@".\QnABot.bot");

// Initialize Bot Connected Services Clients
// Initialize Bot Connected Services clients.
var connectedServices = InitBotServices(botConfig);
services.AddSingleton(sp => connectedServices);

Expand All @@ -72,8 +65,8 @@ public void ConfigureServices(IServiceCollection services)
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app">The application builder. This provides the mechanisms to configure an application's request pipeline.</param>
/// <param name="env">Provides information about the web hosting environment an application is running in.</param>
/// <param name="app">The application builder. This provides the mechanisms to configure the application request pipeline.</param>
/// <param name="env">Provides information about the web hosting environment.</param>
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDefaultFiles()
Expand All @@ -83,11 +76,10 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)

/// <summary>
/// Initialize the bot's references to external services.
///
/// For example, the QnaMaker instance is created here. This external service is configured
/// For example, the Qna Maker instance is created here. This external service is configured
/// using the <see cref="BotConfiguration"/> class (based on the contents of your ".bot" file).
/// </summary>
/// <param name="config">Configuration object based on your ".bot" file.</param>
/// <param name="config">The <see cref="BotConfiguration"/> object based on your ".bot" file.</param>
/// <returns>A <see cref="BotConfiguration"/> representing client objects to access external services the bot uses.</returns>
/// <seealso cref="BotConfiguration"/>
/// <seealso cref="QnAMaker"/>
Expand All @@ -103,7 +95,6 @@ private static BotServices InitBotServices(BotConfiguration config)
{
// Create a QnA Maker that is initialized and suitable for passing
// into the IBot-derived class (QnABot).
// In this case, we're creating a QnAMaker client.
var qna = (QnAMakerService)service;
if (qna == null)
{
Expand All @@ -112,17 +103,17 @@ private static BotServices InitBotServices(BotConfiguration config)

if (string.IsNullOrWhiteSpace(qna.KbId))
{
throw new InvalidOperationException("The Qna KnowledgeBaseId ('kbId') is required to run this sample. Please update your '.bot' file.");
throw new InvalidOperationException("The QnA KnowledgeBaseId ('kbId') is required to run this sample. Please update your '.bot' file.");
}

if (string.IsNullOrWhiteSpace(qna.EndpointKey))
{
throw new InvalidOperationException("The Qna EndpointKey ('endpointKey') is required to run this sample. Please update your '.bot' file.");
throw new InvalidOperationException("The QnA EndpointKey ('endpointKey') is required to run this sample. Please update your '.bot' file.");
}

if (string.IsNullOrWhiteSpace(qna.Hostname))
{
throw new InvalidOperationException("The Qna Host ('hostname') is required to run this sample. Please update your '.bot' file.");
throw new InvalidOperationException("The QnA Host ('hostname') is required to run this sample. Please update your '.bot' file.");
}

var qnaEndpoint = new QnAMakerEndpoint()
Expand Down
2 changes: 0 additions & 2 deletions csharp_dotnetcore/11.QnAMaker/appsettings.json

This file was deleted.

4 changes: 2 additions & 2 deletions csharp_dotnetcore/11.QnAMaker/wwwroot/default.htm
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

</head>
<body style="font-family:'Segoe UI'">
<h1>QnAMaker bot sample using ASP.Net Core 2.</h1>
<h1>QnA Maker bot sample using ASP.Net Core 2.</h1>
<p>Describe your bot here and your terms of use etc.</p>
<p>To debug you bot using the Bot Framework Emulator, paste this URL into the Emulator window:</p>
<p>To debug you bot using the Bot Framework Emulator, paste this URL into the emulator window:</p>
<div style="padding-left: 50px;" id="botBaseUrl"></div>
<p>Visit <a href="https://www.botframework.com/">Bot Framework</a> to register your bot. When you register, remember to set the endpoint to:</p>
<div style="padding-left: 50px;">
Expand Down
2 changes: 1 addition & 1 deletion csharp_dotnetcore/22.QnA-with-AppInsights/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace QnABot
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// This is an ASP.NET Core app that creates a webserver.
Expand Down
25 changes: 1 addition & 24 deletions csharp_dotnetcore/22.QnA-with-AppInsights/QnABot.bot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "QnABot",
"description": "Qna Maker Bot Sample",
"description": "",
"secretKey": "",
"services": [
{
Expand All @@ -10,29 +10,6 @@
"appId": "",
"appPassword": "",
"endpoint": "http://localhost:3978/api/messages"
},
{
"type": "qna",
"id": "",
"name": "QnaBot",
"kbId": "<Fill In Here>",
"subscriptionKey": "",
"endpointKey": "<Fill in Here>",
"hostname": "<Fill In Here>"
},
{
"type": "appInsights",
"id": "3",
"name": "",
"tenantId": "",
"subscriptionId": "",
"resourceGroup": "",
"instrumentationKey": "<Fill In Here>",
"applicationId": "00000000-0000-0000-0000-000000000007",
"apiKeys": {
"key1": "testKey1",
"key2": "testKey2"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
using Microsoft.ApplicationInsights;
using Microsoft.Bot.Builder.AI.QnA;

namespace QnABot
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// Represents the bot's references to external services.
///
/// For example, Application Insights and QnaMaker services
/// are kept here (singletons). These external services are configured
/// using the BotConfigure class (based on the contents of your ".bot" file).
/// are configured here using the <see cref="Microsoft.Bot.Configuration.BotConfiguration"/> class
/// (based on the contents of your ".bot" file).
/// </summary>
[Serializable]
public class BotServices
{
/// <summary>
Expand Down
Loading