Update multiple choice questions generation.

This commit is contained in:
Cristiano Ferreira
2023-11-20 22:51:29 +00:00
parent 75985a4077
commit 223a7dfd11

View File

@@ -53,6 +53,7 @@ def replace_first_occurrences_with_placeholders(text: str, words_to_replace: lis
text = pattern.sub(placeholder, text, 1) text = pattern.sub(placeholder, text, 1)
return text return text
def replace_first_occurrences_with_placeholders_notes(notes: list, words_to_replace: list, start_id): def replace_first_occurrences_with_placeholders_notes(notes: list, words_to_replace: list, start_id):
replaced_notes = [] replaced_notes = []
for i, note in enumerate(notes, start=0): for i, note in enumerate(notes, start=0):
@@ -63,6 +64,7 @@ def replace_first_occurrences_with_placeholders_notes(notes: list, words_to_repl
replaced_notes.append(note) replaced_notes.append(note)
return replaced_notes return replaced_notes
def add_random_words_and_shuffle(word_array, num_random_words): def add_random_words_and_shuffle(word_array, num_random_words):
r = RandomWord() r = RandomWord()
random_words_selected = r.random_words(num_random_words) random_words_selected = r.random_words(num_random_words)
@@ -107,6 +109,7 @@ def build_write_blanks_text(questions: [], start_id):
result = result + q["question"] + placeholder + "\\n" result = result + q["question"] + placeholder + "\\n"
return result return result
def build_write_blanks_text_form(form: [], start_id): def build_write_blanks_text_form(form: [], start_id):
result = "" result = ""
replaced_words = [] replaced_words = []
@@ -147,6 +150,7 @@ def build_write_blanks_solutions(questions: [], start_id):
) )
return solutions return solutions
def build_write_blanks_solutions_listening(words: [], start_id): def build_write_blanks_solutions_listening(words: [], start_id):
solutions = [] solutions = []
for i, word in enumerate(words, start=start_id): for i, word in enumerate(words, start=start_id):
@@ -212,6 +216,7 @@ def generate_listening_1_conversation(topic: str):
segment['voice'] = voice segment['voice'] = voice
return response, processed return response, processed
def generate_listening_2_monologue(topic: str): def generate_listening_2_monologue(topic: str):
gen_listening_2_monologue_social = "Generate a comprehensive monologue set in the social context of: '" + topic + "'" gen_listening_2_monologue_social = "Generate a comprehensive monologue set in the social context of: '" + topic + "'"
token_count = count_tokens(gen_listening_2_monologue_social)["n_tokens"] token_count = count_tokens(gen_listening_2_monologue_social)["n_tokens"]
@@ -224,6 +229,7 @@ def generate_listening_2_monologue(topic: str):
) )
return response return response
def generate_listening_3_conversation(topic: str): def generate_listening_3_conversation(topic: str):
gen_listening_3_conversation_4_people = "Compose an authentic and elaborate conversation between up to four individuals " \ gen_listening_3_conversation_4_people = "Compose an authentic and elaborate conversation between up to four individuals " \
"in the everyday social context of '" + topic + \ "in the everyday social context of '" + topic + \
@@ -264,6 +270,7 @@ def generate_listening_3_conversation(topic: str):
segment['voice'] = voice segment['voice'] = voice
return response, processed return response, processed
def generate_listening_4_monologue(topic: str): def generate_listening_4_monologue(topic: str):
gen_listening_4_monologue_academic = "Generate a comprehensive monologue an academic subject of: '" + topic + "'" gen_listening_4_monologue_academic = "Generate a comprehensive monologue an academic subject of: '" + topic + "'"
token_count = count_tokens(gen_listening_4_monologue_academic)["n_tokens"] token_count = count_tokens(gen_listening_4_monologue_academic)["n_tokens"]
@@ -276,6 +283,7 @@ def generate_listening_4_monologue(topic: str):
) )
return response return response
def generate_reading_exercises(passage: str, req_exercises: list, number_of_exercises_q, start_id): def generate_reading_exercises(passage: str, req_exercises: list, number_of_exercises_q, start_id):
exercises = [] exercises = []
for req_exercise in req_exercises: for req_exercise in req_exercises:
@@ -302,6 +310,7 @@ def generate_reading_exercises(passage: str, req_exercises: list, number_of_exer
return exercises return exercises
def generate_listening_conversation_exercises(conversation: str, req_exercises: list, number_of_exercises_q, start_id): def generate_listening_conversation_exercises(conversation: str, req_exercises: list, number_of_exercises_q, start_id):
exercises = [] exercises = []
for req_exercise in req_exercises: for req_exercise in req_exercises:
@@ -312,15 +321,18 @@ def generate_listening_conversation_exercises(conversation: str, req_exercises:
exercises.append(question) exercises.append(question)
print("Added multiple choice: " + str(question)) print("Added multiple choice: " + str(question))
elif req_exercise == "writeBlanksQuestions": elif req_exercise == "writeBlanksQuestions":
question = gen_write_blanks_questions_exercise_listening_conversation(conversation, number_of_exercises, start_id) question = gen_write_blanks_questions_exercise_listening_conversation(conversation, number_of_exercises,
start_id)
exercises.append(question) exercises.append(question)
print("Added write blanks questions: " + str(question)) print("Added write blanks questions: " + str(question))
elif req_exercise == "writeBlanksFill": elif req_exercise == "writeBlanksFill":
question = gen_write_blanks_notes_exercise_listening_conversation(conversation, number_of_exercises, start_id) question = gen_write_blanks_notes_exercise_listening_conversation(conversation, number_of_exercises,
start_id)
exercises.append(question) exercises.append(question)
print("Added write blanks notes: " + str(question)) print("Added write blanks notes: " + str(question))
elif req_exercise == "writeBlanksForm": elif req_exercise == "writeBlanksForm":
question = gen_write_blanks_form_exercise_listening_conversation(conversation, number_of_exercises, start_id) question = gen_write_blanks_form_exercise_listening_conversation(conversation, number_of_exercises,
start_id)
exercises.append(question) exercises.append(question)
print("Added write blanks form: " + str(question)) print("Added write blanks form: " + str(question))
@@ -328,6 +340,7 @@ def generate_listening_conversation_exercises(conversation: str, req_exercises:
return exercises return exercises
def generate_listening_monologue_exercises(monologue: str, req_exercises: list, number_of_exercises_q, start_id): def generate_listening_monologue_exercises(monologue: str, req_exercises: list, number_of_exercises_q, start_id):
exercises = [] exercises = []
for req_exercise in req_exercises: for req_exercise in req_exercises:
@@ -354,6 +367,7 @@ def generate_listening_monologue_exercises(monologue: str, req_exercises: list,
return exercises return exercises
def gen_multiple_choice_exercise(text: str, quantity: int, start_id): def gen_multiple_choice_exercise(text: str, quantity: int, start_id):
gen_multiple_choice_for_text = "Generate " + str(quantity) + " multiple choice questions for this text: " \ gen_multiple_choice_for_text = "Generate " + str(quantity) + " multiple choice questions for this text: " \
"'" + text + "'\n" \ "'" + text + "'\n" \
@@ -367,15 +381,21 @@ def gen_multiple_choice_exercise(text: str, quantity: int, start_id):
None, None,
GEN_QUESTION_TEMPERATURE) GEN_QUESTION_TEMPERATURE)
parse_mc_questions = "Parse the questions into this json format: {\"questions\": [{\"id\": \"9\", \"options\": [{\"id\": \"A\", \"text\": " \ parse_mc_questions = "Parse the questions into this json format: {\"questions\": [{\"id\": \"9\", \"options\": [{\"id\": \"A\", \"text\": " \
"\"Economic benefits\"}, {\"id\": \"B\", \"text\": \"Government regulations\"}, {\"id\": \"C\", \"text\": " \ "\"Economic benefits\"}, {\"id\": \"B\", \"text\": \"Government regulations\"}, {\"id\": \"C\", \"text\": " \
"\"Concerns about climate change\"}, {\"id\": \"D\", \"text\": \"Technological advancement\"}], " \ "\"Concerns about climate change\"}, {\"id\": \"D\", \"text\": \"Technological advancement\"}], " \
"\"prompt\": \"What is the main reason for the shift towards renewable energy sources?\", " \ "\"prompt\": \"What is the main reason for the shift towards renewable energy sources?\", " \
"\"solution\": \"C\", \"variant\": \"text\"}]}. \nThe questions: '" + mc_questions + "'" "\"solution\": \"C\", \"variant\": \"text\"}]}. \nThe questions: '" + mc_questions + "'"
token_count = count_tokens(parse_mc_questions)["n_tokens"] token_count = count_tokens(parse_mc_questions)["n_tokens"]
question = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, parse_mc_questions, token_count, question = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, parse_mc_questions, token_count,
["questions"], ["questions"],
GEN_QUESTION_TEMPERATURE) GEN_QUESTION_TEMPERATURE)
return fix_exercise_ids(question, start_id) return {
"id": str(uuid.uuid4()),
"prompt": "Select the appropriate option.",
"questions": fix_exercise_ids(question, start_id)["questions"],
"type": "multipleChoice",
}
def gen_summary_fill_blanks_exercise(text: str, quantity: int, start_id): def gen_summary_fill_blanks_exercise(text: str, quantity: int, start_id):
gen_summary_for_text = "Summarize this text: " + text gen_summary_for_text = "Summarize this text: " + text
@@ -408,6 +428,7 @@ def gen_summary_fill_blanks_exercise(text: str, quantity: int, start_id):
} }
def gen_true_false_not_given_exercise(text: str, quantity: int, start_id): def gen_true_false_not_given_exercise(text: str, quantity: int, start_id):
gen_true_false_not_given = "Generate " + str( gen_true_false_not_given = "Generate " + str(
quantity) + " statements in JSON format (True, False, or Not Given) " \ quantity) + " statements in JSON format (True, False, or Not Given) " \
@@ -436,6 +457,7 @@ def gen_true_false_not_given_exercise(text: str, quantity: int, start_id):
"type": "trueFalse" "type": "trueFalse"
} }
def gen_write_blanks_exercise(text: str, quantity: int, start_id): def gen_write_blanks_exercise(text: str, quantity: int, start_id):
gen_short_answer_questions = "Generate " + str(quantity) + " short answer questions, and the possible answers " \ gen_short_answer_questions = "Generate " + str(quantity) + " short answer questions, and the possible answers " \
"(max 3 words per answer), about this text: '" + text + "'. " \ "(max 3 words per answer), about this text: '" + text + "'. " \
@@ -456,9 +478,11 @@ def gen_write_blanks_exercise(text: str, quantity: int, start_id):
"type": "writeBlanks" "type": "writeBlanks"
} }
def gen_multiple_choice_exercise_listening_conversation(text: str, quantity: int, start_id): def gen_multiple_choice_exercise_listening_conversation(text: str, quantity: int, start_id):
gen_multiple_choice_for_text = "Generate " + str(quantity) + " multiple choice questions of 4 options for this conversation: " \ gen_multiple_choice_for_text = "Generate " + str(
"'" + text + "'" quantity) + " multiple choice questions of 4 options for this conversation: " \
"'" + text + "'"
token_count = count_tokens(gen_multiple_choice_for_text)["n_tokens"] token_count = count_tokens(gen_multiple_choice_for_text)["n_tokens"]
mc_questions = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_multiple_choice_for_text, token_count, mc_questions = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_multiple_choice_for_text, token_count,
None, None,
@@ -472,7 +496,13 @@ def gen_multiple_choice_exercise_listening_conversation(text: str, quantity: int
question = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, parse_mc_questions, token_count, question = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, parse_mc_questions, token_count,
["questions"], ["questions"],
GEN_QUESTION_TEMPERATURE) GEN_QUESTION_TEMPERATURE)
return fix_exercise_ids(question, start_id) return {
"id": str(uuid.uuid4()),
"prompt": "Select the appropriate option.",
"questions": fix_exercise_ids(question, start_id)["questions"],
"type": "multipleChoice",
}
def gen_multiple_choice_exercise_listening_monologue(text: str, quantity: int, start_id): def gen_multiple_choice_exercise_listening_monologue(text: str, quantity: int, start_id):
gen_multiple_choice_for_text = "Generate " + str(quantity) + " multiple choice questions for this monologue: " \ gen_multiple_choice_for_text = "Generate " + str(quantity) + " multiple choice questions for this monologue: " \
@@ -490,7 +520,13 @@ def gen_multiple_choice_exercise_listening_monologue(text: str, quantity: int, s
question = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, parse_mc_questions, token_count, question = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, parse_mc_questions, token_count,
["questions"], ["questions"],
GEN_QUESTION_TEMPERATURE) GEN_QUESTION_TEMPERATURE)
return fix_exercise_ids(question, start_id) return {
"id": str(uuid.uuid4()),
"prompt": "Select the appropriate option.",
"questions": fix_exercise_ids(question, start_id)["questions"],
"type": "multipleChoice",
}
def gen_write_blanks_questions_exercise_listening_conversation(text: str, quantity: int, start_id): def gen_write_blanks_questions_exercise_listening_conversation(text: str, quantity: int, start_id):
gen_write_blanks_questions = "Generate " + str(quantity) + " short answer questions, and the possible answers " \ gen_write_blanks_questions = "Generate " + str(quantity) + " short answer questions, and the possible answers " \
@@ -513,6 +549,7 @@ def gen_write_blanks_questions_exercise_listening_conversation(text: str, quanti
"type": "writeBlanks" "type": "writeBlanks"
} }
def gen_write_blanks_questions_exercise_listening_monologue(text: str, quantity: int, start_id): def gen_write_blanks_questions_exercise_listening_monologue(text: str, quantity: int, start_id):
gen_write_blanks_questions = "Generate " + str(quantity) + " short answer questions, and the possible answers " \ gen_write_blanks_questions = "Generate " + str(quantity) + " short answer questions, and the possible answers " \
"(max 3 words per answer), about a monologue and" \ "(max 3 words per answer), about a monologue and" \
@@ -534,6 +571,7 @@ def gen_write_blanks_questions_exercise_listening_monologue(text: str, quantity:
"type": "writeBlanks" "type": "writeBlanks"
} }
def gen_write_blanks_notes_exercise_listening_conversation(text: str, quantity: int, start_id): def gen_write_blanks_notes_exercise_listening_conversation(text: str, quantity: int, start_id):
gen_write_blanks_notes = "Generate " + str(quantity) + " notes taken from the conversation and and respond in this " \ gen_write_blanks_notes = "Generate " + str(quantity) + " notes taken from the conversation and and respond in this " \
"JSON format: { \"notes\": [\"note_1\", \"note_2\"] }. The monologue is this: '" + text + "'" "JSON format: { \"notes\": [\"note_1\", \"note_2\"] }. The monologue is this: '" + text + "'"
@@ -558,9 +596,10 @@ def gen_write_blanks_notes_exercise_listening_conversation(text: str, quantity:
"type": "writeBlanks" "type": "writeBlanks"
} }
def gen_write_blanks_notes_exercise_listening_monologue(text: str, quantity: int, start_id): def gen_write_blanks_notes_exercise_listening_monologue(text: str, quantity: int, start_id):
gen_write_blanks_notes = "Generate " + str(quantity) + " notes taken from the monologue and and respond in this " \ gen_write_blanks_notes = "Generate " + str(quantity) + " notes taken from the monologue and and respond in this " \
"JSON format: { \"notes\": [\"note_1\", \"note_2\"] }. The monologue is this: '" + text + "'" "JSON format: { \"notes\": [\"note_1\", \"note_2\"] }. The monologue is this: '" + text + "'"
token_count = count_tokens(gen_write_blanks_notes)["n_tokens"] token_count = count_tokens(gen_write_blanks_notes)["n_tokens"]
questions = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_write_blanks_notes, token_count, questions = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_write_blanks_notes, token_count,
@@ -570,8 +609,8 @@ def gen_write_blanks_notes_exercise_listening_monologue(text: str, quantity: int
gen_words_to_replace = "Select 1 word from each phrase in the list and respond in this " \ gen_words_to_replace = "Select 1 word from each phrase in the list and respond in this " \
"JSON format: { \"words\": [\"word_1\", \"word_2\"] }. The phrases are: " + formatted_phrases "JSON format: { \"words\": [\"word_1\", \"word_2\"] }. The phrases are: " + formatted_phrases
words = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_words_to_replace, token_count, words = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_words_to_replace, token_count,
["words"], ["words"],
GEN_QUESTION_TEMPERATURE)["words"][:quantity] GEN_QUESTION_TEMPERATURE)["words"][:quantity]
replaced_notes = replace_first_occurrences_with_placeholders_notes(questions, words, start_id) replaced_notes = replace_first_occurrences_with_placeholders_notes(questions, words, start_id)
return { return {
"id": str(uuid.uuid4()), "id": str(uuid.uuid4()),
@@ -582,18 +621,19 @@ def gen_write_blanks_notes_exercise_listening_monologue(text: str, quantity: int
"type": "writeBlanks" "type": "writeBlanks"
} }
def gen_write_blanks_form_exercise_listening_conversation(text: str, quantity: int, start_id): def gen_write_blanks_form_exercise_listening_conversation(text: str, quantity: int, start_id):
gen_write_blanks_form = "Generate a form with " + str(quantity) + " key-value pairs about the conversation. " \ gen_write_blanks_form = "Generate a form with " + str(quantity) + " key-value pairs about the conversation. " \
"The conversation is this: '" + text + "'" "The conversation is this: '" + text + "'"
token_count = count_tokens(gen_write_blanks_form)["n_tokens"] token_count = count_tokens(gen_write_blanks_form)["n_tokens"]
form = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_write_blanks_form, token_count, form = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_write_blanks_form, token_count,
None, None,
GEN_QUESTION_TEMPERATURE) GEN_QUESTION_TEMPERATURE)
parse_form = "Parse the form to this JSON format: { \"form\": [\"string\", \"string\"] }. The form is this: '" + form + "'" parse_form = "Parse the form to this JSON format: { \"form\": [\"string\", \"string\"] }. The form is this: '" + form + "'"
token_count = count_tokens(parse_form)["n_tokens"] token_count = count_tokens(parse_form)["n_tokens"]
parsed_form = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, parse_form, token_count, parsed_form = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, parse_form, token_count,
["form"], ["form"],
GEN_QUESTION_TEMPERATURE)["form"][:quantity] GEN_QUESTION_TEMPERATURE)["form"][:quantity]
replaced_form, words = build_write_blanks_text_form(parsed_form, start_id) replaced_form, words = build_write_blanks_text_form(parsed_form, start_id)
return { return {
"id": str(uuid.uuid4()), "id": str(uuid.uuid4()),
@@ -604,18 +644,19 @@ def gen_write_blanks_form_exercise_listening_conversation(text: str, quantity: i
"type": "writeBlanks" "type": "writeBlanks"
} }
def gen_write_blanks_form_exercise_listening_monologue(text: str, quantity: int, start_id): def gen_write_blanks_form_exercise_listening_monologue(text: str, quantity: int, start_id):
gen_write_blanks_form = "Generate a form with " + str(quantity) + " key-value pairs about the monologue. " \ gen_write_blanks_form = "Generate a form with " + str(quantity) + " key-value pairs about the monologue. " \
"The monologue is this: '" + text + "'" "The monologue is this: '" + text + "'"
token_count = count_tokens(gen_write_blanks_form)["n_tokens"] token_count = count_tokens(gen_write_blanks_form)["n_tokens"]
form = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_write_blanks_form, token_count, form = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_write_blanks_form, token_count,
None, None,
GEN_QUESTION_TEMPERATURE) GEN_QUESTION_TEMPERATURE)
parse_form = "Parse the form to this JSON format: { \"form\": [\"string\", \"string\"] }. The form is this: '" + form + "'" parse_form = "Parse the form to this JSON format: { \"form\": [\"string\", \"string\"] }. The form is this: '" + form + "'"
token_count = count_tokens(parse_form)["n_tokens"] token_count = count_tokens(parse_form)["n_tokens"]
parsed_form = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, parse_form, token_count, parsed_form = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, parse_form, token_count,
["form"], ["form"],
GEN_QUESTION_TEMPERATURE)["form"][:quantity] GEN_QUESTION_TEMPERATURE)["form"][:quantity]
replaced_form, words = build_write_blanks_text_form(parsed_form, start_id) replaced_form, words = build_write_blanks_text_form(parsed_form, start_id)
return { return {
"id": str(uuid.uuid4()), "id": str(uuid.uuid4()),