Exemplo n.º 1
0
        public async Task <PredictResponse> Submit(Question question)
        {
            _httpClient.VerifyNotNull($"{nameof(PredictService)} has been disposed");

            await _limit.WaitAsync(TimeSpan.FromMinutes(5));

            try
            {
                string requestUri = _option.PropertyResolver.Resolve(_option.ServiceUri);

                _logger.LogInformation($"Sending question '{_json.Serialize(question)}' to model at {requestUri}.");

                var             sw = Stopwatch.StartNew();
                PredictResponse predictResponse = await _httpClient.PostAsJsonAsync(requestUri, question)
                                                  .GetContent <PredictResponse>();

                predictResponse.Request ??= question.Sentence;

                _logger.LogInformation($"Receive answer '{_json.Serialize(predictResponse)}' from model for question '{question.Sentence}', ms={sw.ElapsedMilliseconds}");
                return(predictResponse);
            }
            finally
            {
                _limit.Release();
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <BatchResponse> > Process([FromBody] BatchRequest request, CancellationToken token)
        {
            _logger.LogInformation($"{nameof(Question)}: {_json.Serialize(request)}");

            if (!request.IsValidRequest())
            {
                return(StatusCode((int)HttpStatusCode.BadRequest));
            }

            return(await _batchService.Submit(request, token));
        }
Exemplo n.º 3
0
        private void CreateTargetTemplate(int index, string file)
        {
            var record = new TargetRecord
            {
                Id             = $"Target_{index}",
                Description    = $"Target_{index} description",
                ReadyUrl       = $"http://localhost:{index + 5000}/ping/ready",
                RunningUrl     = $"http://localhost:{index + 5000}/ping/running",
                StatusCodeMaps = new StatusCodeMap[]
                {
                    new StatusCodeMap {
                        HttpStatusCode = HttpStatusCode.OK, State = TargetState.Ok
                    },
                    new StatusCodeMap {
                        HttpStatusCode = HttpStatusCode.NotFound, State = TargetState.Error
                    },
                },
                BodyElementMaps = new BodyElementMap[]
                {
                    new BodyElementMap {
                        State = TargetState.Ok, Path = "/state", CompareTo = "success"
                    },
                    new BodyElementMap {
                        State = TargetState.Error, Path = "/state", CompareTo = "error"
                    },
                },
                TargetType         = "REST",
                Enabled            = true,
                FrequencyInSeconds = (int)TimeSpan.FromMinutes(5).TotalSeconds,
            };

            File.WriteAllText(file, _json.Serialize(record));
            _logger.LogInformation($"Create json template {file} for Agent Assignment");
        }
Exemplo n.º 4
0
        public async Task <ActionResult <PredictResponse> > Submit([FromBody] PredictRequest request)
        {
            _logger.LogInformation($"{nameof(Submit)}: {_json.Serialize(request)}");

            if (!request.IsValidRequest())
            {
                return(StatusCode((int)HttpStatusCode.BadRequest));
            }

            switch (_executionContext.State)
            {
            case ExecutionState.Booting:
            case ExecutionState.Starting:
            case ExecutionState.Restarting:
                return(ReturnNotAvailable());

            case ExecutionState.Running:
                try
                {
                    PredictResponse hostResponse = await _predict.Submit(new Question { Sentence = request.Request ?? request.Sentence });

                    _logger.LogInformation($"{nameof(Submit)} answer: {_json.Serialize(hostResponse)}");

                    var result = new PredictResponse
                    {
                        Model   = hostResponse.Model,
                        Request = hostResponse.Request,
                        Intents = hostResponse.Intents
                                  .OrderByDescending(x => x.Score)
                                  .Take(request.IntentLimit ?? int.MaxValue)
                                  .ToList(),
                    };

                    return(Ok(result));
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Exception from model.  Ex={ex}");
                    throw;
                }

            default:
                _logger.LogError($"Failed: ExecutionState={_executionContext.State}");
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Set content
        /// </summary>
        /// <typeparam name="T">type to serialize</typeparam>
        /// <param name="value">value type instance</param>
        /// <param name="required">true if required, false return with out setting</param>
        /// <returns>this</returns>
        public RestClient SetContent <T>(T value, bool required = true)
        {
            value.VerifyAssert(x => !required || x != null, "Value is required but null");

            string jsonString = _json.Serialize(value);

            Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
            return(this);
        }
Exemplo n.º 6
0
        public async Task Get(CancellationToken token)
        {
            _logger.LogInformation($"{nameof(Get)}: Getting ID={_option.Id} and writing to file={_option.File}");

            Record <T>?record = await _recordContainer.Get(_option.Id !, token : token);

            record.VerifyNotNull($"Cannot read {_option.Id} for {_entityName}");

            File.WriteAllText(_option.File, _json.Serialize(record.Value));
        }
Exemplo n.º 7
0
 public async Task <TResp> HttpPost <TResp, TRequest>(TRequest request, string uri)
 {
     try
     {
         if (_stopWatch != null)
         {
             _stopWatch.Start();
         }
         HttpClient client  = GetHttpClient();
         string     jsonReq = _json.Serialize <TRequest>(request);
         using (HttpResponseMessage httpresp = await client.PostAsync(uri, new StringContent(jsonReq, Encoding.UTF8, "application/json"), _cts.Token))
         {
             LastStatus = httpresp.StatusCode;
             return(await GetResult <TResp>(httpresp));
         }
     }
     catch (Exception ex)
     {
         Dispose();
         LastStatus = HttpStatusCode.NotAcceptable;          // UnMarshall error
         _logger.LogWarning($"RestClient | HttpPost : {uri} : { ex.Message}");
     }
     return(default(TResp));
 }
Exemplo n.º 8
0
        private void CreateMetadataTemplate(int index, string file)
        {
            var record = new MetadataRecord
            {
                Id         = $"Metadata_{index}",
                Properties = new[]
                {
                    new KeyValue("key1", "value1"),
                    new KeyValue("key2", "value2"),
                },
            };

            File.WriteAllText(file, _json.Serialize(record));
            _logger.LogInformation($"Create json template {file} for Metadata");
        }