File size: 2,601 Bytes
8e67692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from typing import Any

from graphgen.bases import BaseGenerator
from graphgen.templates import DESCRIPTION_REPHRASING_PROMPT
from graphgen.utils import detect_main_language, logger


class QuizGenerator(BaseGenerator):
    """
    Quiz Generator rephrases given descriptions to create quiz questions.
    """

    @staticmethod
    def build_prompt(
        batch: tuple[list[tuple[str, dict]], list[tuple[Any, Any, dict]]]
    ) -> str:
        """
        Build prompt for rephrasing the description.
        :param batch: A tuple containing (nodes, edges) where nodes/edges
                      contain description information
        :return: Prompt string
        """
        # Extract description from batch
        # For quiz generator, we expect a special format where
        # the description is passed as the first node's description
        nodes, edges = batch
        if nodes:
            description = nodes[0][1].get("description", "")
            template_type = nodes[0][1].get("template_type", "TEMPLATE")
        elif edges:
            description = edges[0][2].get("description", "")
            template_type = edges[0][2].get("template_type", "TEMPLATE")
        else:
            raise ValueError("Batch must contain at least one node or edge with description")

        return QuizGenerator.build_prompt_for_description(description, template_type)

    @staticmethod
    def build_prompt_for_description(description: str, template_type: str = "TEMPLATE") -> str:
        """
        Build prompt for rephrasing a single description.
        :param description: The description to rephrase
        :param template_type: Either "TEMPLATE" (same meaning) or "ANTI_TEMPLATE" (opposite meaning)
        :return: Prompt string
        """
        language = detect_main_language(description)
        prompt = DESCRIPTION_REPHRASING_PROMPT[language][template_type].format(
            input_sentence=description
        )
        return prompt

    @staticmethod
    def parse_rephrased_text(response: str) -> str:
        """
        Parse the rephrased text from the response.
        :param response:
        :return:
        """
        rephrased_text = response.strip().strip('"')
        logger.debug("Rephrased Text: %s", rephrased_text)
        return rephrased_text

    @staticmethod
    def parse_response(response: str) -> Any:
        """
        Parse the LLM response. For quiz generator, this returns the rephrased text.
        :param response: LLM response
        :return: Rephrased text
        """
        return QuizGenerator.parse_rephrased_text(response)