// <Main> static void Main(string[] args) { var authoringKey = "REPLACE-WITH-YOUR-QNA-MAKER-KEY"; var resourceName = "REPLACE-WITH-YOUR-RESOURCE-NAME"; // <AuthorizationAuthoring> var client = new QnAMakerClient(new ApiKeyServiceClientCredentials(authoringKey)) { Endpoint = $"https://{resourceName}.cognitiveservices.azure.com" }; // </AuthorizationAuthoring> var kbId = CreateSampleKb(client).Result; UpdateKB(client, kbId).Wait(); PublishKb(client, kbId).Wait(); DownloadKb(client, kbId).Wait(); var primaryPredictionEndpointKey = GetPredictionEndpointKey(client).Result; // <AuthorizationPrediction> var runtimeClient = new QnAMakerRuntimeClient(new EndpointKeyServiceClientCredentials(primaryPredictionEndpointKey)) { RuntimeEndpoint = $"https://{resourceName}.azurewebsites.net" }; // </AuthorizationPrediction> GenerateAnswer(runtimeClient, kbId).Wait(); DeleteKB(client, kbId).Wait(); }
/// <summary> /// This method gets called by the runtime. Use this method to add services to the container. /// </summary> /// <param name="services"> Service Collection Interface.</param> public void ConfigureServices(IServiceCollection services) { services.AddApplicationInsightsTelemetry(); services.Configure <KnowledgeBaseSettings>(knowledgeBaseSettings => { knowledgeBaseSettings.SearchServiceName = this.Configuration["SearchServiceName"]; knowledgeBaseSettings.SearchServiceQueryApiKey = this.Configuration["SearchServiceQueryApiKey"]; knowledgeBaseSettings.SearchServiceAdminApiKey = this.Configuration["SearchServiceAdminApiKey"]; knowledgeBaseSettings.SearchIndexingIntervalInMinutes = this.Configuration["SearchIndexingIntervalInMinutes"]; knowledgeBaseSettings.StorageConnectionString = this.Configuration["StorageConnectionString"]; knowledgeBaseSettings.IsGCCHighDeployment = this.Configuration.GetValue <bool>("IsGCCHighDeployment"); }); services.Configure <QnAMakerSettings>(qnAMakerSettings => { qnAMakerSettings.ScoreThreshold = this.Configuration["ScoreThreshold"]; }); services.Configure <BotSettings>(botSettings => { botSettings.AccessCacheExpiryInDays = Convert.ToInt32(this.Configuration["AccessCacheExpiryInDays"]); botSettings.AppBaseUri = this.Configuration["AppBaseUri"]; botSettings.MicrosoftAppId = this.Configuration["MicrosoftAppId"]; botSettings.TenantId = this.Configuration["TenantId"]; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddSingleton <Common.Providers.IConfigurationDataProvider>(new Common.Providers.ConfigurationDataProvider(this.Configuration["StorageConnectionString"])); services.AddHttpClient(); services.AddSingleton <ICredentialProvider, ConfigurationCredentialProvider>(); services.AddSingleton <ITicketsProvider>(new TicketsProvider(this.Configuration["StorageConnectionString"])); services.AddSingleton <IBotFrameworkHttpAdapter, BotFrameworkHttpAdapter>(); services.AddSingleton(new MicrosoftAppCredentials(this.Configuration["MicrosoftAppId"], this.Configuration["MicrosoftAppPassword"])); IQnAMakerClient qnaMakerClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(this.Configuration["QnAMakerSubscriptionKey"])) { Endpoint = this.Configuration["QnAMakerApiEndpointUrl"] }; string endpointKey = Task.Run(() => qnaMakerClient.EndpointKeys.GetKeysAsync()).Result.PrimaryEndpointKey; services.AddSingleton <IQnaServiceProvider>((provider) => new QnaServiceProvider( provider.GetRequiredService <Common.Providers.IConfigurationDataProvider>(), provider.GetRequiredService <IOptionsMonitor <QnAMakerSettings> >(), qnaMakerClient, new QnAMakerRuntimeClient(new EndpointKeyServiceClientCredentials(endpointKey)) { RuntimeEndpoint = this.Configuration["QnAMakerHostUrl"] })); services.AddSingleton <IActivityStorageProvider>((provider) => new ActivityStorageProvider(provider.GetRequiredService <IOptionsMonitor <KnowledgeBaseSettings> >())); services.AddSingleton <IKnowledgeBaseSearchService>((provider) => new KnowledgeBaseSearchService(this.Configuration["SearchServiceName"], this.Configuration["SearchServiceQueryApiKey"], this.Configuration["SearchServiceAdminApiKey"], this.Configuration["StorageConnectionString"], this.Configuration.GetValue <bool>("IsGCCHighDeployment"))); services.AddSingleton <ISearchService, SearchService>(); services.AddSingleton <IMemoryCache, MemoryCache>(); services.AddTransient(sp => (BotFrameworkAdapter)sp.GetRequiredService <IBotFrameworkHttpAdapter>()); services.AddTransient <IBot, FaqPlusPlusBot>(); // Create the telemetry middleware(used by the telemetry initializer) to track conversation events services.AddSingleton <TelemetryLoggerMiddleware>(); services.AddMemoryCache(); }
// <Main> static void Main(string[] args) { // <Resourcevariables> var authoringKey = "QNA_MAKER_SUBSCRIPTION_KEY"; var authoringURL = "QNA_MAKER_ENDPOINT"; var queryingURL = "QNA_MAKER_RUNTIME_ENDPOINT"; // </Resourcevariables> // <AuthorizationAuthor> var client = new QnAMakerClient(new ApiKeyServiceClientCredentials(authoringKey)) { Endpoint = authoringURL }; // </AuthorizationAuthor> var kbId = CreateSampleKb(client).Result; UpdateKB(client, kbId).Wait(); PublishKb(client, kbId).Wait(); DownloadKb(client, kbId).Wait(); var primaryQueryEndpointKey = GetQueryEndpointKey(client).Result; // <AuthorizationQuery> var runtimeClient = new QnAMakerRuntimeClient(new EndpointKeyServiceClientCredentials(primaryQueryEndpointKey)) { RuntimeEndpoint = queryingURL }; // </AuthorizationQuery> GenerateAnswer(runtimeClient, kbId).Wait(); DeleteKB(client, kbId).Wait(); }
/// <summary> /// Register Autofac dependencies. /// </summary> /// <returns>Autofac container.</returns> public static IContainer RegisterDependencies() { var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.Register(c => new ConfigurationDataProvider( ConfigurationManager.AppSettings["StorageConnectionString"])) .As <IConfigurationDataProvider>() .SingleInstance(); var qnaMakerClient = new QnAMakerClient( new ApiKeyServiceClientCredentials( ConfigurationManager.AppSettings["QnAMakerSubscriptionKey"])) { Endpoint = StripRouteFromQnAMakerEndpoint(ConfigurationManager.AppSettings["QnAMakerApiEndpointUrl"]) }; builder.Register(c => qnaMakerClient) .As <IQnAMakerClient>() .SingleInstance(); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); return(container); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string question = req.Query["question"]; string answer = req.Query["answer"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); question = question ?? data?.question; answer = answer ?? data?.answer; string responseMessage = string.IsNullOrEmpty(question) ? "This HTTP triggered function executed successfully. Pass a QnA pair." : $"Your question {question}, and the answer {answer}."; var authoringKey = GetEnvironmentVariable("authoringKey"); var authoringUrl = GetEnvironmentVariable("authoringUrl"); var kbId = GetEnvironmentVariable("kbId"); var sourceFile = GetEnvironmentVariable("sourceFile"); var client = new QnAMakerClient(new ApiKeyServiceClientCredentials(authoringKey)) { Endpoint = authoringUrl }; //Update Knowledge Base var updateKB = await client.Knowledgebase.UpdateAsync(kbId, new UpdateKbOperationDTO { Add = new UpdateKbOperationDTOAdd { QnaList = new List <QnADTO> { new QnADTO { Questions = new List <string> { question }, Answer = answer, Source = sourceFile, Metadata = new List <MetadataDTO> { new MetadataDTO { Name = "Category", Value = sourceFile }, } } }, } }); updateKB = await MonitorOperation(client, updateKB); //Publish Knowledge Base await client.Knowledgebase.PublishAsync(kbId); return(new OkObjectResult(responseMessage)); }
private static IEnumerable <IQnaServiceProvider> GetQnaServiceProviders(IServiceProvider provider, List <LanguageQnAMakerKeyCombination> languageQnAMakerKeyCombinations) { var qnaServiceProviders = new List <QnaServiceProvider>(); foreach (var languageQnAMakerKeyCombination in languageQnAMakerKeyCombinations) { IQnAMakerClient qnaMakerClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(languageQnAMakerKeyCombination.QnAMakerSubscriptionKey)) { Endpoint = ConfigurationManager.AppSettings["QnAMakerApiEndpointUrl"] }; string endpointKey = Task.Run(() => qnaMakerClient.EndpointKeys.GetKeysAsync()).Result.PrimaryEndpointKey; qnaServiceProviders.Add(new QnaServiceProvider( languageQnAMakerKeyCombination.LanguageCode, new ConfigurationDataProvider(ConfigurationManager.AppSettings["StorageConnectionString"]), null, qnaMakerClient, new QnAMakerRuntimeClient(new EndpointKeyServiceClientCredentials(endpointKey)) { RuntimeEndpoint = ConfigurationManager.AppSettings["QnAMakerHostUrl"] })); } return(qnaServiceProviders.AsEnumerable()); }
/// <summary> /// Register Autofac dependencies /// </summary> /// <returns>Autofac container</returns> public static IContainer RegisterDependencies() { var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.Register(c => new ConfigurationDataProvider( ConfigurationManager.AppSettings["StorageConnectionString"])) .As <IConfigurationDataProvider>() .SingleInstance(); var qnaMakerClient = new QnAMakerClient( new ApiKeyServiceClientCredentials( ConfigurationManager.AppSettings["QnAMakerSubscriptionKey"])) { Endpoint = StripRouteFromQnAMakerEndpoint(ConfigurationManager.AppSettings["QnAMakerApiEndpointUrl"]) }; builder.Register(c => qnaMakerClient) .As <IQnAMakerClient>() .SingleInstance(); List <LanguageQnAMakerKeyCombination> languageQnAMakerKeyCombinations = JsonConvert.DeserializeObject <List <LanguageQnAMakerKeyCombination> >(ConfigurationManager.AppSettings["LanguageQnAMakerSubscriptionKeyJson"]); IEnumerable <IQnaServiceProvider> qnaServiceProviders = GetQnaServiceProviders(null, languageQnAMakerKeyCombinations); builder.Register(c => qnaServiceProviders) .As <IEnumerable <IQnaServiceProvider> >() .SingleInstance(); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); return(container); }
/// <summary> /// Service Collection extension. /// /// Injects concrete implementation of QnAService. /// </summary> /// <param name="services">Service collection.</param> /// <param name="configuration">Configuration.</param> /// <returns>Service Collection.</returns> public static IServiceCollection AddQnAService(this IServiceCollection services, IConfiguration configuration) { // Add QnAMakerClient var subscriptionKey = configuration.GetValue <string>("QnAMaker:SubscriptionKey"); var endpoint = configuration.GetValue <string>("QnAMaker:Endpoint"); var qnaMakerClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(subscriptionKey)) { Endpoint = endpoint, }; services.AddSingleton <IQnAMakerClient>(qnaMakerClient); // Add QnAMakerRuntimeClient var runtimeEndpoint = configuration.GetValue <string>("QnAMaker:RuntimeEndpoint"); var endpointKey = configuration.GetValue <string>("QnAMaker:EndpointKey"); var qnaMakerRuntimeClient = new QnAMakerRuntimeClient(new EndpointKeyServiceClientCredentials(endpointKey)) { RuntimeEndpoint = runtimeEndpoint, }; services.AddSingleton <IQnAMakerRuntimeClient>(qnaMakerRuntimeClient); // Add QnAServiceSettings var qnaServiceSettings = new QnAServiceSettings() { ScoreThreshold = configuration.GetValue <int>("QnAMaker:ScoreThreshold"), KnowledgeBaseId = configuration.GetValue <string>("QnAMaker:KnowledgeBaseId"), }; services.AddSingleton <IQnAServiceSettings>(qnaServiceSettings); // Add QnAService services.AddTransient <IQnAService, QnAService>(); return(services); }
private IEnumerable <IQnaServiceProvider> GetQnaServiceProviders(IServiceProvider provider, List <LanguageQnAMakerKeyCombination> languageQnAMakerKeyCombinations) { var qnaServiceProviders = new List <QnaServiceProvider>(); foreach (var languageQnAMakerKeyCombination in languageQnAMakerKeyCombinations) { IQnAMakerClient qnaMakerClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(languageQnAMakerKeyCombination.QnAMakerSubscriptionKey)) { Endpoint = this.Configuration["QnAMakerApiEndpointUrl"] }; string endpointKey = Task.Run(() => qnaMakerClient.EndpointKeys.GetKeysAsync()).Result.PrimaryEndpointKey; qnaServiceProviders.Add(new QnaServiceProvider( languageQnAMakerKeyCombination.LanguageCode, provider.GetRequiredService <Common.Providers.IConfigurationDataProvider>(), provider.GetRequiredService <IOptionsMonitor <QnAMakerSettings> >(), qnaMakerClient, new QnAMakerRuntimeClient(new EndpointKeyServiceClientCredentials(endpointKey)) { RuntimeEndpoint = languageQnAMakerKeyCombination.QnAMakerHostUrl })); } return(qnaServiceProviders.AsEnumerable()); }
private async Task CognitiveServices_QnA_MigrationGuide_Authoring() { #region Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_CreateClient QnAMakerClient client = new QnAMakerClient(new ApiKeyServiceClientCredentials("{QnAMakerSubscriptionKey}"), new HttpClient(), false) { Endpoint = "{QnaMakerEndpoint}" }; #endregion Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_CreateClient #region Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_CreateKnowledgeBase Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker.Models.Operation createOp = await client.Knowledgebase.CreateAsync(new CreateKbDTO { Name = "{KnowledgeBaseName}", QnaList = new List <QnADTO> { new QnADTO { Questions = new List <string> { "{Question}" }, Answer = "{Answer}" } } }); #endregion Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_CreateKnowledgeBase #region Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_UpdateKnowledgeBase Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker.Models.Operation updateOp = await client.Knowledgebase.UpdateAsync("{KnowledgeBaseID}", new UpdateKbOperationDTO { Add = new UpdateKbOperationDTOAdd { QnaList = new List <QnADTO> { new QnADTO { Questions = new List <string> { "{Question}" }, Answer = "{Answer}" } } } }); #endregion Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_UpdateKnowledgeBase #region Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_DownloadKnowledgeBase QnADocumentsDTO kbdata = await client.Knowledgebase.DownloadAsync("{KnowledgeBaseID}", EnvironmentType.Test); #endregion Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_UpdateKnowledgeBase #region Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_DeleteKnowledgeBase await client.Knowledgebase.DeleteAsync("{KnowledgeBaseID}"); #endregion Snippet:CognitiveServices_QnA_Maker_Snippets_MigrationGuide_DeleteKnowledgeBase }
protected IQnAMakerClient GetQnAMakerClient(DelegatingHandler handler) { IQnAMakerClient client = new QnAMakerClient(new ApiKeyServiceClientCredentials(QnAMakerSubscriptionKey), handlers: handler) { Endpoint = "https://westus.api.cognitive.microsoft.com" }; return(client); }
protected IQnAMakerClient GetQnAMakerPreviewClient(DelegatingHandler handler) { IQnAMakerClient client = new QnAMakerClient(new ApiKeyServiceClientCredentials(QnAMakerSubscriptionKey), isPreview: true, handlers: handler) { Endpoint = "https://australiaeast.api.cognitive.microsoft.com" }; return(client); }
// <Main> static void Main(string[] args) { // <AuthoringAuthorization> string subscriptionKey = "PASTE_YOUR_QNA_MAKER_AUTHORING_SUBSCRIPTION_KEY_HERE"; string endpoint = "PASTE_YOUR_QNA_MAKER_AUTHORING_ENDPOINT_HERE"; // set tryPreview to 'true' for QnAMakerV2 resources bool tryPreview = false; var authoringClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(subscriptionKey), tryPreview) { Endpoint = endpoint }; // </AuthoringAuthorization> // <RuntimeAuthorization> string runtimeEndpoint = "PASTE_YOUR_QNA_MAKER_RUNTIME_ENDPOINT_HERE"; if (tryPreview) { runtimeEndpoint = endpoint; } string endpointKey = GetQueryEndpointKey(authoringClient).Result; QnAMakerRuntimeClient runtimeClient; if (tryPreview) { runtimeClient = new QnAMakerRuntimeClient(new ApiKeyServiceClientCredentials(subscriptionKey), tryPreview) { RuntimeEndpoint = runtimeEndpoint }; } else { runtimeClient = new QnAMakerRuntimeClient(new EndpointKeyServiceClientCredentials(endpointKey)) { RuntimeEndpoint = runtimeEndpoint }; } // </RuntimeAuthorization> Console.WriteLine("Creating KB..."); var kbId = CreateSampleKb(authoringClient).Result; Console.WriteLine("Created KB with ID " + kbId + "."); Console.WriteLine("Downloading KB..."); DownloadKb(authoringClient, kbId).Wait(); Console.WriteLine("Updating KB..."); UpdateKB(authoringClient, kbId).Wait(); Console.WriteLine("Publishing KB..."); PublishKb(authoringClient, kbId).Wait(); Console.WriteLine("Querying KB..."); GenerateAnswer(runtimeClient, kbId).Wait(); Console.WriteLine("Deleting KB..."); DeleteKB(authoringClient, kbId).Wait(); }
public FunctionAppBastardBrain(IConfiguration config) { _settings = new SystemSettings(config); _context = new BastardDBContext(config.GetConnectionString("DefaultConnection")); _qnAMakerClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(_settings.QnASubscriptionKey)) { Endpoint = _settings.QnAMakerHost }; }
private static IQnAMakerClient GetQnAMakerClient(string qnaMakerSubscriptionKey, string endpointHost) { IQnAMakerClient client = new QnAMakerClient(new ApiKeyServiceClientCredentials(qnaMakerSubscriptionKey)) { Endpoint = endpointHost }; return(client); }
public DIBastardBrain(IServiceProvider scopeFactory) { _serviceScope = scopeFactory.CreateScope(); _settings = _serviceScope.ServiceProvider.GetService <SystemSettings>() ?? throw new ArgumentNullException(nameof(_settings)); _qnAMakerClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(_settings.QnASubscriptionKey)) { Endpoint = _settings.QnAMakerHost }; }
private async Task <DialogTurnResult> SummaryStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) { if ((bool)stepContext.Result) { await stepContext.Context.SendActivityAsync(MessageFactory.Text("Here are the details you provided."), cancellationToken); await stepContext.Context.SendActivityAsync(MessageFactory.Text("Python programming questions - "), cancellationToken); for (int i = 0; i < QnAData.QuestionPhrase.Count; i++) { await stepContext.Context.SendActivityAsync(MessageFactory.Text(QnAData.QuestionPhrase[i]), cancellationToken); } await stepContext.Context.SendActivityAsync(MessageFactory.Text("Your programming Answer - "), cancellationToken); await stepContext.Context.SendActivityAsync(MessageFactory.Text((string)stepContext.Values["Answer"]), cancellationToken); await stepContext.Context.SendActivityAsync(MessageFactory.Text("Please wait while I update my Knowledge Base."), cancellationToken); await stepContext.Context.SendActivitiesAsync( new Activity[] { new Activity { Type = ActivityTypes.Typing }, new Activity { Type = "delay", Value = 5000 }, }, cancellationToken); var authoringURL = $"https://{Configuration["ResourceName"]}.cognitiveservices.azure.com"; // <AuthorizationAuthor> var client = new QnAMakerClient(new ApiKeyServiceClientCredentials(Configuration["Key"])) { Endpoint = authoringURL }; // </AuthorizationAuthor> QnAClient.UpdateKB(client, Configuration["KnowledgeBaseId"], (string)stepContext.Values["Answer"]).Wait(); QnAClient.PublishKb(client, Configuration["KnowledgeBaseId"]).Wait(); await stepContext.Context.SendActivityAsync(MessageFactory.Text("I have update my Knowledge Base. Thank you for your contribution 😇")); return(await stepContext.EndDialogAsync(null, cancellationToken)); } else { await stepContext.Context.SendActivityAsync(MessageFactory.Text("Request Not Confirmed.")); return(await stepContext.EndDialogAsync(cancellationToken : cancellationToken)); } }
// <Main> static void Main(string[] args) { // <Resourcevariables> var authoringKey = "REPLACE-WITH-YOUR-QNA-MAKER-KEY"; var resourceName = "REPLACE-WITH-YOUR-RESOURCE-NAME"; var authoringURL = $"https://{resourceName}.cognitiveservices.azure.com"; var queryingURL = $"https://{resourceName}.azurewebsites.net"; // </Resourcevariables> // <TryPreview> // To be set to 'true' to use QnAMakerV2 Public Preview resources // Use the package Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker version 2.1.0-preview.1 var tryPreview = false; // </TryPreview> // <AuthorizationAuthor> var client = new QnAMakerClient(new ApiKeyServiceClientCredentials(authoringKey), tryPreview) { Endpoint = authoringURL }; // </AuthorizationAuthor> var kbId = CreateSampleKb(client).Result; UpdateKB(client, kbId).Wait(); PublishKb(client, kbId).Wait(); DownloadKb(client, kbId).Wait(); var primaryQueryEndpointKey = GetQueryEndpointKey(client).Result; // <AuthorizationQuery> QnAMakerRuntimeClient runtimeClient; if (tryPreview) { runtimeClient = new QnAMakerRuntimeClient(new ApiKeyServiceClientCredentials(authoringKey), tryPreview) { RuntimeEndpoint = authoringURL }; } else { runtimeClient = new QnAMakerRuntimeClient(new EndpointKeyServiceClientCredentials(primaryQueryEndpointKey)) { RuntimeEndpoint = queryingURL }; } // </AuthorizationQuery> GenerateAnswer(runtimeClient, kbId).Wait(); DeleteKB(client, kbId).Wait(); }
static async Task Run() { var subscriptionKey = Environment.GetEnvironmentVariable("QNAMAKER_SUBSCRIPTION_KEY"); var client = new QnAMakerClient(new ApiKeyServiceClientCredentials(subscriptionKey)) { Endpoint = "https://westus.api.cognitive.microsoft.com" }; // Create a KB Console.WriteLine("Creating KB..."); var kbId = await CreateSampleKb(client); Console.WriteLine("Created KB with ID : {0}", kbId); // Update the KB Console.WriteLine("Updating KB..."); await UpdateKB(client, kbId); Console.WriteLine("KB Updated."); // Publish the KB Console.Write("Publishing KB..."); await client.Knowledgebase.PublishAsync(kbId); Console.WriteLine("KB Published."); // Download the KB Console.Write("Downloading KB..."); /* * QnAMaker API - Download a knowledgebase */ var kbData = await client.Knowledgebase.DownloadAsync(kbId, EnvironmentType.Prod); // END - Download a knowledgebase Console.WriteLine("KB Downloaded. It has {0} QnAs.", kbData.QnaDocuments.Count); // Delete the KB Console.Write("Deleting KB..."); /* * QnAMaker API - Delete a knowledgebase */ await client.Knowledgebase.DeleteAsync(kbId); // END - Delete a knowledgebase Console.WriteLine("KB Deleted."); }
/// <summary> /// Application startup configuration. /// </summary> /// <param name="builder">webjobs builder.</param> public void Configure(IWebJobsBuilder builder) { builder.Services.AddSingleton <IConfigurationStorageProvider, ConfigurationStorageProvider>(); IQnAMakerClient qnaMakerClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(Environment.GetEnvironmentVariable("QnAMakerSubscriptionKey"))) { Endpoint = Environment.GetEnvironmentVariable("QnAMakerApiUrl") }; builder.Services.AddSingleton <IQnaServiceProvider>((provider) => new QnaServiceProvider( provider.GetRequiredService <IConfigurationStorageProvider>(), qnaMakerClient)); builder.Services.AddSingleton <ISearchServiceDataProvider, SearchServiceDataProvider>(); builder.Services.AddSingleton <ISearchService, SearchService>(); }
/// <summary> /// Application startup configuration. /// </summary> /// <param name="builder">Webjobs builder.</param> public void Configure(IWebJobsBuilder builder) { IQnAMakerClient qnaMakerClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(Environment.GetEnvironmentVariable("QnAMakerSubscriptionKey"))) { Endpoint = Environment.GetEnvironmentVariable("QnAMakerApiUrl") }; builder.Services.AddSingleton <IQnaServiceProvider>((provider) => new QnaServiceProvider( provider.GetRequiredService <IConfigurationDataProvider>(), provider.GetRequiredService <IOptionsMonitor <QnAMakerSettings> >(), qnaMakerClient)); builder.Services.AddSingleton <IConfigurationDataProvider, Common.Providers.ConfigurationDataProvider>(); builder.Services.AddSingleton <ISearchServiceDataProvider>((provider) => new SearchServiceDataProvider(provider.GetRequiredService <IQnaServiceProvider>(), Environment.GetEnvironmentVariable("StorageConnectionString"))); builder.Services.AddSingleton <IConfigurationDataProvider>(new Common.Providers.ConfigurationDataProvider(Environment.GetEnvironmentVariable("StorageConnectionString"))); builder.Services.AddSingleton <IKnowledgeBaseSearchService, KnowledgeBaseSearchService>(); builder.Services.AddSingleton <IKnowledgeBaseSearchService>((provider) => new KnowledgeBaseSearchService(Environment.GetEnvironmentVariable("SearchServiceName"), Environment.GetEnvironmentVariable("SearchServiceQueryApiKey"), Environment.GetEnvironmentVariable("SearchServiceAdminApiKey"), Environment.GetEnvironmentVariable("StorageConnectionString"))); }
public static async Task <String> GetAnswer(string question) { var client = new QnAMakerClient(new ApiKeyServiceClientCredentials(authoringKey)) { Endpoint = authoringURL }; var primaryQueryEndpointKey = await GetQueryEndpointKey(client); var runtimeClient = new QnAMakerRuntimeClient(new EndpointKeyServiceClientCredentials(primaryQueryEndpointKey)) { RuntimeEndpoint = queryingURL }; var answer = await GenerateAnswer(runtimeClient, question); return(answer); }
private static async Task <string> CreateKB(ILogger log) { var qnaClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(GetAppSetting("QnAAuthoringKey"))) { Endpoint = $"https://{GetAppSetting("QnAServiceName")}.cognitiveservices.azure.com" }; var createKbDTO = new CreateKbDTO { Name = "search", Language = "English" }; var operation = await qnaClient.Knowledgebase.CreateAsync(createKbDTO); operation = await MonitorOperation(qnaClient, operation, log); var kbId = operation.ResourceLocation.Replace("/knowledgebases/", string.Empty); log.LogInformation("init-kb: Created KB " + kbId); return(kbId); }
// <Main> static void Main(string[] args) { // <Authorization> var subscriptionKey = Environment.GetEnvironmentVariable("QNAMAKER_SUBSCRIPTION_KEY"); var client = new QnAMakerClient(new ApiKeyServiceClientCredentials(subscriptionKey)) { Endpoint = "https://westus.api.cognitive.microsoft.com" }; // </Authorization> // Create a KB Console.WriteLine("Creating KB..."); var kbId = CreateSampleKb(client).Result; Console.WriteLine("Created KB with ID : {0}", kbId); // Update the KB Console.WriteLine("Updating KB..."); UpdateKB(client, kbId).Wait(); Console.WriteLine("KB Updated."); // <PublishKB> Console.Write("Publishing KB..."); client.Knowledgebase.PublishAsync(kbId).Wait(); Console.WriteLine("KB Published."); // </PublishKB> // <DownloadKB> Console.Write("Downloading KB..."); var kbData = client.Knowledgebase.DownloadAsync(kbId, EnvironmentType.Prod).Result; Console.WriteLine("KB Downloaded. It has {0} QnAs.", kbData.QnaDocuments.Count); // </DownloadKB> // <DeleteKB> Console.Write("Deleting KB..."); client.Knowledgebase.DeleteAsync(kbId).Wait(); Console.WriteLine("KB Deleted."); // </DeleteKB> }
private IEnumerable <IQnaServiceProvider> GetQnaServiceProviders(IServiceProvider provider, List <LanguageQnAMakerKeyCombination> languageQnAMakerKeyCombinations) { var qnaServiceProviders = new List <QnaServiceProvider>(); foreach (var languageQnAMakerKeyCombination in languageQnAMakerKeyCombinations) { IQnAMakerClient qnaMakerClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(languageQnAMakerKeyCombination.QnAMakerSubscriptionKey)) { Endpoint = Environment.GetEnvironmentVariable("QnAMakerApiUrl") }; qnaServiceProviders.Add(new QnaServiceProvider( languageQnAMakerKeyCombination.LanguageCode, provider.GetRequiredService <IConfigurationDataProvider>(), provider.GetRequiredService <IOptionsMonitor <QnAMakerSettings> >(), qnaMakerClient)); } return(qnaServiceProviders.AsEnumerable()); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string sourceFile = req.Query["sourceFile"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); sourceFile = sourceFile ?? data?.sourceFile; var authoringKey = GetEnvironmentVariable("authoringKey"); var authoringUrl = GetEnvironmentVariable("authoringUrl"); var kbId = GetEnvironmentVariable("kbId"); var client = new QnAMakerClient(new ApiKeyServiceClientCredentials(authoringKey)) { Endpoint = authoringUrl }; //Delete Source File List <string> toDelete = new List <string>(); toDelete.Add(sourceFile); var updateDelete = await client.Knowledgebase.UpdateAsync(kbId, new UpdateKbOperationDTO { Add = null, Update = null, Delete = new UpdateKbOperationDTODelete(null, toDelete) }); updateDelete = await MonitorOperation(client, updateDelete); string responseMessage = "This HTTP triggered function executed successfully."; return(new OkObjectResult(responseMessage)); }
// <Main> static void Main(string[] args) { // <Resourcevariables> var authoringKey = "PASTE_YOUR_QNA_MAKER_MANAGED_SUBSCRIPTION_KEY_HERE"; var authoringURL = "PASTE_YOUR_QNA_MAKER_MANAGED_ENDPOINT_HERE"; // </Resourcevariables> // <AuthorizationAuthor> var client = new QnAMakerClient(new ApiKeyServiceClientCredentials(authoringKey)) { Endpoint = authoringURL }; // </AuthorizationAuthor> var kbId = CreateSampleKb(client).Result; UpdateKB(client, kbId).Wait(); PublishKb(client, kbId).Wait(); DownloadKb(client, kbId).Wait(); GenerateAnswer(client, kbId).Wait(); DeleteKB(client, kbId).Wait(); }
/// <summary> /// This method gets called by the runtime. Use this method to add services to the container. /// </summary> /// <param name="services"> Service Collection Interface.</param> public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddSingleton <ICredentialProvider, ConfigurationCredentialProvider>(); services.AddSingleton <IBotFrameworkHttpAdapter, BotFrameworkHttpAdapter>(); services.AddSingleton(new MicrosoftAppCredentials(this.Configuration["MicrosoftAppId"], this.Configuration["MicrosoftAppPassword"])); services.AddSingleton <IConfigurationStorageProvider>(new ConfigurationStorageProvider(this.Configuration)); services.AddSingleton <IObjectIdToNameMapper>(new ObjectIdToNameMapper(this.Configuration)); IQnAMakerClient qnaMakerClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(this.Configuration["QnAMakerSubscriptionKey"])) { Endpoint = this.Configuration["QnAMakerApiUrl"] }; string endpointKey = Task.Run(() => qnaMakerClient.EndpointKeys.GetKeysAsync()).Result.PrimaryEndpointKey; services.AddSingleton <IQnaServiceProvider>((provider) => new QnaServiceProvider( provider.GetRequiredService <IConfigurationStorageProvider>(), this.Configuration, qnaMakerClient, new QnAMakerRuntimeClient(new EndpointKeyServiceClientCredentials(endpointKey)) { RuntimeEndpoint = this.Configuration["QnAMakerHostUrl"] })); services.AddSingleton <ISearchService, SearchService>(); services.AddSingleton <CrowdSourcerCards>(); services.AddTransient <IBot>((provider) => new CrowdSourcerBot( provider.GetRequiredService <TelemetryClient>(), provider.GetRequiredService <IQnaServiceProvider>(), this.Configuration, provider.GetRequiredService <IConfigurationStorageProvider>(), provider.GetRequiredService <IObjectIdToNameMapper>(), provider.GetRequiredService <ISearchService>(), provider.GetRequiredService <CrowdSourcerCards>())); services.AddApplicationInsightsTelemetry(); services.AddLocalization(); }
public static async Task Run(string key, string endpoint) { var client = new QnAMakerClient(new ApiKeyServiceClientCredentials(key)) { Endpoint = endpoint }; // Create a KB Console.WriteLine("Creating KB..."); var kbId = await CreateSampleKb(client); Console.WriteLine("Created KB with ID : {0}", kbId); // Update the KB Console.WriteLine("Updating KB..."); await UpdateKB(client, kbId); Console.WriteLine("KB Updated."); // Publish the KB Console.Write("Publishing KB..."); await client.Knowledgebase.PublishAsync(kbId); Console.WriteLine("KB Published."); // Download the KB Console.Write("Downloading KB..."); var kbData = await client.Knowledgebase.DownloadAsync(kbId, EnvironmentType.Prod); Console.WriteLine("KB Downloaded. It has {0} QnAs.", kbData.QnaDocuments.Count); // Delete the KB Console.Write("Deleting KB..."); await client.Knowledgebase.DeleteAsync(kbId); Console.WriteLine("KB Deleted."); }
// <Main> static void Main(string[] args) { // <AuthoringAuthorization> string subscriptionKey = Environment.GetEnvironmentVariable("QNA_MAKER_SUBSCRIPTION_KEY"); string endpoint = Environment.GetEnvironmentVariable("QNA_MAKER_ENDPOINT"); var authoringClient = new QnAMakerClient(new ApiKeyServiceClientCredentials(subscriptionKey)) { Endpoint = endpoint }; // </AuthoringAuthorization> // <RuntimeAuthorization> string runtimeEndpoint = Environment.GetEnvironmentVariable("QNA_MAKER_RUNTIME_ENDPOINT"); string endpointKey = GetQueryEndpointKey(authoringClient).Result; var runtimeClient = new QnAMakerRuntimeClient(new EndpointKeyServiceClientCredentials(endpointKey)) { RuntimeEndpoint = runtimeEndpoint }; // </RuntimeAuthorization> Console.WriteLine("Creating KB..."); var kbId = CreateSampleKb(authoringClient).Result; Console.WriteLine("Created KB with ID " + kbId + "."); Console.WriteLine("Downloading KB..."); DownloadKb(authoringClient, kbId).Wait(); Console.WriteLine("Updating KB..."); UpdateKB(authoringClient, kbId).Wait(); Console.WriteLine("Publishing KB..."); PublishKb(authoringClient, kbId).Wait(); Console.WriteLine("Querying KB..."); GenerateAnswer(runtimeClient, kbId).Wait(); Console.WriteLine("Deleting KB..."); DeleteKB(authoringClient, kbId).Wait(); }