public async Task SessionTest()
        {
            var config = new AIConfiguration(SUBSCRIPTION_KEY, ACCESS_TOKEN, SupportedLanguage.English);

            var firstService = new AIDataService(config);
            var secondService = new AIDataService(config);

            {
                var weatherRequest = new AIRequest("weather");
                var weatherResponse = await MakeRequestAsync(firstService, weatherRequest);
                Assert.IsNotNull(weatherResponse);
            }

            {
                var checkSecondRequest = new AIRequest("check weather");
                var checkSecondResponse = await MakeRequestAsync(secondService, checkSecondRequest);
                Assert.IsTrue(string.IsNullOrEmpty(checkSecondResponse.Result.Action));
            }

            {
                var checkFirstRequest = new AIRequest("check weather");
                var checkFirstResponse = await MakeRequestAsync(firstService, checkFirstRequest);
                Assert.IsNotNull(checkFirstResponse.Result.Action);
                Assert.IsTrue(checkFirstResponse.Result.Action.Equals("checked", StringComparison.CurrentCultureIgnoreCase));
            }
        }
        public async Task DifferentAgentsTest()
        {
            var query = "I want pizza";

            {
                var config = new AIConfiguration(SUBSCRIPTION_KEY, ACCESS_TOKEN, SupportedLanguage.English);
                var dataService = new AIDataService(config);

                var request = new AIRequest(query);

                var response = await dataService.RequestAsync(request);
                Assert.IsNotNull(response.Result);
                Assert.AreEqual("pizza", response.Result.Action);

            }

            {
                var config = new AIConfiguration(SUBSCRIPTION_KEY, "968235e8e4954cf0bb0dc07736725ecd", SupportedLanguage.English);
                var dataService = new AIDataService(config);
                var request = new AIRequest(query);

                var response = await dataService.RequestAsync(request);
                Assert.IsNotNull(response.Result);
                Assert.IsTrue(string.IsNullOrEmpty(response.Result.Action));

            }

        }
Ejemplo n.º 3
0
		public void TextRequestTest()
		{
			var config = new AIConfiguration(SUBSCRIPTION_KEY, ACCESS_TOKEN, SupportedLanguage.English);

			var apiAi = new ApiAi(config);

			var response = apiAi.TextRequest("hello");

			Assert.IsNotNull(response);
			Assert.AreEqual("greeting", response.Result.Action);
            Assert.AreEqual("Hi! How are you?", response.Result.Fulfillment.Speech);
		}
Ejemplo n.º 4
0
		public void VoiceRequestTest()
		{
			var config = new AIConfiguration(SUBSCRIPTION_KEY, ACCESS_TOKEN, SupportedLanguage.English);

			var apiAi = new ApiAi(config);
			var stream = ReadFileFromResource("ApiAiSDK.Tests.TestData.what_is_your_name.raw");

			var response = apiAi.VoiceRequest(stream);

			Assert.IsNotNull(response);
			Assert.AreEqual("what is your name", response.Result.ResolvedQuery);
		}
        public async Task TestTextRequest()
        {
            var config = new AIConfiguration(SUBSCRIPTION_KEY, ACCESS_TOKEN, SupportedLanguage.English);
            var dataService = new AIDataService(config);

            var request = new AIRequest("Hello");

            var response = await dataService.RequestAsync(request);

            Assert.IsNotNull(response);
            Assert.AreEqual("greeting", response.Result.Action);
            Assert.AreEqual("Hi! How are you?", response.Result.Fulfillment.Speech);
        }
Ejemplo n.º 6
0
 public SpeaktoitRecognitionService(Context context, AIConfiguration config)
     : base(config)
 {
     InitRecorder();
 }
Ejemplo n.º 7
0
 public static AIService CreateService(AIConfiguration config)
 {
     return(new SpeaktoitRecognitionService(config));
 }
Ejemplo n.º 8
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");
            }
        }
Ejemplo n.º 9
0
        private void Initialise()
        {
            if (DoOnce)
            {
                return;
            }
            try
            {
                var config = new AIConfiguration(Tokens.Load().DialogFlowToken, SupportedLanguage.English);
                _apiAi = new ApiAi(config);
            }
            catch
            {
                //
            }

            try
            {
                foreach (var guild in _client.Guilds)
                {
                    try
                    {
                        var guildconfig = GuildConfig.GetServer(guild);

                        try
                        {
                            if (guildconfig.Levels.LevellingEnabled)
                            {
                                Levels.Add(new LevellingObj
                                {
                                    GuildID = guildconfig.GuildId,
                                    Users   = guildconfig.Levels.Users
                                });
                            }

                            if (guildconfig.AutoMessage.Any())
                            {
                                AutomessageList.Add(new AutoMessages
                                {
                                    GuildID  = guildconfig.GuildId,
                                    Channels = guildconfig.AutoMessage
                                });
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }

                        if (guildconfig.PartnerSetup.banned)
                        {
                            continue;
                        }
                        if (!guildconfig.PartnerSetup.IsPartner || !(_client.GetChannel(guildconfig.PartnerSetup.PartherChannel) is IMessageChannel))
                        {
                            continue;
                        }
                        Console.WriteLine($"{guild.Id} Added to Timers");
                        TimerService.AcceptedServers.Add(guild.Id);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
            catch
            {
                //
            }

            _service.Restart();
            Console.WriteLine("DoOnce Completed.");
            DoOnce = true;
        }
Ejemplo n.º 10
0
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result)
        {
            var activity = await result as Activity;

            var apiAiToken = ConfigurationManager.AppSettings["ApiAiToken"];
            var config     = new AIConfiguration(apiAiToken, SupportedLanguage.English);

            if (!string.IsNullOrEmpty(SessionId))
            {
                config.SessionId          = SessionId;
                appointment.AppointmentId = Guid.NewGuid();
            }
            var apiAi = new ApiAi(config);

            // return our reply to the user
            if (activity != null)
            {
                var response = ApiAiHelper.ParseApiResponse(apiAi.TextRequest(activity.Text));
                SessionId = response.SessionId;
                if (response.Parameters.All(x => string.IsNullOrEmpty(x.Value.ToString())))
                {
                    await context.PostAsync(response.ResponseMessage);

                    context.Wait(this.MessageReceivedAsync);
                    return;
                }

                foreach (var item in response.Parameters)
                {
                    switch (item.Key)
                    {
                    case "Datetime":
                    {
                        if (appointment.Datetime == DateTime.MinValue && item.Value != "")
                        {
                            var      data     = item.Value.ToString();
                            var      errorMsg = string.Empty;
                            var      isError  = false;
                            DateTime date;
                            if (!DateTime.TryParse(data, out date))
                            {
                                errorMsg = "Please Enter valid Appointment Date";
                                isError  = true;
                            }
                            else if (date.Date <= DateTime.Now.Date)
                            {
                                errorMsg = "Please Enter valid Appointment Date";
                                isError  = true;
                            }
                            else
                            {
                                appointment.Datetime = date;
                                isError = false;
                            }
                            if (isError)
                            {
                                await context.PostAsync(errorMsg);

                                context.Wait(this.MessageReceivedAsync);
                                return;
                            }
                        }
                    }
                    break;

                    case "Time":
                    {
                        if (appointment.Time == default(TimeSpan) && !string.IsNullOrEmpty(item.Value.ToString()))
                        {
                            var      data     = item.Value.ToString();
                            var      errorMsg = string.Empty;
                            TimeSpan time;
                            var      isError = false;
                            if (!TimeSpan.TryParse(data, out time))
                            {
                                errorMsg = "Please Enter valid Appointment Time";
                                isError  = true;
                            }
                            else if (appointment.Datetime != DateTime.MinValue && time <= appointment.Datetime.TimeOfDay)
                            {
                                errorMsg = "Please Enter valid Appointment Time";
                                isError  = true;
                            }
                            else if (appointment.Datetime == DateTime.MinValue && time <= DateTime.Now.TimeOfDay)
                            {
                                errorMsg = "Please Enter valid Appointment Time";
                                isError  = true;
                            }
                            else
                            {
                                appointment.Time = time;
                                isError          = false;
                            }
                            if (isError)
                            {
                                await context.PostAsync(errorMsg);

                                context.Wait(this.MessageReceivedAsync);
                                return;
                            }
                        }
                    }
                    break;

                    case "Make":
                    {
                        if (string.IsNullOrEmpty(appointment.Make) && !string.IsNullOrEmpty(item.Value.ToString()))
                        {
                            appointment.Make = item.Value.ToString();
                        }
                    }
                    break;

                    case "Model":
                    {
                        if (string.IsNullOrEmpty(appointment.Model) && !string.IsNullOrEmpty(item.Value.ToString()))
                        {
                            appointment.Model = item.Value.ToString();
                        }
                        ;
                    }
                    break;

                    case "Year":
                    {
                        if (string.IsNullOrEmpty(appointment.Year) && !string.IsNullOrEmpty(item.Value.ToString()))
                        {
                            var   data     = item.Value.ToString();
                            var   errorMsg = string.Empty;
                            var   isError  = false;
                            Regex regex    = new Regex(@"^\d{4}$");
                            if (!regex.IsMatch(data))
                            {
                                errorMsg = "Please Enter valid Vehicle Year";
                                isError  = true;
                            }
                            else
                            {
                                appointment.Year = data;
                                isError          = false;
                            }
                            if (isError)
                            {
                                await context.PostAsync(errorMsg);

                                context.Wait(this.MessageReceivedAsync);
                                return;
                            }
                        }
                    }
                    break;
                    }
                }

                if (response.IsComplete)
                {
                    FirebaseProvider fp = new FirebaseProvider();
                    fp.SaveAppointment(JsonConvert.SerializeObject(appointment));
                }
                await context.PostAsync(response.ResponseMessage);

                context.Wait(this.MessageReceivedAsync);
            }
        }
        public async Task ContextsTest()
        {
            var config = new AIConfiguration(SUBSCRIPTION_KEY, ACCESS_TOKEN, SupportedLanguage.English);
            var dataService = new AIDataService(config);
            var aiRequest = new AIRequest("weather");

            await dataService.ResetContextsAsync();

            var aiResponse = await MakeRequestAsync(dataService, aiRequest);
            var action = aiResponse.Result.Action;
            Assert.AreEqual("showWeather", action);
            Assert.IsTrue(aiResponse.Result.Contexts.Any(c => c.Name == "weather"));

        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null)
            {
                var config = new AIConfiguration("cb9693af-85ce-4fbf-844a-5563722fc27f",
                                                 "40048a5740a1455c9737342154e86946",
                                                 SupportedLanguage.English);

                apiAi = new ApiAi(config);
                apiAi.DataService.PersistSessionId();

                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                    voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted;
                    var voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    var recognizedText = voiceCommand.SpeechRecognitionResult?.Text;

                    switch (voiceCommand.CommandName)
                    {
                    case "type":
                    {
                        var aiResponse = await apiAi.TextRequestAsync(recognizedText);

                        await apiAi.LaunchAppInForegroundAsync(voiceServiceConnection, aiResponse);
                    }
                    break;

                    case "unknown":
                    {
                        if (!string.IsNullOrEmpty(recognizedText))
                        {
                            var aiResponse = await apiAi.TextRequestAsync(recognizedText);

                            if (aiResponse != null)
                            {
                                await apiAi.SendResponseToCortanaAsync(voiceServiceConnection, aiResponse);
                            }
                        }
                    }
                    break;

                    case "greetings":
                    {
                        var aiResponse = await apiAi.TextRequestAsync(recognizedText);

                        var repeatMessage = new VoiceCommandUserMessage
                        {
                            DisplayMessage = "Repeat please",
                            SpokenMessage  = "Repeat please"
                        };

                        var processingMessage = new VoiceCommandUserMessage
                        {
                            DisplayMessage = aiResponse?.Result?.Fulfillment?.Speech ?? "Pizza",
                            SpokenMessage  = ""
                        };

                        var resp = VoiceCommandResponse.CreateResponseForPrompt(processingMessage, repeatMessage);
                        await voiceServiceConnection.ReportSuccessAsync(resp);

                        break;
                    }

                    default:
                        if (!string.IsNullOrEmpty(recognizedText))
                        {
                            var aiResponse = await apiAi.TextRequestAsync(recognizedText);

                            if (aiResponse != null)
                            {
                                await apiAi.SendResponseToCortanaAsync(voiceServiceConnection, aiResponse);
                            }
                        }
                        else
                        {
                            await SendResponse("Can't recognize");
                        }

                        break;
                    }
                }
                catch (Exception e)
                {
                    var message = e.ToString();
                    Debug.WriteLine(message);
                }
                finally
                {
                    serviceDeferral?.Complete();
                }
            }
        }
        public async Task WrongEntitiesTest()
        {
            var config = new AIConfiguration(SUBSCRIPTION_KEY, ACCESS_TOKEN, SupportedLanguage.English);
            var dataService = new AIDataService(config);

            var aiRequest = new AIRequest("hi nori");

            var myDwarfs = new Entity("not_dwarfs");
            myDwarfs.AddEntry(new EntityEntry("Ori", new[] { "ori", "Nori" }));
            myDwarfs.AddEntry(new EntityEntry("bifur", new[] { "Bofur", "Bombur" }));

            var extraEntities = new List<Entity> { myDwarfs };

            aiRequest.Entities = extraEntities;

            try
            {
                var aiResponse = await MakeRequestAsync(dataService, aiRequest);
                Assert.IsTrue(false, "Request should throws bad_request exception");
            }
            catch (AIServiceException e)
            {
                Assert.IsTrue(true);
            }
        }
        public async Task EntitiesTest()
        {
            var config = new AIConfiguration(SUBSCRIPTION_KEY, ACCESS_TOKEN, SupportedLanguage.English);
            var dataService = new AIDataService(config);

            var aiRequest = new AIRequest("hi nori");

            var myDwarfs = new Entity("dwarfs");
            myDwarfs.AddEntry(new EntityEntry("Ori", new[] { "ori", "Nori" }));
            myDwarfs.AddEntry(new EntityEntry("bifur", new[] { "Bofur", "Bombur" }));

            var extraEntities = new List<Entity> { myDwarfs };

            aiRequest.Entities = extraEntities;

            var aiResponse = await MakeRequestAsync(dataService, aiRequest);

            Assert.IsTrue(!string.IsNullOrEmpty(aiResponse.Result.ResolvedQuery));
            Assert.AreEqual("say_hi", aiResponse.Result.Action);
            Assert.AreEqual("hi Bilbo, I am Ori", aiResponse.Result.Fulfillment.Speech);
        }
        public async Task ResetContextsTest()
        {
            var config = new AIConfiguration(SUBSCRIPTION_KEY, ACCESS_TOKEN, SupportedLanguage.English);
            var dataService = new AIDataService(config);

            await dataService.ResetContextsAsync();

            {
                var aiRequest = new AIRequest("what is your name");
                var aiResponse = await MakeRequestAsync(dataService, aiRequest);
                Assert.IsTrue(aiResponse.Result.Contexts.Any(c => c.Name == "name_question"));
                var resetSucceed = await dataService.ResetContextsAsync();
                Assert.IsTrue(resetSucceed);
            }

            {
                var aiRequest = new AIRequest("hello");
                var aiResponse = await MakeRequestAsync(dataService, aiRequest);
                Assert.IsFalse(aiResponse.Result.Contexts.Any(c => c.Name == "name_question"));
            }

        }
Ejemplo n.º 16
0
 protected AIService(AIConfiguration config)
 {
     this.config = config;
     dataService = new AIDataService(config);
 }
Ejemplo n.º 17
0
 public static AIService CreateService(Context context, AIConfiguration config)
 {
     return(new SystemRecognitionService(context, config));
 }
Ejemplo n.º 18
0
        public ActionResult <RasaResponse> Parse(RasaRequestModel request)
        {
            var config = new AIConfiguration("", SupportedLanguage.English);

            config.SessionId = "rasa nlu";

            string body = "";

            using (var reader = new StreamReader(Request.Body))
            {
                body = reader.ReadToEnd();
            }

            Console.WriteLine($"Got message from {Request.Host}: {body}", Color.Green);
            if (request.Project == null && !String.IsNullOrEmpty(body))
            {
                request = JsonConvert.DeserializeObject <RasaRequestModel>(body);
            }

            // Load agent
            var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", request.Project);

            if (String.IsNullOrEmpty(request.Model))
            {
                request.Model = Directory.GetDirectories(projectPath).Where(x => x.Contains("model_")).Last().Split(Path.DirectorySeparatorChar).Last();
            }

            var modelPath = Path.Combine(projectPath, request.Model);

            var agent = _platform.LoadAgentFromFile(modelPath);

            var aIResponse = _platform.TextRequest(new AIRequest
            {
                AgentDir = projectPath,
                Model    = request.Model,
                Query    = new String[] { request.Text }
            });

            var rasaResponse = new RasaResponse
            {
                Intent = new RasaResponseIntent
                {
                    Name       = aIResponse.Result.Metadata.IntentName,
                    Confidence = aIResponse.Result.Score
                },
                Entities = aIResponse.Result.Entities.Select(x => new RasaResponseEntity
                {
                    Extractor = x.Extrator,
                    Start     = x.Start,
                    Entity    = x.Entity,
                    Value     = x.Value
                }).ToList(),
                Text          = request.Text,
                Model         = request.Model,
                Project       = agent.Name,
                IntentRanking = new List <RasaResponseIntent>
                {
                    new RasaResponseIntent
                    {
                        Name       = aIResponse.Result.Metadata.IntentName,
                        Confidence = aIResponse.Result.Score
                    }
                },
                Fullfillment = aIResponse.Result.Fulfillment
            };

            return(rasaResponse);
        }
Ejemplo n.º 19
0
 public void Initialize(AIConfiguration config)
 {
     this.config = config;
     apiAi       = new ApiAi(this.config);
 }
        public async Task ParametersTest()
        {
            var config = new AIConfiguration(SUBSCRIPTION_KEY, ACCESS_TOKEN, SupportedLanguage.English);
            var dataService = new AIDataService(config);

            var request = new AIRequest("what is your name");

            var response = await MakeRequestAsync(dataService, request);

            Assert.IsNotNull(response.Result.Parameters);
            Assert.IsTrue(response.Result.Parameters.Count > 0);

            Assert.IsTrue(response.Result.Parameters.ContainsKey("my_name"));
            Assert.IsTrue(response.Result.Parameters.ContainsValue("Sam"));

            Assert.IsNotNull(response.Result.Contexts);
            Assert.IsTrue(response.Result.Contexts.Length > 0);
            var context = response.Result.Contexts[0];

            Assert.IsNotNull(context.Parameters);
            Assert.IsTrue(context.Parameters.ContainsKey("my_name"));
            Assert.IsTrue(context.Parameters.ContainsValue("Sam"));
        }