From 9bc06d83407cffef5c0853276f6056e701b4295b Mon Sep 17 00:00:00 2001 From: Cristiano Ferreira Date: Mon, 10 Jun 2024 19:30:41 +0100 Subject: [PATCH] Start on level exam for utas. --- app.py | 15 ++++++++++++++ helper/exercises.py | 50 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 9b22d2e..6f02d4f 100644 --- a/app.py +++ b/app.py @@ -1013,6 +1013,21 @@ def get_level_exam(): except Exception as e: return str(e) +@app.route('/level_utas', method=['GET']) +@jwt_required() +def get_level_utas(): + try: + number_of_exercises = 45 + mc_exercises = gen_multiple_choice_level(number_of_exercises) + return { + "exercises": [mc_exercises], + "isDiagnostic": False, + "minTimer": 25, + "module": "level" + } + except Exception as e: + return str(e) + @app.route('/fetch_tips', methods=['POST']) @jwt_required() diff --git a/helper/exercises.py b/helper/exercises.py index c009e2d..c379417 100644 --- a/helper/exercises.py +++ b/helper/exercises.py @@ -1036,7 +1036,7 @@ def gen_multiple_choice_level(quantity: int, start_id=1): ["questions"], GEN_QUESTION_TEMPERATURE) - if len(question["questions"]) != 25: + if len(question["questions"]) != quantity: return gen_multiple_choice_level(quantity, start_id) else: all_exams = get_all("level") @@ -1063,12 +1063,11 @@ def replace_exercise_if_exists(all_exams, current_exercise, current_exam, seen_k seen_keys.add(key) for exam in all_exams: - exam_dict = exam.to_dict() if any( exercise["prompt"] == current_exercise["prompt"] and any(exercise["options"][0]["text"] == current_option["text"] for current_option in current_exercise["options"]) - for exercise in exam_dict.get("exercises", [])[0]["questions"] + for exercise in exam.get("questions", []) ): return replace_exercise_if_exists(all_exams, generate_single_mc_level_question(), current_exam, seen_keys) return current_exercise, seen_keys @@ -1109,3 +1108,48 @@ def parse_conversation(conversation_data): readable_text.append(f"{name}: {text}") return "\n".join(readable_text) + + +def gen_multiple_choice_blank_space_utas(quantity: int, start_id: int, all_exams): + gen_multiple_choice_for_text = "Generate " + str( + quantity) + " multiple choice blank space questions of 4 options for an english level exam, some easy questions, some intermediate " \ + "questions and some advanced questions. Ensure that the questions cover a range of topics such as " \ + "verb tense, subject-verb agreement, pronoun usage, sentence structure, and punctuation. Make sure " \ + "every question only has 1 correct answer." + + messages = [ + { + "role": "system", + "content": ( + 'You are a helpful assistant designed to output JSON on this format: {"questions": [{"id": "9", "options": ' + '[{"id": "A", "text": ' + '"And"}, {"id": "B", "text": "Cat"}, {"id": "C", "text": ' + '"Happy"}, {"id": "D", "text": "Jump"}], ' + '"prompt": "Which of the following is a conjunction?", ' + '"solution": "A", "variant": "text"}]}') + }, + { + "role": "user", + "content": gen_multiple_choice_for_text + } + ] + + token_count = count_total_tokens(messages) + question = make_openai_call(GPT_4_O, messages, token_count, + ["questions"], + GEN_QUESTION_TEMPERATURE) + + if len(question["questions"]) != quantity: + return gen_multiple_choice_level(quantity, start_id) + else: + seen_keys = set() + for i in range(len(question["questions"])): + question["questions"][i], seen_keys = replace_exercise_if_exists(all_exams, question["questions"][i], + question, + seen_keys) + return { + "id": str(uuid.uuid4()), + "prompt": "Select the appropriate option.", + "questions": fix_exercise_ids(question, start_id)["questions"], + "type": "multipleChoice", + }