Example #1
0
        /* Create a new LUIS application or get an existing one, depending on whether app_id
         * is specified. Return the application ID and version.
         */
        async static Task <ApplicationInfo> GetOrCreateApplication(LUISAuthoringClient client)
        {
            if (String.IsNullOrEmpty(app_id))
            {
                string app_version = "0.1";
                var    app_info    = new ApplicationCreateObject()
                {
                    Name             = String.Format("Contoso {0}", DateTime.Now),
                    InitialVersionId = app_version,
                    Description      = "Flight booking app built with LUIS .NET SDK.",
                    Culture          = "en-us"
                };
                var app_id = await client.Apps.AddAsync(app_info);

                Console.WriteLine("Created new LUIS application {0}\n with ID {1}", app_info.Name, app_id);
                Console.WriteLine("Make a note of this ID to use this application with other samples.\n");
                return(new ApplicationInfo()
                {
                    ID = app_id, Version = app_version
                });
            }
            else
            {
                var app_info = await client.Apps.GetAsync(new Guid(app_id));

                var app_version = app_info.ActiveVersion;
                Console.WriteLine("Using existing LUIS application", app_info.Name, app_version);
                return(new ApplicationInfo()
                {
                    ID = new Guid(app_id), Version = app_version
                });
            }
        }
Example #2
0
        public override void Display()
        {
            base.Display();

            var defaultAppName = $"Contoso-{DateTime.UtcNow.Ticks}";
            var versionId      = "0.1";

            var appName = Input.ReadString($"Enter your App's name: (default {defaultAppName})");

            if (string.IsNullOrWhiteSpace(appName))
            {
                appName = defaultAppName;
            }

            var newApp = new ApplicationCreateObject
            {
                Name             = appName,
                InitialVersionId = versionId,
                Description      = "New App created with LUIS SDK wizard",
                Culture          = "en-us",
                Domain           = "",
                UsageScenario    = ""
            };

            var appId = AwaitTask(Client.Apps.AddAsync(newApp));

            Console.WriteLine($"{appName} app created with the id {appId}");

            NavigateWithInitializer <T>((page) => {
                page.AppId     = appId;
                page.VersionId = versionId;
            });
        }
Example #3
0
 /// <summary>
 /// Creates a new LUIS app.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='applicationCreateObject'>
 /// A model containing Name, Description (optional), Culture, Usage Scenario
 /// (optional), Domain (optional) and initial version ID (optional) of the
 /// application. Default value for the version ID is 0.1. Note: the culture
 /// cannot be changed after the app is created.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <System.Guid> AddAsync(this IApps operations, ApplicationCreateObject applicationCreateObject, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.AddWithHttpMessagesAsync(applicationCreateObject, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Example #4
0
        static Guid CreateApp(LUISAuthoringClient client, string resourceRegion, string appName, string versionId, string culture, string description)
        {
            try
            {
                // don't set because it isn't used by LUIS
                var domain        = String.Empty;
                var usageScenario = String.Empty;

                // create app definition object
                var app = new ApplicationCreateObject(culture, appName, domain, description, versionId, usageScenario);

                // create app
                var response = client.Apps.AddWithHttpMessagesAsync(app, null, CancellationToken.None).Result;

                // get appId from `Location` header - trim away URL part to get appId
                var newAppId = response.Response.Headers.Location.ToString().Replace(String.Format("https://{0}.api.cognitive.microsoft.com/luis/api/v2.0/apps/", resourceRegion), "");

                // convert string to guid and return it
                return(new Guid(newAppId));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                throw ex;
            }
        }
Example #5
0
        async static Task <Guid> CreateApplication(LUISAuthoringClient client, string appName, string versionId)
        {
            // <AuthoringCreateApplication>
            var newApp = new ApplicationCreateObject
            {
                Culture          = "en-us",
                Name             = appName,
                InitialVersionId = versionId
            };

            var appId = await client.Apps.AddAsync(newApp);

            // </AuthoringCreateApplication>

            Console.WriteLine("New app ID {0}.", appId);
            return(appId);
        }
Example #6
0
        // Create a new LUIS application. Return the application ID and version.
        async static Task <ApplicationInfo> CreateApplication(LUISAuthoringClient client)
        {
            string app_version = "0.1";
            var    app_info    = new ApplicationCreateObject()
            {
                Name             = String.Format("Contoso {0}", DateTime.Now),
                InitialVersionId = app_version,
                Description      = "Flight booking app built with LUIS .NET SDK.",
                Culture          = "en-us"
            };
            var app_id = await client.Apps.AddAsync(app_info);

            Console.WriteLine("Created new LUIS application {0}\n with ID {1}.", app_info.Name, app_id);
            return(new ApplicationInfo()
            {
                ID = app_id, Version = app_version
            });
        }
Example #7
0
        public async Task <string> CreateAppAsync(string appName, CancellationToken cancellationToken)
        {
            var request = new ApplicationCreateObject
            {
                Name    = appName,
                Culture = "en-us",
            };

            // Creating LUIS app.
            var appId = await this.AuthoringClient.Apps.AddAsync(request, cancellationToken).ConfigureAwait(false);

            // Assign Azure resource to LUIS app.
            if (this.AzureSubscriptionInfo != null)
            {
                await this.AssignAzureResourceAsync(appId).ConfigureAwait(false);
            }

            return(appId.ToString());
        }
        public override void Display()
        {
            base.Display();

            var defaultAppName = $"Contoso-{DateTime.UtcNow.Ticks}";
            var versionId      = "0.1";

            var appName = Input.ReadString($"Enter your App's name: (default {defaultAppName})");

            if (string.IsNullOrWhiteSpace(appName))
            {
                appName = defaultAppName;
            }

            var newApp = new ApplicationCreateObject
            {
                Name             = appName,
                InitialVersionId = versionId,
                Description      = "New App created with LUIS SDK wizard",
                Culture          = "en-us",
                Domain           = "",
                UsageScenario    = ""
            };

            try {
                var appId = AwaitTask(Client.Apps.AddAsync(newApp));
                Console.WriteLine($"{appName} app created with the id {appId}");


                NavigateWithInitializer <T>((page) => {
                    page.AppId     = appId;
                    page.VersionId = versionId;
                });
            }
            catch (Exception)
            {
                Console.WriteLine("Something went wrong. Please enter a different name");
                Input.ReadString("Press any key to continue");
                Program.NavigateTo <CreateAppPage <T> >();
            }
        }
Example #9
0
        // It's always a good idea to access services in an async fashion
        public static async Task Main()
        {
            var authoringEndpoint  = String.Format("https://{0}.cognitiveservices.azure.com/", AUTHORING_RESOURCE_NAME);
            var predictionEndpoint = String.Format("https://{0}.cognitiveservices.azure.com/", PREDICTION_RESOURCE_NAME);

            var appName    = "Contoso Pizza Company";
            var versionId  = "0.1";
            var intentName = "OrderPizza";

            // Authenticate the client
            Console.WriteLine("Authenticating the client...");
            var credentials = new Microsoft.Azure.CognitiveServices.Language.LUIS.Authoring.ApiKeyServiceClientCredentials(AUTHORING_KEY);
            var client      = new LUISAuthoringClient(credentials)
            {
                Endpoint = authoringEndpoint
            };

            // Create a LUIS app
            var newApp = new ApplicationCreateObject
            {
                Culture          = "en-us",
                Name             = appName,
                InitialVersionId = versionId
            };

            Console.WriteLine("Creating a LUIS app...");
            var appId = await client.Apps.AddAsync(newApp);

            // Create intent for the app
            Console.WriteLine("Creating intent for the app...");
            await client.Model.AddIntentAsync(appId, versionId, new ModelCreateObject()
            {
                Name = intentName
            });

            // Create entities for the app
            Console.WriteLine("Creating entities for the app...");

            // Add Prebuilt entity
            await client.Model.AddPrebuiltAsync(appId, versionId, new[] { "number" });

            // Define ml entity with children and grandchildren
            var mlEntityDefinition = new EntityModelCreateObject
            {
                Name     = "Pizza order",
                Children = new[]
                {
                    new ChildEntityModelCreateObject
                    {
                        Name     = "Pizza",
                        Children = new[]
                        {
                            new ChildEntityModelCreateObject {
                                Name = "Quantity"
                            },
                            new ChildEntityModelCreateObject {
                                Name = "Type"
                            },
                            new ChildEntityModelCreateObject {
                                Name = "Size"
                            }
                        }
                    },
                    new ChildEntityModelCreateObject
                    {
                        Name     = "Toppings",
                        Children = new[]
                        {
                            new ChildEntityModelCreateObject {
                                Name = "Type"
                            },
                            new ChildEntityModelCreateObject {
                                Name = "Quantity"
                            }
                        }
                    }
                }
            };

            // Add ML entity
            var mlEntityId = await client.Model.AddEntityAsync(appId, versionId, mlEntityDefinition);;

            // Add phraselist feature
            var phraselistId = await client.Features.AddPhraseListAsync(appId, versionId, new PhraselistCreateObject
            {
                EnabledForAllModels = false,
                IsExchangeable      = true,
                Name    = "QuantityPhraselist",
                Phrases = "few,more,extra"
            });

            // Get entity and subentities
            var model = await client.Model.GetEntityAsync(appId, versionId, mlEntityId);

            var toppingQuantityId = GetModelGrandchild(model, "Toppings", "Quantity");
            var pizzaQuantityId   = GetModelGrandchild(model, "Pizza", "Quantity");

            // add model as feature to subentity model
            await client.Features.AddEntityFeatureAsync(appId, versionId, pizzaQuantityId, new ModelFeatureInformation { ModelName = "number", IsRequired = true });

            await client.Features.AddEntityFeatureAsync(appId, versionId, toppingQuantityId, new ModelFeatureInformation { ModelName = "number" });

            // add phrase list as feature to subentity model
            await client.Features.AddEntityFeatureAsync(appId, versionId, toppingQuantityId, new ModelFeatureInformation { FeatureName = "QuantityPhraselist" });

            // Add example utterance to intent
            Console.WriteLine("Adding example utterance to intent...");

            // Define labeled example
            var labeledExampleUtteranceWithMLEntity = new ExampleLabelObject
            {
                Text         = "I want two small seafood pizzas with extra cheese.",
                IntentName   = intentName,
                EntityLabels = new[]
                {
                    new EntityLabelObject
                    {
                        StartCharIndex = 7,
                        EndCharIndex   = 48,
                        EntityName     = "Pizza order",
                        Children       = new[]
                        {
                            new EntityLabelObject
                            {
                                StartCharIndex = 7,
                                EndCharIndex   = 30,
                                EntityName     = "Pizza",
                                Children       = new[]
                                {
                                    new EntityLabelObject {
                                        StartCharIndex = 7, EndCharIndex = 9, EntityName = "Quantity"
                                    },
                                    new EntityLabelObject {
                                        StartCharIndex = 11, EndCharIndex = 15, EntityName = "Size"
                                    },
                                    new EntityLabelObject {
                                        StartCharIndex = 17, EndCharIndex = 23, EntityName = "Type"
                                    }
                                }
                            },
                            new EntityLabelObject
                            {
                                StartCharIndex = 37,
                                EndCharIndex   = 48,
                                EntityName     = "Toppings",
                                Children       = new[]
                                {
                                    new EntityLabelObject {
                                        StartCharIndex = 37, EndCharIndex = 41, EntityName = "Quantity"
                                    },
                                    new EntityLabelObject {
                                        StartCharIndex = 43, EndCharIndex = 48, EntityName = "Type"
                                    }
                                }
                            }
                        }
                    },
                }
            };

            // Add an example for the entity.
            // Enable nested children to allow using multiple models with the same name.
            // The quantity subentity and the phraselist could have the same exact name if this is set to True
            await client.Examples.AddAsync(appId, versionId, labeledExampleUtteranceWithMLEntity, enableNestedChildren : true);

            // Train the app
            Console.WriteLine("Training the app...");
            await client.Train.TrainVersionAsync(appId, versionId);

            while (true)
            {
                var status = await client.Train.GetStatusAsync(appId, versionId);

                if (status.All(m => m.Details.Status == "Success"))
                {
                    // Assumes that we never fail, and that eventually we'll always succeed.
                    break;
                }
            }

            // Publish app to production slot
            Console.WriteLine("Publishing the app to production slot...");
            await client.Apps.PublishAsync(appId, new ApplicationPublishObject { VersionId = versionId, IsStaging = false });

            // Authenticate the prediction runtime client
            Console.WriteLine("Authenticating the prediction client...");
            credentials = new Microsoft.Azure.CognitiveServices.Language.LUIS.Authoring.ApiKeyServiceClientCredentials(PREDICTION_KEY);
            var runtimeClient = new LUISRuntimeClient(credentials)
            {
                Endpoint = predictionEndpoint
            };

            // Get prediction from runtime
            // Production == slot name
            Console.WriteLine("Getting prediction...");
            var request = new PredictionRequest {
                Query = "I want two small pepperoni pizzas with more salsa"
            };
            var prediction = await runtimeClient.Prediction.GetSlotPredictionAsync(appId, "Production", request);

            Console.Write(JsonConvert.SerializeObject(prediction, Formatting.Indented));
        }