Beispiel #1
0
        public async Task <bool> Train(Agent agent, NlpDoc doc, PipeModel meta)
        {
            var client  = new RestClient(Configuration.GetSection("SpaCyProvider:Url").Value);
            var request = new RestRequest("tokenizer", Method.GET);
            List <List <NlpToken> > tokens = new List <List <NlpToken> >();
            Boolean res    = true;
            var     dc     = new DefaultDataContextLoader().GetDefaultDc();
            var     corpus = agent.Corpus;

            doc.Sentences = new List <NlpDocSentence>();

            corpus.UserSays.ForEach(usersay => {
                Console.WriteLine(usersay.Text);
                request.AddParameter("text", usersay.Text);
                var response = client.Execute <Result>(request);

                tokens.Add(response.Data.Tokens);

                doc.Sentences.Add(new NlpDocSentence
                {
                    Tokens = response.Data.Tokens,
                    Text   = usersay.Text
                });

                res = res && response.IsSuccessful;
            });

            return(res);
        }
Beispiel #2
0
        private void OnTextMessaged(string agentId, WebhookMessage <WebhookTextMessage> message)
        {
            Console.WriteLine($"OnTextMessaged: {message.Message.Text}");

            var ai    = new ApiAi();
            var agent = ai.LoadAgent(agentId);

            ai.AiConfig = new AIConfiguration(agent.ClientAccessToken, SupportedLanguage.English)
            {
                AgentId = agentId
            };
            ai.AiConfig.SessionId = message.Sender.Id;
            var aiResponse = ai.TextRequest(new AIRequest {
                Query = new String[] { message.Message.Text }
            });

            var dc     = new DefaultDataContextLoader().GetDefaultDc();
            var config = dc.Table <AgentIntegration>().FirstOrDefault(x => x.AgentId == agentId && x.Platform == "Facebook Messenger");

            SendTextMessage(config.AccessToken, new WebhookMessage <WebhookTextMessage>
            {
                Recipient = message.Sender.ToObject <WebhookMessageRecipient>(),
                Message   = new WebhookTextMessage
                {
                    Text = String.IsNullOrEmpty(aiResponse.Result.Fulfillment.Speech) ? aiResponse.Result.Action : aiResponse.Result.Fulfillment.Speech
                }
            });
        }
        public IActionResult Token([FromBody] VmUserLogin userModel)
        {
            if (String.IsNullOrEmpty(userModel.UserName) || String.IsNullOrEmpty(userModel.Password))
            {
                return(new BadRequestObjectResult("Username and password should not be empty."));
            }

            var dc = new DefaultDataContextLoader().GetDefaultDc();

            // validate from local
            var user = (from usr in dc.Table <User>()
                        join auth in dc.Table <UserAuth>() on usr.Id equals auth.UserId
                        where usr.Email == userModel.UserName
                        select auth).FirstOrDefault();

            if (user != null)
            {
                // validate password
                string hash = PasswordHelper.Hash(userModel.Password, user.Salt);
                if (user.Password == hash)
                {
                    var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
                    return(Ok(JwtToken.GenerateToken(config, user.UserId)));
                }
                else
                {
                    return(BadRequest("Authorization Failed."));
                }
            }
            else
            {
                return(BadRequest("Account doesn't exist"));
            }
        }
Beispiel #4
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseDefaultFiles();
            app.UseStaticFiles();

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                var info = Configuration.GetSection("Swagger").Get <Info>();

                c.SupportedSubmitMethods(SubmitMethod.Get, SubmitMethod.Post, SubmitMethod.Put, SubmitMethod.Patch, SubmitMethod.Delete);
                c.ShowExtensions();
                c.SwaggerEndpoint(Configuration.GetValue <String>("Swagger:Endpoint"), info.Title);
                c.RoutePrefix   = String.Empty;
                c.DocumentTitle = info.Title;
                c.InjectStylesheet(Configuration.GetValue <String>("Swagger:Stylesheet"));
            });

            app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());

            app.Use(async(context, next) =>
            {
                string token = context.Request.Headers["Authorization"];
                if (!string.IsNullOrWhiteSpace(token) && (token = token.Split(' ').Last()).Length == 32)
                {
                    var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
                    context.Request.Headers["ClientAccessToken"] = token;

                    var dc     = new DefaultDataContextLoader().GetDefaultDc();
                    var userId = dc.Table <Agent>().FirstOrDefault(x => x.ClientAccessToken == token)?.UserId;

                    context.Request.Headers["Authorization"] = "Bearer " + JwtToken.GenerateToken(config, userId);
                }

                await next.Invoke();
            });
            app.UseAuthentication();

            app.UseMvc();

            AppDomain.CurrentDomain.SetData("DataPath", Path.Combine(env.ContentRootPath, "App_Data"));
            AppDomain.CurrentDomain.SetData("Configuration", Configuration);
            AppDomain.CurrentDomain.SetData("ContentRootPath", env.ContentRootPath);
            AppDomain.CurrentDomain.SetData("Assemblies", Configuration.GetValue <String>("Assemblies").Split(','));

            InitializationLoader loader = new InitializationLoader();

            loader.Env    = env;
            loader.Config = Configuration;
            loader.Load();
        }
Beispiel #5
0
        public async Task <bool> Train(Agent agent, NlpDoc doc, PipeModel meta)
        {
            var dc     = new DefaultDataContextLoader().GetDefaultDc();
            var corpus = agent.Corpus;

            meta.Model = "ner-crf.model";

            List <TrainingIntentExpression <TrainingIntentExpressionPart> > userSays = corpus.UserSays;
            List <List <TrainingData> > list = new List <List <TrainingData> >();

            string rawTrainingDataFileName    = Path.Join(Settings.TrainDir, "ner-crf.corpus.txt");
            string parsedTrainingDataFileName = Path.Join(Settings.TrainDir, "ner-crf.parsed.txt");
            string modelFileName = Path.Join(Settings.ModelDir, meta.Model);

            using (FileStream fs = new FileStream(rawTrainingDataFileName, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    for (int i = 0; i < doc.Sentences.Count; i++)
                    {
                        List <TrainingData> curLine = Merge(doc.Sentences[i].Tokens, userSays[i].Entities);
                        curLine.ForEach(trainingData =>
                        {
                            string[] wordParams = { trainingData.Entity, trainingData.Token, trainingData.Pos, trainingData.Chunk };
                            string wordStr      = string.Join(" ", wordParams);
                            sw.Write(wordStr + "\n");
                        });
                        list.Add(curLine);
                        sw.Write("\n");
                    }
                    sw.Flush();
                }
            }

            var fields      = Configuration.GetValue <String>($"CRFsuiteEntityRecognizer:fields");
            var uniFeatures = Configuration.GetValue <String>($"CRFsuiteEntityRecognizer:uniFeatures");
            var biFeatures  = Configuration.GetValue <String>($"CRFsuiteEntityRecognizer:biFeatures");

            new MachineLearning.CRFsuite.Ner()
            .NerStart(rawTrainingDataFileName, parsedTrainingDataFileName, fields, uniFeatures.Split(" "), biFeatures.Split(" "));

            var algorithmDir = Path.Join(AppDomain.CurrentDomain.GetData("ContentRootPath").ToString(), "Algorithms");

            CmdHelper.Run(Path.Join(algorithmDir, "crfsuite"), $"learn -m {modelFileName} {parsedTrainingDataFileName}"); // --split=3 -x
            Console.WriteLine($"Saved model to {modelFileName}");
            meta.Meta                = new JObject();
            meta.Meta["fields"]      = fields;
            meta.Meta["uniFeatures"] = uniFeatures;
            meta.Meta["biFeatures"]  = biFeatures;

            return(true);
        }
Beispiel #6
0
        public AccountCore(Database dc = null)
        {
            if (dc == null)
            {
                dc = new DefaultDataContextLoader().GetDefaultDc();
            }
            else
            {
                _dc = dc;
            }

            _config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
        }
Beispiel #7
0
        public async Task <bool> Train(Agent agent, NlpDoc doc, PipeModel meta)
        {
            var client  = new RestClient(Configuration.GetSection("NltkProvider:Url").Value);
            var request = new RestRequest("nltktokenizesentences", Method.POST);
            List <List <Token> > tokens = new List <List <Token> >();
            Boolean res    = true;
            var     dc     = new DefaultDataContextLoader().GetDefaultDc();
            var     corpus = agent.Corpus;

            doc.Sentences = new List <NlpDocSentence>();
            List <string> sentencesList = new List <string>();

            corpus.UserSays.ForEach(usersay => sentencesList.Add(usersay.Text));

            request.RequestFormat = DataFormat.Json;

            request.AddParameter("application/json", JsonConvert.SerializeObject(new Documents(sentencesList)), ParameterType.RequestBody);

            var response = client.Execute <Result>(request);

            tokens = response.Data.TokensList;

            for (int i = 0; i < sentencesList.Count; i++)
            {
                doc.Sentences.Add(new NlpDocSentence
                {
                    Tokens = tokens[i],
                    Text   = sentencesList[i]
                });
            }
            res = res && response.IsSuccessful;
            return(res);

            /*
             * corpus.UserSays.ForEach(usersay => {
             *  Console.WriteLine(usersay.Text);
             *  request.AddParameter("text", usersay.Text);
             *  var response = client.Execute<Result>(request);
             *
             *  tokens.Add(response.Data.Tokens);
             *
             *  doc.Sentences.Add(new NlpDocSentence
             *  {
             *      Tokens = response.Data.Tokens,
             *      Text = usersay.Text
             *  });
             *
             *  res = res && response.IsSuccessful;
             * });
             */
        }
Beispiel #8
0
        public void Initialize()
        {
            var dc = new DefaultDataContextLoader().GetDefaultDc();

            var instances = TypeHelper.GetInstanceWithInterface <IHookDbInitializer>(Database.Assemblies).OrderBy(x => x.Priority).ToList();

            for (int idx = 0; idx < instances.Count; idx++)
            {
                DateTime start = DateTime.UtcNow;
                Console.WriteLine($"{instances[idx].ToString()} P:{instances[idx].Priority} started at {DateTime.UtcNow}");
                int effected = dc.DbTran(() => instances[idx].Load(Database.Configuration, dc));
                Console.WriteLine($"{instances[idx].ToString()} effected [{effected}] records in {(DateTime.UtcNow - start).TotalMilliseconds} ms");
            }
        }
Beispiel #9
0
        public void Initialize(IConfiguration config, IHostingEnvironment env)
        {
            var dc = new DefaultDataContextLoader().GetDefaultDc();

            // Amend table structure
            var instances = TypeHelper.GetInstanceWithInterface <IHookDbInitializer>("Voicebot.Core");

            // initial app db order by priority
            instances.OrderBy(x => x.Priority).ToList()
            .ForEach(instance =>
            {
                Console.WriteLine($"DbInitializer: {instance.ToString()}");
                dc.Transaction <IDbRecord>(() => instance.Load(dc));
            });
        }
Beispiel #10
0
        public void Initialize(IConfiguration config, IWebHostEnvironment env)
        {
            var dc = new DefaultDataContextLoader().GetDefaultDc();

            var assemblies = (string[])AppDomain.CurrentDomain.GetData("Assemblies");
            var instances  = TypeHelper.GetInstanceWithInterface <IHookDbInitializer>(assemblies);

            // initial app db order by priority
            instances.OrderBy(x => x.Priority).ToList()
            .ForEach(instance =>
            {
                Console.WriteLine($"DbInitializer: {instance.ToString()}");
                dc.Transaction <IDbRecord>(() => instance.Load(dc));
            });
        }
        public ActionResult Verify([FromRoute] string agentId)
        {
            var mode      = Request.Query.ContainsKey("hub.mode") ? Request.Query["hub.mode"].ToString() : String.Empty;
            var token     = Request.Query.ContainsKey("hub.verify_token") ? Request.Query["hub.verify_token"].ToString() : String.Empty;
            var challenge = Request.Query.ContainsKey("hub.challenge") ? Request.Query["hub.challenge"].ToString() : String.Empty;

            if (mode == "subscribe")
            {
                var dc     = new DefaultDataContextLoader().GetDefaultDc();
                var config = dc.Table <AgentIntegration>().FirstOrDefault(x => x.AgentId == agentId && x.Platform == "Facebook Messenger");

                return(config.VerifyToken == token?Ok(challenge) : Ok(agentId));
            }

            return(BadRequest());
        }
Beispiel #12
0
        public async Task <bool> Train(Agent agent, NlpDoc doc, PipeModel meta)
        {
            var client  = new RestClient(Configuration.GetSection("SpaCyProvider:Url").Value);
            var request = new RestRequest("tagger", Method.GET);
            List <List <String> > tags = new List <List <String> >();
            Boolean res    = true;
            var     dc     = new DefaultDataContextLoader().GetDefaultDc();
            var     corpus = agent.Corpus;

            corpus.UserSays.ForEach(usersay => {
                request.AddParameter("text", usersay.Text);
                var response = client.Execute <Result>(request);
                tags.Add(response.Data.Tags);
                res = res && response.IsSuccessful;
            });

            return(res);
        }
Beispiel #13
0
        public async Task <string> Run([FromRoute] string workflowId, [FromBody] JObject data)
        {
            var dc = new DefaultDataContextLoader().GetDefaultDc();

            var wf = new WorkflowEngine
            {
                WorkflowId    = workflowId,
                TransactionId = Guid.NewGuid().ToString(),
                SignalHub     = hub
            };

            dc.Transaction <IDbRecord>(async delegate
            {
                await wf.Run(dc, data);
            });

            return(wf.TransactionId);
        }
Beispiel #14
0
        public async Task <bool> Train(Agent agent, JObject data, PipeModel meta)
        {
            var client  = new RestClient(Configuration.GetSection("SpaCyProvider:Url").Value);
            var request = new RestRequest("featurize", Method.GET);
            List <List <decimal> > vectors = new List <List <decimal> >();
            Boolean res = true;
            var     dc  = new DefaultDataContextLoader().GetDefaultDc();

            /*var corpus = agent.GrabCorpus(dc);
             *
             * corpus.UserSays.ForEach(usersay => {
             *  request.AddParameter("text", usersay.Text);
             *  var response = client.Execute<Result>(request);
             *  vectors.Add(response.Data.Vectors);
             *  res = res && response.IsSuccessful;
             * });*/

            data.Add("Features", JToken.FromObject(vectors));

            return(res);
        }
Beispiel #15
0
        public async Task <bool> Train(Agent agent, JObject data, PipeModel meta)
        {
            String modelPath    = "./entity_rec_output";
            String newModelName = "test";
            String outputDir    = "./entity_rec_output2";
            int    iterTimes    = 20;

            List <TrainingNode> trainingData = new List <TrainingNode>();

            var dc = new DefaultDataContextLoader().GetDefaultDc();

            /*var corpus = agent.GrabCorpus(dc);
             *
             * corpus.UserSays.ForEach(userSay =>
             * {
             *  if (userSay.Entities != null) {
             *      //texts.Add(userSay.Text);
             *      List<EntityLabel> entityLabel = new List<EntityLabel>();
             *      userSay.Entities.ForEach(entity => {
             *          entityLabel.Add(new EntityLabel(entity.Start, entity.End, entity.Entity));
             *          entitiesInTrainingSet.Add(entity.Entity);
             *      });
             *      trainingData.Add(new TrainingNode(userSay.Text, entityLabel));
             *  }
             * });*/
            entitiesInTrainingSet = entitiesInTrainingSet.Distinct().ToList();
            var client  = new RestClient(Configuration.GetSection("SpaCyProvider:Url").Value);
            var request = new RestRequest("entityrecognizer", Method.POST);

            request.RequestFormat = DataFormat.Json;

            request.AddParameter("application/json", JsonConvert.SerializeObject(new NERTrainingModel(modelPath, newModelName, outputDir, iterTimes, trainingData, entitiesInTrainingSet)), ParameterType.RequestBody);

            var response = client.Execute <Result>(request);

            data["EntityModelTrained"] = response.Data.EntityModelTrained;

            return(true);
        }
Beispiel #16
0
        public async Task SendMessage(string conversationId, string text)
        {
            await Clients.Caller.SendAsync("ShowLoading");

            var dc = new DefaultDataContextLoader().GetDefaultDc();

            string agentId           = dc.Table <Conversation>().Find(conversationId).AgentId;
            string clientAccessToken = dc.Table <Agent>().Find(agentId).ClientAccessToken;

            var config = new AIConfiguration(clientAccessToken, SupportedLanguage.English);

            config.SessionId = conversationId;

            var rasa = new RasaAi(dc, config);

            var aIResponse = rasa.TextRequest(new AIRequest {
                Query = new String[] { text }
            });

            // redirect to third-part api when get fallback intent
            if (aIResponse.Result.Metadata.IntentName == "Default Fallback Intent")
            {
                var apiAi         = new ApiAiSDK.ApiAi(new ApiAiSDK.AIConfiguration("d018bf12a8a8419797fe3965637389b0", ApiAiSDK.SupportedLanguage.English));
                var apiAiResponse = apiAi.TextRequest(text);
                aIResponse = apiAiResponse.ToObject <AIResponse>();
            }

            VoicechainResponse <ANameModel> aName = null;
            VoiceId voiceId = dc.Table <AgentVoice>().FirstOrDefault(x => x.AgentId == agentId)?.VoiceId;

            string awsAccessKey = Database.Configuration.GetSection("Aws:AWSAccessKey").Value;
            string awsSecretKey = Database.Configuration.GetSection("Aws:AWSSecretKey").Value;
            var    polly        = new PollyUtter(awsAccessKey, awsSecretKey);

            await Clients.Caller.SendAsync("HideLoading");

            for (int messageIndex = 0; messageIndex < aIResponse.Result.Fulfillment.Messages.Count; messageIndex++)
            {
                await Clients.Caller.SendAsync("ShowLoading");

                await Task.Delay(1000);

                var    message = JObject.FromObject(aIResponse.Result.Fulfillment.Messages[messageIndex]);
                string type    = (message["Type"] ?? message["type"]).ToString();

                if (type == "0")
                {
                    string speech   = (message["Speech"] ?? message["speech"]).ToString();
                    string filePath = await polly.Utter(speech, Database.ContentRootPath, voiceId);

                    //polly.Play(filePath);

                    await Clients.Caller.SendAsync("ReceiveMessage", new VmTestPayload
                    {
                        ConversationId  = conversationId,
                        Sender          = rasa.agent.Name,
                        FulfillmentText = speech,
                        Payload         = aIResponse,
                        AudioPath       = filePath
                    });
                }
                else if (type == "4")
                {
                    var payload = (message["Payload"] ?? message["payload"]).ToObject <AIResponseCustomPayload>();
                    if (payload.Task == "delay")
                    {
                        await Task.Delay(int.Parse(payload.Parameters.First().ToString()));
                    }
                    else if (payload.Task == "voice")
                    {
                        //voiceId = VoiceId.FindValue(payload.Parameters.First().ToString());
                    }
                    else if (payload.Task == "terminate")
                    {
                        if (rasa.agent.Id == "fd9f1b29-fed8-4c68-8fda-69ab463da126")
                        {
                            continue;
                        }

                        // update conversation agent id back to Voiceweb
                        dc.DbTran(() => {
                            var conversation     = dc.Table <Conversation>().Find(conversationId);
                            conversation.AgentId = "fd9f1b29-fed8-4c68-8fda-69ab463da126";
                        });

                        await Clients.Caller.SendAsync("Transfer", new VmTestPayload
                        {
                            ConversationId  = conversationId,
                            Sender          = "Voiceweb",
                            FulfillmentText = "Hi",
                            Payload         = aIResponse
                        });
                    }
                    else if (payload.Task == "transfer")
                    {
                        // get VNS, query blockchain
                        var vcDriver = new VoicechainDriver(dc);
                        aName = vcDriver.GetAName(aIResponse.Result.Parameters["VNS"]);

                        // sending feedback when do querying blockchain
                        await Clients.Caller.SendAsync("SystemNotification", new VmTestPayload
                        {
                            ConversationId  = conversationId,
                            Sender          = rasa.agent.Name,
                            FulfillmentText = $"Querying VNS {aName.Data.Domain} on Blockchain: IP - {aName.Data.Value}. <a href='http://www.voicecoin.net/tx/{aName.Data.Txid}' target='_blank' style='color:white;'>View Transaction</a>",
                            Payload         = aName
                        });

                        // switch to another agent
                        var newAgent = (from vns in dc.Table <VnsTable>()
                                        join agent in dc.Table <Agent>() on vns.AgentId equals agent.Id
                                        select new
                        {
                            agent.Id,
                            agent.Name,
                            vns.Domain
                        })
                                       .FirstOrDefault(x => x.Domain == aName.Data.Domain);

                        // update conversation agent id to new agent
                        dc.DbTran(() => {
                            var conversation     = dc.Table <Conversation>().Find(conversationId);
                            conversation.AgentId = newAgent.Id;
                        });

                        await Clients.Caller.SendAsync("Transfer", new VmTestPayload
                        {
                            ConversationId  = conversationId,
                            Sender          = newAgent.Name,
                            FulfillmentText = "Hi",
                            Payload         = aName
                        });
                    }
                }

                await Clients.Caller.SendAsync("HideLoading");
            }
        }
Beispiel #17
0
        public ActionResult <List <Agent> > AllAgents()
        {
            var dc = new DefaultDataContextLoader().GetDefaultDc();

            return(dc.Table <Agent>().ToList());
        }