Skip to content

lesson

Represents an educational lesson generated from a document.

Handles the generation of questions and educational prompts based on document content and metadata.

Attributes:

Name Type Description
id str

Unique identifier for the lesson.

doc_id str

Identifier of the source document.

tracking_id str

ID for tracking the lesson's progress.

service TeachService

Parent service managing this lesson.

meta Meta

Metadata about the source document.

Methods:

Name Description
run_lesson

Initiates the lesson generation process.

generate_questions

Generates educational questions based on document content.

on_questions_generated

Processes generated questions and creates prompts.

_on_send_prompt_complete

Handles completion of prompt submission.

Source code in src/engramic/application/teach/lesson.py
 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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
class Lesson:
    """
    Represents an educational lesson generated from a document.

    Handles the generation of questions and educational prompts based on
    document content and metadata.

    Attributes:
        id (str): Unique identifier for the lesson.
        doc_id (str): Identifier of the source document.
        tracking_id (str): ID for tracking the lesson's progress.
        service (TeachService): Parent service managing this lesson.
        meta (Meta): Metadata about the source document.

    Methods:
        run_lesson(meta_in) -> None:
            Initiates the lesson generation process.
        generate_questions() -> Any:
            Generates educational questions based on document content.
        on_questions_generated(future) -> None:
            Processes generated questions and creates prompts.
        _on_send_prompt_complete(ret) -> None:
            Handles completion of prompt submission.
    """

    def __init__(self, parent_service: TeachService, lesson_id: str, tracking_id: str, doc_id: str) -> None:
        """
        Initializes a new Lesson instance.

        Args:
            parent_service (TeachService): The service managing this lesson.
            lesson_id (str): Unique identifier for the lesson.
            tracking_id (str): ID for tracking the lesson's progress.
            doc_id (str): Identifier of the source document.
        """
        self.id = lesson_id
        self.doc_id = doc_id
        self.tracking_id = tracking_id
        self.service = parent_service

    def run_lesson(self, meta_in: Meta) -> None:
        """
        Initiates the lesson generation process.

        Stores the document metadata and starts the question generation task.

        Args:
            meta_in (Meta): Metadata about the source document.
        """
        self.meta = meta_in
        future = self.service.run_task(self.generate_questions())
        future.add_done_callback(self.on_questions_generated)

    async def generate_questions(self) -> Any:
        """
        Generates educational questions based on document content.

        Uses an LLM plugin to generate study questions from the document metadata.

        Returns:
            dict: The generated questions and study actions.
        """
        plugin = self.service.teach_generate_questions

        prompt = PromptGenQuestions(input_data={'meta': asdict(self.meta)})

        structured_response = {'study_actions': list[str]}

        ret = plugin['func'].submit(
            prompt=prompt,
            images=None,
            structured_schema=structured_response,
            args=self.service.host.mock_update_args(plugin),
        )

        self.service.host.update_mock_data(plugin, ret)

        initial_scan = json.loads(ret[0]['llm_response'])

        return initial_scan

    def on_questions_generated(self, future: Future[Any]) -> None:
        """
        Processes generated questions and creates learning prompts.

        Takes the questions from the generator and submits them as learning prompts.
        Also adds document-specific questions and notifies about lesson creation.

        Args:
            future (Future[Any]): Future containing the generated questions.
        """
        res = future.result()
        text_prompts = res['study_actions']

        if self.meta.type == self.meta.SourceType.DOCUMENT.value:
            location = self.meta.locations[0]

            # generate some static question for file discovery.
            text_prompts.append(f'Tell me about the file {location}')

        async def send_prompt(question: str) -> None:
            self.service.send_message_async(
                Service.Topic.SUBMIT_PROMPT,
                {
                    'prompt_str': question,
                    'parent_id': self.id,
                    'training_mode': True,
                    'is_lesson': True,
                    'tracking_id': self.tracking_id,
                    'repo_ids_filters': self.meta.repo_ids,
                },
            )

        for text_prompt in reversed(text_prompts):
            future = self.service.run_task(send_prompt(text_prompt))
            future.add_done_callback(self._on_send_prompt_complete)

        async def send_lesson() -> None:
            self.service.send_message_async(
                Service.Topic.LESSON_CREATED,
                {'id': self.id, 'tracking_id': self.tracking_id, 'doc_id': self.doc_id},
            )

        self.service.run_task(send_lesson())

    def _on_send_prompt_complete(self, ret: Future[Any]) -> None:
        """
        Handles completion of prompt submission.

        Args:
            ret (Future[Any]): Future representing the completed prompt submission.
        """
        ret.result()

__init__(parent_service, lesson_id, tracking_id, doc_id)

Initializes a new Lesson instance.

Parameters:

Name Type Description Default
parent_service TeachService

The service managing this lesson.

required
lesson_id str

Unique identifier for the lesson.

required
tracking_id str

ID for tracking the lesson's progress.

required
doc_id str

Identifier of the source document.

required
Source code in src/engramic/application/teach/lesson.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def __init__(self, parent_service: TeachService, lesson_id: str, tracking_id: str, doc_id: str) -> None:
    """
    Initializes a new Lesson instance.

    Args:
        parent_service (TeachService): The service managing this lesson.
        lesson_id (str): Unique identifier for the lesson.
        tracking_id (str): ID for tracking the lesson's progress.
        doc_id (str): Identifier of the source document.
    """
    self.id = lesson_id
    self.doc_id = doc_id
    self.tracking_id = tracking_id
    self.service = parent_service

generate_questions() async

Generates educational questions based on document content.

Uses an LLM plugin to generate study questions from the document metadata.

Returns:

Name Type Description
dict Any

The generated questions and study actions.

Source code in src/engramic/application/teach/lesson.py
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
async def generate_questions(self) -> Any:
    """
    Generates educational questions based on document content.

    Uses an LLM plugin to generate study questions from the document metadata.

    Returns:
        dict: The generated questions and study actions.
    """
    plugin = self.service.teach_generate_questions

    prompt = PromptGenQuestions(input_data={'meta': asdict(self.meta)})

    structured_response = {'study_actions': list[str]}

    ret = plugin['func'].submit(
        prompt=prompt,
        images=None,
        structured_schema=structured_response,
        args=self.service.host.mock_update_args(plugin),
    )

    self.service.host.update_mock_data(plugin, ret)

    initial_scan = json.loads(ret[0]['llm_response'])

    return initial_scan

on_questions_generated(future)

Processes generated questions and creates learning prompts.

Takes the questions from the generator and submits them as learning prompts. Also adds document-specific questions and notifies about lesson creation.

Parameters:

Name Type Description Default
future Future[Any]

Future containing the generated questions.

required
Source code in src/engramic/application/teach/lesson.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def on_questions_generated(self, future: Future[Any]) -> None:
    """
    Processes generated questions and creates learning prompts.

    Takes the questions from the generator and submits them as learning prompts.
    Also adds document-specific questions and notifies about lesson creation.

    Args:
        future (Future[Any]): Future containing the generated questions.
    """
    res = future.result()
    text_prompts = res['study_actions']

    if self.meta.type == self.meta.SourceType.DOCUMENT.value:
        location = self.meta.locations[0]

        # generate some static question for file discovery.
        text_prompts.append(f'Tell me about the file {location}')

    async def send_prompt(question: str) -> None:
        self.service.send_message_async(
            Service.Topic.SUBMIT_PROMPT,
            {
                'prompt_str': question,
                'parent_id': self.id,
                'training_mode': True,
                'is_lesson': True,
                'tracking_id': self.tracking_id,
                'repo_ids_filters': self.meta.repo_ids,
            },
        )

    for text_prompt in reversed(text_prompts):
        future = self.service.run_task(send_prompt(text_prompt))
        future.add_done_callback(self._on_send_prompt_complete)

    async def send_lesson() -> None:
        self.service.send_message_async(
            Service.Topic.LESSON_CREATED,
            {'id': self.id, 'tracking_id': self.tracking_id, 'doc_id': self.doc_id},
        )

    self.service.run_task(send_lesson())

run_lesson(meta_in)

Initiates the lesson generation process.

Stores the document metadata and starts the question generation task.

Parameters:

Name Type Description Default
meta_in Meta

Metadata about the source document.

required
Source code in src/engramic/application/teach/lesson.py
65
66
67
68
69
70
71
72
73
74
75
76
def run_lesson(self, meta_in: Meta) -> None:
    """
    Initiates the lesson generation process.

    Stores the document metadata and starts the question generation task.

    Args:
        meta_in (Meta): Metadata about the source document.
    """
    self.meta = meta_in
    future = self.service.run_task(self.generate_questions())
    future.add_done_callback(self.on_questions_generated)