Beispiel #1
0
 /// <summary>
 /// Imports an application to LUIS, the application's structure should be
 /// included in in the request body.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='luisApp'>
 /// A LUIS application structure.
 /// </param>
 /// <param name='appName'>
 /// The application name to create. If not specified, the application name will
 /// be read from the imported object.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <System.Guid> ImportAsync(this IApps operations, LuisApp luisApp, string appName = default(string), CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.ImportWithHttpMessagesAsync(luisApp, appName, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LuisNLUTrainClient"/> class.
 /// </summary>
 /// <param name="luisConfiguration">LUIS configuration.</param>
 /// <param name="luisTemplate">LUIS app template.</param>
 /// <param name="luisClient">LUIS client.</param>
 public LuisNLUTrainClient(ILuisConfiguration luisConfiguration, LuisApp luisTemplate, ILuisTrainClient luisClient)
 {
     this.LuisConfiguration = luisConfiguration ?? throw new ArgumentNullException(nameof(luisConfiguration));
     this.LuisTemplate      = luisTemplate ?? throw new ArgumentNullException(nameof(luisTemplate));
     this.LuisClient        = luisClient ?? throw new ArgumentNullException(nameof(luisClient));
     this.LuisAppId         = luisConfiguration.AppId;
     this.LuisAppCreated    = luisConfiguration.AppCreated;
 }
Beispiel #3
0
        public async Task <ServiceResponse> PublishLuis(LuisApp App)
        {
            dynamic bodyobj = new ExpandoObject();

            bodyobj.versionId = App.versionId;
            bodyobj.isStaging = false;
            bodyobj.region    = Constants.LUISAppRegion;
            var body = JsonConvert.SerializeObject(bodyobj);

            return(GetServiceResponse(await GetResponse(body, string.Format(Constants.LUISPublishURL, App.appId), Constants.RequestType.post.ToString())));
        }
Beispiel #4
0
        private async Task VerifyStatusTraining(LuisProgClient client, LuisApp app)
        {
            IEnumerable <Training> trainingList;

            for (int attempts = 0; attempts < 3; attempts++)
            {
                trainingList = await client.GetTrainingStatusListAsync(app.Id, app.Endpoints.Production.VersionId);

                if (trainingList.All(x => x.Details.Status.Equals("Success")))
                {
                    return;
                }
            }
        }
Beispiel #5
0
        private static bool TryGetEntityTypeForRole(this LuisApp luisApp, string role, out string entityType)
        {
            // Confirm no entity type has given name
            if (luisApp.Entities.Any(e => e.Name == role) ||
                luisApp.PrebuiltEntities.Any(e => e.Name == role) ||
                luisApp.RegexEntities.Any(e => e.Name == role) ||
                luisApp.PatternAnyEntities.Any(e => e.Name == role))
            {
                entityType = null;
                return(false);
            }

            // Search for any entity type with given name as role
            var entity = luisApp.Entities.FirstOrDefault(e => e.Roles?.Contains(role) ?? false);

            if (entity != null)
            {
                entityType = entity.Name;
                return(true);
            }

            var prebuiltEntity = luisApp.PrebuiltEntities.FirstOrDefault(e => e.Roles?.Contains(role) ?? false);

            if (prebuiltEntity != null)
            {
                entityType = prebuiltEntity.Name;
                return(true);
            }

            var regexEntity = luisApp.RegexEntities.FirstOrDefault(e => e.Roles?.Contains(role) ?? false);

            if (regexEntity != null)
            {
                entityType = regexEntity.Name;
                return(true);
            }

            var patternAnyEntity = luisApp.PatternAnyEntities.FirstOrDefault(e => e.Roles?.Contains(role) ?? false);

            if (patternAnyEntity != null)
            {
                entityType = patternAnyEntity.Name;
                return(true);
            }

            entityType = null;
            return(false);
        }
        public override void Display()
        {
            base.Display();

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

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

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

            var path = Input.ReadString("Enter the path to the file to import: ").Trim();

            LuisApp app = null;

            try
            {
                var import = File.ReadAllText(path);
                app = JsonConvert.DeserializeObject <LuisApp>(import);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error reading the import file. Err: {ex.Message}");
            }

            if (app != null)
            {
                Console.WriteLine("Importing app...");

                try
                {
                    var result = AwaitTask(Client.Apps.ImportAsync(app, appName));
                    Console.WriteLine("Your app has been imported.");
                    Print(result);
                }
                catch (Exception ex)
                {
                    var message = (ex as ErrorResponseException)?.Body.Message ?? ex.Message;
                    Console.WriteLine($"Error importing the application. Err: {message}");
                }
            }

            WaitForGoBack();
        }
Beispiel #7
0
        public static async Task DoesNotOverwriteTemplateIntents()
        {
            var role        = Guid.NewGuid().ToString();
            var intentName  = Guid.NewGuid().ToString();
            var appTemplate = new LuisApp
            {
                ClosedLists = new List <ClosedList>(),
                Entities    = new List <HierarchicalModel>(),
                Intents     = new List <HierarchicalModel>
                {
                    new HierarchicalModel
                    {
                        Name  = intentName,
                        Roles = new List <string> {
                            role
                        },
                    },
                },
                ModelFeatures    = new List <JSONModelFeature>(),
                PrebuiltEntities = new List <PrebuiltEntity>(),
            };

            var mockClient = new MockLuisClient();
            var builder    = GetTestLuisBuilder();

            builder.LuisSettings = new LuisSettings(appTemplate);
            builder.LuisClient   = mockClient;
            using (var luis = builder.Build())
            {
                var utterance = new Models.LabeledUtterance(null, intentName, null);
                await luis.TrainAsync(new[] { utterance }).ConfigureAwait(false);

                // Ensure LUIS app intent still has role
                var importRequest = mockClient.Requests.FirstOrDefault(request => request.Method == nameof(ILuisClient.ImportVersionAsync));
                importRequest.Should().NotBeNull();
                var luisApp = importRequest.Arguments[2].As <LuisApp>();
                luisApp.Intents.Should().Contain(intent => intent.Name == intentName);
                luisApp.Intents.First(intent => intent.Name == intentName).Roles.Count.Should().Be(1);
                luisApp.Intents.First(intent => intent.Name == intentName).Roles.First().Should().Be(role);
            }
        }
Beispiel #8
0
        private LuisApp CreateLuisAppTemplate()
        {
            var defaultTemplate = new LuisApp(
                name: this.LuisConfiguration.AppName,
                versionId: this.LuisConfiguration.VersionId,
                desc: string.Empty,
                culture: "en-us",
                entities: new List <HierarchicalModel>(),
                closedLists: new List <ClosedList>(),
                composites: new List <HierarchicalModel>(),
                patternAnyEntities: new List <PatternAny>(),
                regexEntities: new List <RegexEntity>(),
                prebuiltEntities: new List <PrebuiltEntity>(),
                regexFeatures: new List <JSONRegexFeature>(),
                modelFeatures: new List <JSONModelFeature>(),
                patterns: new List <PatternRule>());

            var templateJson = JObject.FromObject(defaultTemplate);

            templateJson.Merge(JObject.FromObject(this.LuisTemplate));
            return(templateJson.ToObject <LuisApp>());
        }
Beispiel #9
0
 public Task ImportVersionAsync(string appId, string versionId, LuisApp luisApp, CancellationToken cancellationToken)
 {
     return(this.AuthoringClient.Versions.ImportAsync(Guid.Parse(appId), luisApp, versionId, cancellationToken));
 }
Beispiel #10
0
 /// <summary>
 /// Imports a new version into a LUIS application.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='appId'>
 /// The application ID.
 /// </param>
 /// <param name='luisApp'>
 /// A LUIS application structure.
 /// </param>
 /// <param name='versionId'>
 /// The new versionId to import. If not specified, the versionId will be read
 /// from the imported object.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <string> ImportAsync(this IVersions operations, System.Guid appId, LuisApp luisApp, string versionId = default(string), CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.ImportWithHttpMessagesAsync(appId, luisApp, versionId, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Beispiel #11
0
 public Task ImportVersionAsync(string appId, string versionId, LuisApp luisApp, CancellationToken cancellationToken)
 {
     return(this.ProcessRequestAsync(appId, versionId, luisApp));
 }
Beispiel #12
0
 public LuisSettings(LuisApp appTemplate, IReadOnlyDictionary <string, string> prebuiltEntityTypes)
 {
     this.AppTemplate         = appTemplate ?? new LuisApp();
     this.PrebuiltEntityTypes = prebuiltEntityTypes ?? new Dictionary <string, string>();
 }
Beispiel #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LuisSettings"/> class.
 /// </summary>
 /// <param name="appTemplate">App template.</param>
 public LuisSettings(LuisApp appTemplate)
     : this(appTemplate, null)
 {
 }
Beispiel #14
0
        private static async Task <bool> WaitUntilTrainCompletes(ILUISServices luisservice, IDataService dataservice, LuisApp app)
        {
            Thread.Sleep(10000);
            var response = await luisservice.GetTrainLuisStatus(app);

            var     converter        = new ExpandoObjectConverter();
            dynamic results          = JsonConvert.DeserializeObject <IEnumerable <dynamic> >(response.ResponseMessage, converter);
            bool    trainingComplete = true;

            foreach (dynamic result in results)
            {
                var status = result.details.status.ToString();
                switch (status)
                {
                case "InProgress":
                    Console.WriteLine($"ModelId:{result.modelId}. Status:{result.details.statusId}-{status}. ExampleCount: {result.details.exampleCount}");
                    break;

                case "Fail":
                    Console.WriteLine($"ModelId:{result.modelId}. Status:{result.details.statusId}-{status}. Failure reason: {result.details.failureReason} ExampleCount: {result.exampleCount}");
                    break;

                case "UpToDate":
                    Console.WriteLine($"ModelId:{result.modelId}. Status:{result.details.statusId}-{status}. ExampleCount: {result.details.exampleCount}. Training TimeDate: {result.details.trainingDateTime}");
                    break;

                case "Success":
                    Console.WriteLine($"ModelId:{result.modelId}. Status:{result.details.statusId}-{status}. ExampleCount: {result.details.exampleCount}. Training TimeDate: {result.details.trainingDateTime}");
                    break;
                }
                if (status == "InProgress" || status == "Fail")
                {
                    trainingComplete = false;
                    break;
                }
            }
            if (trainingComplete)
            {
                Console.WriteLine("Training completed");
            }
            else
            {
                Console.WriteLine($"Error found. Training could not be completed. Please check if all the intents have utterances");
                // Uncomment the below method to re run the same method again until it finishes
                //await WaitUntilTrainCompletes(luisservice, dataservice, app);
            }
            return(trainingComplete);
        }
Beispiel #15
0
        public static JSONUtterance ToJSONUtterance(this ILabeledUtterance utterance, LuisApp luisApp)
        {
            JSONEntity toJSONEntity(IEntity entity)
            {
                var startPos = entity.StartCharIndexInText(utterance.Text);
                var endPos   = startPos + entity.MatchText.Length - 1;

                if (luisApp.TryGetEntityTypeForRole(entity.EntityType, out var entityType))
                {
                    return(new JSONEntityWithRole(startPos, endPos, entityType, entity.EntityType));
                }

                return(new JSONEntity(startPos, endPos, entity.EntityType));
            }

            return(new JSONUtterance(
                       utterance.Text,
                       utterance.Intent,
                       utterance.Entities?.Select(toJSONEntity).ToArray() ?? Array.Empty <JSONEntity>()));
        }
Beispiel #16
0
        public async Task <ServiceResponse> CreateLuisApp(LuisApp App)
        {
            var body = JsonConvert.SerializeObject(App);

            return(GetServiceResponse(await GetResponse(body, Constants.LUISAppURL, Constants.RequestType.post.ToString())));
        }
Beispiel #17
0
 public async Task <ServiceResponse> GetTrainLuisStatus(LuisApp App)
 {
     return(GetServiceResponse(await GetResponse(string.Format(Constants.LUISTrainStatusURL, App.appId, App.versionId), Constants.RequestType.post.ToString())));
 }