Files
encoach_backend/helper/exercises.py
2023-10-19 23:39:45 +01:00

72 lines
4.2 KiB
Python

import queue
from helper.api_messages import QuestionType
from helper.openai_interface import make_openai_instruct_call
from helper.token_counter import count_tokens
from helper.constants import *
def divide_number_into_parts(number, parts):
if number < parts:
return None
part_size = number // parts
remaining = number % parts
q = queue.Queue()
for i in range(parts):
if i < remaining:
q.put(part_size + 1)
else:
q.put(part_size)
return q
def fix_exercise_ids(exercises):
# Initialize the starting ID for the first exercise
current_id = 1
# Iterate through exercises
for exercise in exercises:
questions = exercise["questions"]
# Iterate through questions and update the "id" value
for question in questions:
question["id"] = str(current_id)
current_id += 1
return exercises
def generate_reading_passage(type: QuestionType, topic: str):
gen_reading_passage_1 = "Generate an extensive text for IELTS " + type.READING_PASSAGE_1.value + ", of at least 1500 words, on the topic " \
"of " + topic + ". The passage should offer a substantial amount of " \
"information, analysis, or narrative " \
"relevant to the chosen subject matter. This text passage aims to serve as the primary reading " \
"section of an IELTS test, providing an in-depth and comprehensive exploration of the topic." \
"Provide your response in this json format: {'title': 'title of the text', 'text': 'generated text'}"
token_count = count_tokens(gen_reading_passage_1)["n_tokens"]
return make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_reading_passage_1, token_count, GEN_TEXT_FIELDS,
GEN_QUESTION_TEMPERATURE)
def gen_multiple_choice_exercise(text: str, quantity: int):
gen_multiple_choice_for_text = "Generate" + str(quantity) + "multiple choice questions for this text: " \
"'" + text + "'\n" \
"Use this format: 'questions': [{'id': '9', 'options': [{'id': 'A', 'text': " \
"'Economic benefits'}, {'id': 'B', 'text': 'Government regulations'}, {'id': 'C', 'text': " \
"'Concerns about climate change'}, {'id': 'D', 'text': 'Technological advancement'}], " \
"'prompt': 'What is the main reason for the shift towards renewable energy sources?', " \
"'solution': 'C', 'variant': 'text'}]"
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,
None,
GEN_QUESTION_TEMPERATURE)
parse_mc_questions = "Parse this '" + mc_questions + "' into this json format: 'questions': [{'id': '9', 'options': [{'id': 'A', 'text': " \
"'Economic benefits'}, {'id': 'B', 'text': 'Government regulations'}, {'id': 'C', 'text': " \
"'Concerns about climate change'}, {'id': 'D', 'text': 'Technological advancement'}], " \
"'prompt': 'What is the main reason for the shift towards renewable energy sources?', " \
"'solution': 'C', 'variant': 'text'}]"
token_count = count_tokens(parse_mc_questions)["n_tokens"]
return make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, parse_mc_questions, token_count,
["questions"],
GEN_QUESTION_TEMPERATURE)