import random from app.configs.constants import GPTModels, TemperatureSettings, EducationalContent from app.services.abc import ILLMService class FillBlanks: def __init__(self, llm: ILLMService): self._llm = llm async def gen_fill_blanks( self, quantity: int, start_id: int, size: int, topic=None ): if not topic: topic = random.choice(EducationalContent.MTI_TOPICS) messages = [ { "role": "system", "content": f'You are a helpful assistant designed to output JSON on this format: {self._fill_blanks_mc_template()}' }, { "role": "user", "content": f'Generate a text of at least {size} words about the topic {topic}.' }, { "role": "user", "content": ( f'From the generated text choose {quantity} words (cannot be sequential words) to replace ' 'once with {{id}} where id starts on ' + str(start_id) + ' and is incremented for each word. ' 'The ids must be ordered throughout the text and the words must be replaced only once. ' 'For each removed word you will place it in the solutions array and assign a letter from A to D,' ' then you will place that removed word and the chosen letter on the words array along with ' ' other 3 other words for the remaining letter. This is a fill blanks question for an english ' 'exam, so don\'t choose words completely at random.' ) } ] question = await self._llm.prediction( GPTModels.GPT_4_O, messages, ["question"], TemperatureSettings.GEN_QUESTION_TEMPERATURE ) return { **question, "type": "fillBlanks", "variant": "mc", "prompt": "Click a blank to select the appropriate word for it.", } @staticmethod def _fill_blanks_mc_template(): return { "text": "", "solutions": [ { "id": "", "solution": "" } ], "words": [ { "id": "", "options": { "A": "", "B": "", "C": "", "D": "" } } ] }