Example #1
0
    public async ValueTask <ActionResult <string> > GenerateTextValue(
        int length,
        TextStructure textStructure = TextStructure.Text,
        string?language             = "en",
        int maxShouldContainErrors  = 10,
        int maxShouldContainSlow    = 10)
    {
        if (language == null)
        {
            language = "en";
        }

        var shouldContain = new List <string>();

        if (Profile.Type == ProfileType.User)
        {
            // Personalize text generation.
            var data = await _typingReportGerenator.GenerateStandardUserStatisticsAsync(Profile.ProfileId, language);

            shouldContain.AddRange(data.KeyPairs
                                   .Where(x => x.FromKey?.Length == 1 && x.ToKey.Length == 1)
                                   .OrderByDescending(x => x.MistakesToSuccessRatio)
                                   .Select(x => $"{x.FromKey}{x.ToKey}")
                                   .Take(maxShouldContainErrors));

            shouldContain.AddRange(data.KeyPairs
                                   .Where(x => x.FromKey?.Length == 1 && x.ToKey.Length == 1)
                                   .OrderByDescending(x => x.AverageDelay)
                                   .Select(x => $"{x.FromKey}{x.ToKey}")
                                   .Take(maxShouldContainSlow));
        }

        _logger.LogDebug("Generating text for user {ProfileId} containing {@Contains}.", ProfileId, shouldContain);

        //var textValue = await _textGenerator.GenerateTextAsync(new TextGenerationConfigurationDto(length, shouldContain, textType, language));
        var config = Texts.TextGenerationConfiguration.SelfImprovement(
            new(language), textStructure: textStructure, shouldContain: shouldContain);
        var generatedText = await _textsClient.GenerateTextAsync(config, EndpointAuthentication.Service, default);

        var textValue = generatedText.Value;

        var textId = await _textRepository.NextIdAsync();

        var configuration = new TextConfiguration(
            TextType.Generated,
            new Typing.TextGenerationConfiguration(length, shouldContain, config.GetTextGenerationType()),
            language);

        var text = new Text(textId, textValue, ProfileId, DateTime.UtcNow, false, configuration);
        await _textRepository.SaveAsync(text);

        return(Ok(textId));
    }