Future generateNewSuggestions()

in frontend/frontend-flutter/lib/services/new_suggestions/new_suggestion_cubit.dart [18:197]


  Future<void> generateNewSuggestions(int scenarioNumber, String question,
      {String? lastCannedQuestion,
      bool? isACannedQuestion,
      String? userGrouping}) async {
    List<String> respLLMQuestion = [];
    List<String> respCannedQuestions = [];
    List<String> resp = [];
    List<String> tmpQuestions = [];
    String body = "";
    Uri url;
    String question1 = "";
    String question2 = "";
    String question3 = "";
    String question4 = "";
    String timeString = "";
    String originalQuestion = "";

    print(
        'NewSuggestionCubit : NewSuggestionCubit() : generateNewSuggestions : START');
    print(
        'NewSuggestionCubit : NewSuggestionCubit() : generateNewSuggestions : userGrouping = $userGrouping');
    print(
        'NewSuggestionCubit : NewSuggestionCubit() : generateNewSuggestions : TextToDocParameter.lastCannedQuestion = ${TextToDocParameter.lastCannedQuestion}');
    print(
        'NewSuggestionCubit : NewSuggestionCubit() : generateNewSuggestions : lastCannedQuestion= ${lastCannedQuestion}');

    print(
        'NewSuggestionCubit : NewSuggestionCubit() : generateNewSuggestions : scenarioNumber = $scenarioNumber');
    print(
        'NewSuggestionCubit : NewSuggestionCubit() : generateNewSuggestions : question = $question');

    timeString = displayDateTime();
    //TextToDocParameter.lastCannedQuestion = question;
    //TextToDocParameter.lastCannedQuestion = lastCannedQuestion?? "";

    isACannedQuestion = isACannedQuestion ?? false;
    print(
        'NewSuggestionCubit : NewSuggestionCubit() : generateNewSuggestions : Not a canned question : isACannedQuestion = $isACannedQuestion');
    originalQuestion = question;

    print(
        'NewSuggestionCubit : NewSuggestionCubit() : generateNewSuggestions : Not a canned question : originalQuestion = $originalQuestion');

    //Create the header
    Map<String, String>? _headers = {
      "Content-Type": "application/json",
      //"Authorization": " Bearer ${client!.credentials.accessToken.toString()}",
    };

    //Create the body
    body = '''{
          "user_grouping": "$userGrouping"
      }''';

    print(
        'NewSuggestionCubit : generateNewSuggestions() : Not a canned question : body = ' +
            body);

    try {
      var response = await html.HttpRequest.requestCrossOrigin(
          '${TextToDocParameter.endpoint_opendataqnq}/get_known_sql',
          method: "POST",
          sendData: body);

      print(
          'NewSuggestionCubit : generateNewSuggestions() : Not a canned question : response = ' +
              response.toString());

      final jsonData = jsonDecode(response);

      if (jsonData != null) {
        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : jsonData = $jsonData');

        //KnownSQL = [{"example_user_question": "question1", "example_generated_sql": "sql1"},
        // {"example_user_question": "question2", "example_generated_sql": "sql2"},
        // ...]

        var knownSql =
            jsonData["KnownSQL"].replaceAll(RegExp(r'((\\n)|(\\r))'), '');

        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : knownSql = $knownSql');

        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : knownSql.runtimeType = ${knownSql.runtimeType}');

        if (knownSql is Map)
          print(
              'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : knownSql is a Map');
        if (knownSql is List)
          print(
              'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : knownSql is a List');
        if (knownSql is String)
          print(
              'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : knownSql is a String');

        var knownSqlMap = jsonDecode(knownSql);

        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : knownSqlMap.runtimeType = ${knownSqlMap.runtimeType}');

        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : knownSqlMap[0].runtimeType = ${knownSqlMap[0].runtimeType}');

        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : knownSqlMap = ${knownSqlMap}');

        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : knownSqlMap[0] = ${knownSqlMap[0].toString()}');

        for (int i = 0; i < knownSqlMap.length; i++) {
          for (var entry in knownSqlMap[i].entries) {
            print('${entry.key} : ${entry.value}');
            if (entry.key == "example_user_question")
              tmpQuestions.add(entry.value);
          }
        }

        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : Before pickUpRandomQuestion() : tmpQuestions = ${tmpQuestions}');

        tmpQuestions = pickUpRandomQuestion(question, tmpQuestions);

        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : After pickUpRandomQuestion() : tmpQuestions = ${tmpQuestions}');

        if (tmpQuestions.length > 0)
          question1 = tmpQuestions[0] ?? "No suggestion for question1";
        if (tmpQuestions.length > 1)
          question2 = tmpQuestions[1] ?? "No suggestion for question2";
        if (tmpQuestions.length > 2)
          question3 = tmpQuestions[2] ?? "No suggestion for question3";
        if (tmpQuestions.length > 3)
          question4 = tmpQuestions[3] ?? "No suggestion for question4";

        //Adding scenarioNumber + ":" and ":userGrouping" to have same format as for Business KPI questions
        question1 = "$scenarioNumber:" + question1 + ":" + userGrouping!;
        question2 = "$scenarioNumber:" + question2 + ":" + userGrouping!;
        question3 = "$scenarioNumber:" + question3 + ":" + userGrouping!;
        question4 = "$scenarioNumber:" + question4 + ":" + userGrouping!;

        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : question1 = ${question1}');
        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : question2 = ${question2}');
        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : question3 = ${question3}');
        print(
            'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : question4 = ${question4}');
      }
    } catch (e) {
      print(
          'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : EXCEPTION = $e');
      throw Exception('Failed to get suggestions: $e');
    } finally {
      respLLMQuestion.add(question1);
      respLLMQuestion.add(question2);
      respLLMQuestion.add(question3);
      respLLMQuestion.add(question4);

      print(
          'NewSuggestionCubit: generateNewSuggestions() : Not a canned question :scenarioNumber = $scenarioNumber >= 9 : respLLMQuestion = $respLLMQuestion');

      //2024-07-14 respCannedQuestions = pickUpNextQuestions(scenarioNumber, question);
      print(
          'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : scenarioNumber = $scenarioNumber >= 9 : respCannedQuestions = $respCannedQuestions');

      resp.addAll(respLLMQuestion);

      print(
          'NewSuggestionCubit: generateNewSuggestions() : Not a canned question : scenarioNumber = $scenarioNumber >= 9 : resp = $resp');
    }

    emit(state.copyWith(
        status: NewSuggestionStateStatus.loaded,
        suggestionList: resp,
        time: timeString,
        scenarioNumber: scenarioNumber));
  }