Ejemplo n.º 1
0
        async Task <ClassificationResponse[]> CallLuisClassifier(ClassifierConfigSection info, string message)
        {
            LuisService classifier = new LuisService(new UrlConfig()
            {
                Url = info.Url, Key = info.Key
            }, null);
            var luisResponse = await classifier.Parse(message, 0);

            if (luisResponse == null)
            {
                return new ClassificationResponse[] { }
            }
            ;

            // TODO: Need to figure out how to handle WrongIntent. See AVA-3345 for info.
            // Normally commonchat always overrides the flow.
            // But for WrongIntent, it doesn't always make sense, and we want the flow to use its error handler.

            return((from c in luisResponse.Intents
                    where c.Intent != "WrongIntent"
                    orderby c.Score descending
                    select new ClassificationResponse
            {
                Intent = $"{info.Id}-{c.Intent}",
                Result = new Dictionary <string, object> {
                    { "intent", $"{info.Id}-{c.Intent}" }, { "selected_system", info.Id }, { "score", c.Score }
                },
                Probability = c.Score,
                Source = "luis",
                RawResponse = JsonConvert.SerializeObject(luisResponse)
            })
                   .Take(1).ToArray());
        }
Ejemplo n.º 2
0
        async Task <ClassificationResponse[]> CallClassifier(ClassifierConfigSection info, string message)
        {
            var scoreRequest = new
            {
                Inputs = new Dictionary <string, StringTable>()
                {
                    {
                        "input1",
                        new StringTable()
                        {
                            ColumnNames = new string[] { "category", "text" },
                            Values      = new string[, ] {
                                { "value", message }
                            }
                        }
                    },
                },
                GlobalParameters = new Dictionary <string, string>()
                {
                }
            };

            RestApiService restApiService = new RestApiService(info.Url, info.Key);
            string         result         = await restApiService.CallRestApi(null, JsonConvert.SerializeObject(scoreRequest), "application/json");

            if (result == null)
            {
                logger.ErrorFormat("Network: Failed to call classifier service. {0}", info.Url);

                return(new ClassificationResponse[] { });
            }

            var answer = JsonConvert.DeserializeObject <dynamic>(result);

            Dictionary <string, object>   results         = ConvertTableToDictionary(answer.Results.output1.value);
            List <ClassificationResponse> classifications = new List <ClassificationResponse>();

            foreach (var key in results.Keys)
            {
                var match = Regex.Match(key, "Scored Probabilities for Class \"(.*)\"");
                if (match.Success)
                {
                    double probability = (double)results[key];
                    classifications.Add(new ClassificationResponse
                    {
                        Intent = match.Groups[1].Value,
                        Result = new Dictionary <string, object> {
                            { "intent", match.Groups[1].Value }, { "selected_system", info.Id }, { "score", probability }
                        },
                        Probability = probability,
                        Source      = "azureml",
                        SourceData  = info.Id,
                        RawResponse = result //answer
                    });
                }
            }

            return(classifications.OrderByDescending(c => c.Probability).Take(2).ToArray());
        }