コード例 #1
0
        public async Task <AiResponse> TextRequest(AiRequest request)
        {
            var aiResponse = new AiResponse();

            // Load agent
            var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", request.AgentId);
            var model       = Directory.GetDirectories(projectPath).Where(x => x.Contains("model_")).Last().Split(Path.DirectorySeparatorChar).Last();
            var modelPath   = Path.Combine(projectPath, model);

            request.AgentDir = projectPath;
            request.Model    = model;

            var agent = await GetAgentById(request.AgentId);

            var preditor = new BotPredictor();
            var doc      = await preditor.Predict(agent, request);

            var parameters = new Dictionary <String, Object>();

            if (doc.Sentences[0].Entities == null)
            {
                doc.Sentences[0].Entities = new List <NlpEntity>();
            }
            doc.Sentences[0].Entities.ForEach(x => parameters[x.Entity] = x.Value);

            aiResponse.Intent = doc.Sentences[0].Intent.Label;
            aiResponse.Speech = aiResponse.Intent;

            return(aiResponse);
        }
コード例 #2
0
        public virtual async Task <TResult> TextRequest <TResult>(AiRequest request)
        {
            // merge last contexts
            string contextHash = await GetContextsHash(request);

            Console.WriteLine($"TextRequest: {request.Text}, {request.Contexts}, {request.SessionId}");

            // Load agent
            var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", request.AgentId);
            var model       = Directory.GetDirectories(projectPath).Where(x => x.Contains("model_")).Last().Split(Path.DirectorySeparatorChar).Last();
            var modelPath   = Path.Combine(projectPath, model);

            request.AgentDir = projectPath;
            request.Model    = model + $"{Path.DirectorySeparatorChar}{contextHash}";

            Agent = await GetAgentById(request.AgentId);

            var preditor = new BotPredictor();
            var doc      = await preditor.Predict(Agent, request);

            var predictedIntent = doc.Sentences[0].Intent;

            if (predictedIntent.Confidence < Agent.MlConfig.MinConfidence)
            {
                predictedIntent = await FallbackResponse(request);

                predictedIntent.Confidence = Agent.MlConfig.MinConfidence;
                predictedIntent.Label      = "fallback";

                Agent.Intents.Add(new Intent
                {
                    Name      = predictedIntent.Label,
                    Responses = new List <IntentResponse>
                    {
                        new IntentResponse
                        {
                            IntentName = predictedIntent.Label,
                            Messages   = new List <IntentResponseMessage>
                            {
                                new IntentResponseMessage
                                {
                                    Speech = "\"" + predictedIntent.Text + "\"",
                                    Type   = AIResponseMessageType.Text
                                }
                            }
                        }
                    }
                });
            }

            var aiResponse = new AiResponse
            {
                ResolvedQuery = request.Text,
                Score         = predictedIntent.Confidence,
                Source        = predictedIntent.Classifier,
                Intent        = predictedIntent.Label,
                Entities      = doc.Sentences[0].Entities
            };

            Console.WriteLine($"TextResponse: {aiResponse.Intent}, {request.SessionId}");

            return(await AssembleResult <TResult>(request, aiResponse));
        }