Skip to content

Teach Service

Bases: Service

Service that generates educational lessons from documents.

Monitors document insertions and metadata completion to create learning materials based on document content.

Attributes:

Name Type Description
teach_generate_questions

Plugin for generating questions from documents.

meta_cache dict[str, Meta]

Cache for document metadata.

Methods:

Name Description
init_async

Initializes asynchronous components.

start

Starts the service and subscribes to relevant topics.

on_meta_complete

Handles metadata completion events.

on_document_inserted

Processes newly inserted documents to create lessons.

Source code in src/engramic/application/teach/teach_service.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class TeachService(Service):
    """
    Service that generates educational lessons from documents.

    Monitors document insertions and metadata completion to create learning
    materials based on document content.

    Attributes:
        teach_generate_questions: Plugin for generating questions from documents.
        meta_cache (dict[str, Meta]): Cache for document metadata.

    Methods:
        init_async() -> None:
            Initializes asynchronous components.
        start() -> None:
            Starts the service and subscribes to relevant topics.
        on_meta_complete(msg) -> None:
            Handles metadata completion events.
        on_document_inserted(msg) -> None:
            Processes newly inserted documents to create lessons.
    """

    def __init__(self, host: Host) -> None:
        """
        Initializes the TeachService.

        Args:
            host (Host): The host system providing access to plugins and services.
        """
        super().__init__(host)
        self.teach_generate_questions = host.plugin_manager.get_plugin('llm', 'teach_generate_questions')
        self.meta_cache: dict[str, Meta] = {}

    def init_async(self) -> None:
        """Initializes asynchronous components of the service."""
        return super().init_async()

    def start(self) -> None:
        """
        Starts the service and subscribes to relevant topics.

        Subscribes to META_COMPLETE and DOCUMENT_INSERTED topics to monitor
        document processing events.
        """
        self.subscribe(Service.Topic.META_COMPLETE, self.on_meta_complete)
        self.subscribe(Service.Topic.DOCUMENT_INSERTED, self.on_document_inserted)
        super().start()

    def on_meta_complete(self, msg: dict[Any, Any]) -> None:
        """
        Handles metadata completion events.

        Stores document metadata in the cache for later processing.

        Args:
            msg (dict[Any, Any]): The metadata message.
        """
        meta = Meta(**msg)
        if meta.type == meta.SourceType.DOCUMENT.value and meta.parent_id is not None:
            self.meta_cache[meta.parent_id] = meta

    def on_document_inserted(self, msg: dict[Any, Any]) -> None:
        """
        Processes newly inserted documents to create lessons.

        When a document is inserted and its metadata is available, creates
        a new lesson for the document.

        Args:
            msg (dict[Any, Any]): The document insertion message.
        """
        document_id = msg['id']
        if document_id in self.meta_cache:
            meta = self.meta_cache[document_id]
            lesson = Lesson(self, str(uuid.uuid4()), str(uuid.uuid4()), document_id)
            lesson.run_lesson(meta)
            del self.meta_cache[document_id]

__init__(host)

Initializes the TeachService.

Parameters:

Name Type Description Default
host Host

The host system providing access to plugins and services.

required
Source code in src/engramic/application/teach/teach_service.py
40
41
42
43
44
45
46
47
48
49
def __init__(self, host: Host) -> None:
    """
    Initializes the TeachService.

    Args:
        host (Host): The host system providing access to plugins and services.
    """
    super().__init__(host)
    self.teach_generate_questions = host.plugin_manager.get_plugin('llm', 'teach_generate_questions')
    self.meta_cache: dict[str, Meta] = {}

init_async()

Initializes asynchronous components of the service.

Source code in src/engramic/application/teach/teach_service.py
51
52
53
def init_async(self) -> None:
    """Initializes asynchronous components of the service."""
    return super().init_async()

on_document_inserted(msg)

Processes newly inserted documents to create lessons.

When a document is inserted and its metadata is available, creates a new lesson for the document.

Parameters:

Name Type Description Default
msg dict[Any, Any]

The document insertion message.

required
Source code in src/engramic/application/teach/teach_service.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def on_document_inserted(self, msg: dict[Any, Any]) -> None:
    """
    Processes newly inserted documents to create lessons.

    When a document is inserted and its metadata is available, creates
    a new lesson for the document.

    Args:
        msg (dict[Any, Any]): The document insertion message.
    """
    document_id = msg['id']
    if document_id in self.meta_cache:
        meta = self.meta_cache[document_id]
        lesson = Lesson(self, str(uuid.uuid4()), str(uuid.uuid4()), document_id)
        lesson.run_lesson(meta)
        del self.meta_cache[document_id]

on_meta_complete(msg)

Handles metadata completion events.

Stores document metadata in the cache for later processing.

Parameters:

Name Type Description Default
msg dict[Any, Any]

The metadata message.

required
Source code in src/engramic/application/teach/teach_service.py
66
67
68
69
70
71
72
73
74
75
76
77
def on_meta_complete(self, msg: dict[Any, Any]) -> None:
    """
    Handles metadata completion events.

    Stores document metadata in the cache for later processing.

    Args:
        msg (dict[Any, Any]): The metadata message.
    """
    meta = Meta(**msg)
    if meta.type == meta.SourceType.DOCUMENT.value and meta.parent_id is not None:
        self.meta_cache[meta.parent_id] = meta

start()

Starts the service and subscribes to relevant topics.

Subscribes to META_COMPLETE and DOCUMENT_INSERTED topics to monitor document processing events.

Source code in src/engramic/application/teach/teach_service.py
55
56
57
58
59
60
61
62
63
64
def start(self) -> None:
    """
    Starts the service and subscribes to relevant topics.

    Subscribes to META_COMPLETE and DOCUMENT_INSERTED topics to monitor
    document processing events.
    """
    self.subscribe(Service.Topic.META_COMPLETE, self.on_meta_complete)
    self.subscribe(Service.Topic.DOCUMENT_INSERTED, self.on_document_inserted)
    super().start()