64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
import random
|
|
import uuid
|
|
|
|
from ielts_be.configs.constants import GPTModels, TemperatureSettings
|
|
from ielts_be.helpers import ExercisesHelper
|
|
from ielts_be.services import ILLMService
|
|
|
|
|
|
class ParagraphMatch:
|
|
|
|
def __init__(self, llm: ILLMService):
|
|
self._llm = llm
|
|
|
|
async def gen_paragraph_match_exercise(self, text: str, quantity: int, start_id: int):
|
|
paragraphs = ExercisesHelper.assign_letters_to_paragraphs(text)
|
|
messages = [
|
|
{
|
|
"role": "system",
|
|
"content": (
|
|
'You are a helpful assistant designed to output JSON on this format: '
|
|
'{"headings": [ {"heading": "first paragraph heading"}, {"heading": "second paragraph heading"}]}'
|
|
)
|
|
},
|
|
{
|
|
"role": "user",
|
|
"content": (
|
|
'For every paragraph of the list generate a minimum 5 word heading for it. '
|
|
f'The paragraphs are these: {str(paragraphs)}'
|
|
)
|
|
|
|
}
|
|
]
|
|
|
|
response = await self._llm.prediction(
|
|
GPTModels.GPT_4_O, messages, ["headings"], TemperatureSettings.GEN_QUESTION_TEMPERATURE
|
|
)
|
|
headings = response["headings"]
|
|
|
|
options = []
|
|
for i, paragraph in enumerate(paragraphs, start=0):
|
|
paragraph["heading"] = headings[i]["heading"]
|
|
options.append({
|
|
"id": paragraph["letter"],
|
|
"sentence": paragraph["paragraph"]
|
|
})
|
|
|
|
random.shuffle(paragraphs)
|
|
sentences = []
|
|
for i, paragraph in enumerate(paragraphs, start=start_id):
|
|
sentences.append({
|
|
"id": i,
|
|
"sentence": paragraph["heading"],
|
|
"solution": paragraph["letter"]
|
|
})
|
|
|
|
return {
|
|
"id": str(uuid.uuid4()),
|
|
"allowRepetition": False,
|
|
"options": options,
|
|
"prompt": "Choose the correct heading for paragraphs from the list of headings below.",
|
|
"sentences": sentences[:quantity],
|
|
"type": "matchSentences"
|
|
}
|