gllm-core-binary 0.4.4__py3-none-macosx_13_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gllm_core/__init__.py +1 -0
- gllm_core/__init__.pyi +0 -0
- gllm_core/adapters/__init__.py +5 -0
- gllm_core/adapters/__init__.pyi +3 -0
- gllm_core/adapters/tool/__init__.py +6 -0
- gllm_core/adapters/tool/__init__.pyi +4 -0
- gllm_core/adapters/tool/google_adk.py +91 -0
- gllm_core/adapters/tool/google_adk.pyi +23 -0
- gllm_core/adapters/tool/langchain.py +130 -0
- gllm_core/adapters/tool/langchain.pyi +31 -0
- gllm_core/constants.py +55 -0
- gllm_core/constants.pyi +36 -0
- gllm_core/event/__init__.py +6 -0
- gllm_core/event/__init__.pyi +4 -0
- gllm_core/event/event_emitter.py +211 -0
- gllm_core/event/event_emitter.pyi +155 -0
- gllm_core/event/handler/__init__.py +7 -0
- gllm_core/event/handler/__init__.pyi +5 -0
- gllm_core/event/handler/console_event_handler.py +48 -0
- gllm_core/event/handler/console_event_handler.pyi +32 -0
- gllm_core/event/handler/event_handler.py +89 -0
- gllm_core/event/handler/event_handler.pyi +51 -0
- gllm_core/event/handler/print_event_handler.py +130 -0
- gllm_core/event/handler/print_event_handler.pyi +33 -0
- gllm_core/event/handler/stream_event_handler.py +85 -0
- gllm_core/event/handler/stream_event_handler.pyi +62 -0
- gllm_core/event/hook/__init__.py +5 -0
- gllm_core/event/hook/__init__.pyi +3 -0
- gllm_core/event/hook/event_hook.py +30 -0
- gllm_core/event/hook/event_hook.pyi +18 -0
- gllm_core/event/hook/json_stringify_event_hook.py +32 -0
- gllm_core/event/hook/json_stringify_event_hook.pyi +16 -0
- gllm_core/event/messenger.py +133 -0
- gllm_core/event/messenger.pyi +66 -0
- gllm_core/schema/__init__.py +8 -0
- gllm_core/schema/__init__.pyi +6 -0
- gllm_core/schema/chunk.py +148 -0
- gllm_core/schema/chunk.pyi +66 -0
- gllm_core/schema/component.py +546 -0
- gllm_core/schema/component.pyi +205 -0
- gllm_core/schema/event.py +50 -0
- gllm_core/schema/event.pyi +33 -0
- gllm_core/schema/schema_generator.py +150 -0
- gllm_core/schema/schema_generator.pyi +35 -0
- gllm_core/schema/tool.py +418 -0
- gllm_core/schema/tool.pyi +198 -0
- gllm_core/utils/__init__.py +32 -0
- gllm_core/utils/__init__.pyi +13 -0
- gllm_core/utils/analyzer.py +256 -0
- gllm_core/utils/analyzer.pyi +123 -0
- gllm_core/utils/binary_handler_factory.py +99 -0
- gllm_core/utils/binary_handler_factory.pyi +62 -0
- gllm_core/utils/chunk_metadata_merger.py +102 -0
- gllm_core/utils/chunk_metadata_merger.pyi +41 -0
- gllm_core/utils/concurrency.py +184 -0
- gllm_core/utils/concurrency.pyi +94 -0
- gllm_core/utils/event_formatter.py +69 -0
- gllm_core/utils/event_formatter.pyi +30 -0
- gllm_core/utils/google_sheets.py +115 -0
- gllm_core/utils/google_sheets.pyi +18 -0
- gllm_core/utils/imports.py +91 -0
- gllm_core/utils/imports.pyi +42 -0
- gllm_core/utils/logger_manager.py +339 -0
- gllm_core/utils/logger_manager.pyi +176 -0
- gllm_core/utils/main_method_resolver.py +185 -0
- gllm_core/utils/main_method_resolver.pyi +54 -0
- gllm_core/utils/merger_method.py +130 -0
- gllm_core/utils/merger_method.pyi +49 -0
- gllm_core/utils/retry.py +258 -0
- gllm_core/utils/retry.pyi +41 -0
- gllm_core/utils/similarity.py +29 -0
- gllm_core/utils/similarity.pyi +10 -0
- gllm_core/utils/validation.py +26 -0
- gllm_core/utils/validation.pyi +12 -0
- gllm_core_binary-0.4.4.dist-info/METADATA +177 -0
- gllm_core_binary-0.4.4.dist-info/RECORD +78 -0
- gllm_core_binary-0.4.4.dist-info/WHEEL +5 -0
- gllm_core_binary-0.4.4.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from gllm_core.constants import EventLevel as EventLevel
|
|
3
|
+
from gllm_core.event.handler import ConsoleEventHandler as ConsoleEventHandler, PrintEventHandler as PrintEventHandler, StreamEventHandler as StreamEventHandler
|
|
4
|
+
from gllm_core.event.handler.event_handler import BaseEventHandler as BaseEventHandler
|
|
5
|
+
from gllm_core.event.hook.event_hook import BaseEventHook as BaseEventHook
|
|
6
|
+
from gllm_core.schema import Event as Event
|
|
7
|
+
from gllm_core.utils import validate_string_enum as validate_string_enum
|
|
8
|
+
from typing import AsyncGenerator
|
|
9
|
+
|
|
10
|
+
class EventEmitter:
|
|
11
|
+
'''Handles events emitting using event handlers with various levels and types.
|
|
12
|
+
|
|
13
|
+
The `EventEmitter` class is responsible for handling and emitting events using a list of event handlers.
|
|
14
|
+
Events are processed based on their severity level and type, with the option to disable specific handlers.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
handlers (list[BaseEventHandler]): A list of event handlers to process emitted events.
|
|
18
|
+
severity (int): The minimum severity level of events to be processed.
|
|
19
|
+
|
|
20
|
+
Examples:
|
|
21
|
+
Basic usage:
|
|
22
|
+
```python
|
|
23
|
+
event_emitter = EventEmitter(handlers=[ConsoleEventHandler()])
|
|
24
|
+
await event_emitter.emit("Hello, world!")
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Emitting an event object:
|
|
28
|
+
```python
|
|
29
|
+
event_emitter = EventEmitter(handlers=[ConsoleEventHandler()])
|
|
30
|
+
event = Event(id="123", value="Hello, world!", level=EventLevel.INFO, type="object", metadata={})
|
|
31
|
+
await event_emitter.emit(event)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Using with hooks:
|
|
35
|
+
```python
|
|
36
|
+
event_emitter = EventEmitter(handlers=[ConsoleEventHandler()], hooks=[JSONStringifyEventHook()])
|
|
37
|
+
await event_emitter.emit("Hello, world!")
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Using with customized event level:
|
|
41
|
+
```python
|
|
42
|
+
event_emitter = EventEmitter(handlers=[ConsoleEventHandler()], event_level=EventLevel.DEBUG)
|
|
43
|
+
await event_emitter.emit("Hello, world!")
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Using with console handler:
|
|
47
|
+
```python
|
|
48
|
+
event_emitter = EventEmitter.with_console_handler()
|
|
49
|
+
await event_emitter.emit("Hello, world!")
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Using with print handler:
|
|
53
|
+
```python
|
|
54
|
+
event_emitter = EventEmitter.with_print_handler()
|
|
55
|
+
await event_emitter.emit("Hello, world!")
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Using with stream handler:
|
|
59
|
+
```python
|
|
60
|
+
async def function(event_emitter: EventEmitter):
|
|
61
|
+
await event_emitter.emit("Hello, world!")
|
|
62
|
+
|
|
63
|
+
event_emitter = EventEmitter.with_stream_handler()
|
|
64
|
+
asyncio.create_task(function(event_emitter))
|
|
65
|
+
async for event in event_emitter.stream():
|
|
66
|
+
print(event)
|
|
67
|
+
```
|
|
68
|
+
'''
|
|
69
|
+
handlers: Incomplete
|
|
70
|
+
severity: Incomplete
|
|
71
|
+
hooks: Incomplete
|
|
72
|
+
def __init__(self, handlers: list[BaseEventHandler], event_level: EventLevel = ..., hooks: list[BaseEventHook] | None = None) -> None:
|
|
73
|
+
"""Initializes a new instance of the EventEmitter class.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
handlers (list[BaseEventHandler]): A list of event handlers to process emitted events.
|
|
77
|
+
event_level (EventLevel, optional): The minimum severity level of events to be processed.
|
|
78
|
+
Defaults to EventLevel.INFO.
|
|
79
|
+
hooks (list[BaseEventHook] | None, optional): A list of event hooks to be applied to the emitted events.
|
|
80
|
+
Defaults to None, in which case the event emitter will not apply any hooks.
|
|
81
|
+
|
|
82
|
+
Raises:
|
|
83
|
+
ValueError:
|
|
84
|
+
1. If the event handlers list is empty.
|
|
85
|
+
2. If an invalid event level is provided.
|
|
86
|
+
"""
|
|
87
|
+
@classmethod
|
|
88
|
+
def with_console_handler(cls, event_level: EventLevel = ..., hooks: list[BaseEventHook] | None = None) -> EventEmitter:
|
|
89
|
+
"""Creates a new instance of the EventEmitter class with a single ConsoleEventHandler.
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
event_level (EventLevel, optional): The minimum severity level of events to be processed.
|
|
93
|
+
Defaults to EventLevel.INFO.
|
|
94
|
+
hooks (list[BaseEventHook] | None, optional): A list of event hooks to be applied to the emitted events.
|
|
95
|
+
Defaults to None, in which case the event emitter will not apply any hooks.
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
EventEmitter: A new instance of the EventEmitter class with a single ConsoleEventHandler.
|
|
99
|
+
"""
|
|
100
|
+
@classmethod
|
|
101
|
+
def with_print_handler(cls, event_level: EventLevel = ..., hooks: list[BaseEventHook] | None = None) -> EventEmitter:
|
|
102
|
+
"""Creates a new instance of the EventEmitter class with a single PrintEventHandler.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
event_level (EventLevel, optional): The minimum severity level of events to be processed.
|
|
106
|
+
Defaults to EventLevel.INFO.
|
|
107
|
+
hooks (list[BaseEventHook] | None, optional): A list of event hooks to be applied to the emitted events.
|
|
108
|
+
Defaults to None, in which case the event emitter will not apply any hooks.
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
EventEmitter: A new instance of the EventEmitter class with a single PrintEventHandler.
|
|
112
|
+
"""
|
|
113
|
+
@classmethod
|
|
114
|
+
def with_stream_handler(cls, event_level: EventLevel = ..., hooks: list[BaseEventHook] | None = None) -> EventEmitter:
|
|
115
|
+
"""Creates a new instance of the EventEmitter class with a single StreamEventHandler.
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
event_level (EventLevel, optional): The minimum severity level of events to be processed.
|
|
119
|
+
Defaults to EventLevel.INFO.
|
|
120
|
+
hooks (list[BaseEventHook] | None, optional): A list of event hooks to be applied to the emitted events.
|
|
121
|
+
Defaults to None, in which case the event emitter will not apply any hooks.
|
|
122
|
+
|
|
123
|
+
Returns:
|
|
124
|
+
EventEmitter: A new instance of the EventEmitter class with a single StreamEventHandler.
|
|
125
|
+
"""
|
|
126
|
+
async def emit(self, event: Event, disabled_handlers: list[str] | None = None) -> None:
|
|
127
|
+
"""Emits an event using the configured event handlers.
|
|
128
|
+
|
|
129
|
+
Events are emitted by passing them to all handlers except those listed in disabled_handlers.
|
|
130
|
+
Events are only processed if their severity level meets or exceeds the EventEmitter's configured level.
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
event (Event): The event to emit.
|
|
134
|
+
disabled_handlers (list[str] | None, optional): Names of handlers to skip for this event. Defaults to None.
|
|
135
|
+
|
|
136
|
+
Raises:
|
|
137
|
+
ValueError: If the provided event_level is not a valid EventLevel.
|
|
138
|
+
"""
|
|
139
|
+
async def close(self) -> None:
|
|
140
|
+
"""Closes all handlers in the handler list.
|
|
141
|
+
|
|
142
|
+
This method iterates through the list of handlers and calls the `close` method on each handler.
|
|
143
|
+
|
|
144
|
+
Returns:
|
|
145
|
+
None
|
|
146
|
+
"""
|
|
147
|
+
def stream(self) -> AsyncGenerator:
|
|
148
|
+
"""Streams events from the StreamEventHandler.
|
|
149
|
+
|
|
150
|
+
This method is a convenience method that calls the `stream` method on the StreamEventHandler.
|
|
151
|
+
To use this method, the event emitter must have exactly one StreamEventHandler.
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
AsyncGenerator: An asynchronous generator yielding events from the StreamEventHandler.
|
|
155
|
+
"""
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""Modules concerning the event handler modules used throughout the Gen AI applications."""
|
|
2
|
+
|
|
3
|
+
from gllm_core.event.handler.console_event_handler import ConsoleEventHandler
|
|
4
|
+
from gllm_core.event.handler.print_event_handler import PrintEventHandler
|
|
5
|
+
from gllm_core.event.handler.stream_event_handler import StreamEventHandler
|
|
6
|
+
|
|
7
|
+
__all__ = ["ConsoleEventHandler", "PrintEventHandler", "StreamEventHandler"]
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
from gllm_core.event.handler.console_event_handler import ConsoleEventHandler as ConsoleEventHandler
|
|
2
|
+
from gllm_core.event.handler.print_event_handler import PrintEventHandler as PrintEventHandler
|
|
3
|
+
from gllm_core.event.handler.stream_event_handler import StreamEventHandler as StreamEventHandler
|
|
4
|
+
|
|
5
|
+
__all__ = ['ConsoleEventHandler', 'PrintEventHandler', 'StreamEventHandler']
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Defines an event handler class to print events to console.
|
|
2
|
+
|
|
3
|
+
Authors:
|
|
4
|
+
Henry Wicaksono (henry.wicaksono@gdplabs.id)
|
|
5
|
+
|
|
6
|
+
References:
|
|
7
|
+
NONE
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from rich.console import Console
|
|
11
|
+
|
|
12
|
+
from gllm_core.event.handler.event_handler import BaseEventHandler
|
|
13
|
+
from gllm_core.schema import Event
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ConsoleEventHandler(BaseEventHandler):
|
|
17
|
+
"""Defines an event handler class that prints events to the console.
|
|
18
|
+
|
|
19
|
+
Attributes:
|
|
20
|
+
name (str): The name assigned to the event handler.
|
|
21
|
+
color_map (dict[str, str]): The dictionary that maps certain event types to their
|
|
22
|
+
corresponding colors in Rich format.
|
|
23
|
+
console (Console): The Rich Console object to use for printing.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self, name: str | None = None, color_map: dict[str, str] | None = None) -> None:
|
|
27
|
+
"""Initializes a new instance of the ConsoleEventHandler class.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
name (str | None, optional): The name assigned to the event handler. Defaults to None,
|
|
31
|
+
in which case the class name will be used.
|
|
32
|
+
color_map (dict[str, str], optional): The dictionary that maps certain event types to their corresponding
|
|
33
|
+
colors in Rich format. Defaults to None, in which case the default color map will be used.
|
|
34
|
+
"""
|
|
35
|
+
super().__init__(name, color_map)
|
|
36
|
+
self.console = Console(highlight=False)
|
|
37
|
+
|
|
38
|
+
async def emit(self, event: Event) -> None:
|
|
39
|
+
"""Emits the given event by printing it to the console as a JSON string.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
event (Event): The event to be emitted.
|
|
43
|
+
|
|
44
|
+
Raises:
|
|
45
|
+
ValueError: If the event type is invalid.
|
|
46
|
+
"""
|
|
47
|
+
color = self._get_rich_color(event.type)
|
|
48
|
+
self.console.print(f"[{color}]{event.model_dump_json()}[/]")
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from gllm_core.event.handler.event_handler import BaseEventHandler as BaseEventHandler
|
|
3
|
+
from gllm_core.schema import Event as Event
|
|
4
|
+
|
|
5
|
+
class ConsoleEventHandler(BaseEventHandler):
|
|
6
|
+
"""Defines an event handler class that prints events to the console.
|
|
7
|
+
|
|
8
|
+
Attributes:
|
|
9
|
+
name (str): The name assigned to the event handler.
|
|
10
|
+
color_map (dict[str, str]): The dictionary that maps certain event types to their
|
|
11
|
+
corresponding colors in Rich format.
|
|
12
|
+
console (Console): The Rich Console object to use for printing.
|
|
13
|
+
"""
|
|
14
|
+
console: Incomplete
|
|
15
|
+
def __init__(self, name: str | None = None, color_map: dict[str, str] | None = None) -> None:
|
|
16
|
+
"""Initializes a new instance of the ConsoleEventHandler class.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
name (str | None, optional): The name assigned to the event handler. Defaults to None,
|
|
20
|
+
in which case the class name will be used.
|
|
21
|
+
color_map (dict[str, str], optional): The dictionary that maps certain event types to their corresponding
|
|
22
|
+
colors in Rich format. Defaults to None, in which case the default color map will be used.
|
|
23
|
+
"""
|
|
24
|
+
async def emit(self, event: Event) -> None:
|
|
25
|
+
"""Emits the given event by printing it to the console as a JSON string.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
event (Event): The event to be emitted.
|
|
29
|
+
|
|
30
|
+
Raises:
|
|
31
|
+
ValueError: If the event type is invalid.
|
|
32
|
+
"""
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""Defines an abstract base class for all event handlers used throughout the Gen AI applications.
|
|
2
|
+
|
|
3
|
+
Authors:
|
|
4
|
+
Henry Wicaksono (henry.wicaksono@gdplabs.id)
|
|
5
|
+
|
|
6
|
+
References:
|
|
7
|
+
NONE
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from abc import ABC, abstractmethod
|
|
11
|
+
|
|
12
|
+
from gllm_core.constants import EventType, EventTypeSuffix
|
|
13
|
+
from gllm_core.schema import Event
|
|
14
|
+
from gllm_core.utils import LoggerManager
|
|
15
|
+
|
|
16
|
+
DEFAULT_COLOR_MAP = {
|
|
17
|
+
EventType.ACTIVITY: "bright_blue",
|
|
18
|
+
EventType.CODE: "bright_red",
|
|
19
|
+
EventType.THINKING: "bright_yellow",
|
|
20
|
+
EventType.REFERENCE: "bright_green",
|
|
21
|
+
EventType.STATUS: "bright_black",
|
|
22
|
+
}
|
|
23
|
+
DEFAULT_COLOR = "white"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class BaseEventHandler(ABC):
|
|
27
|
+
"""An abstract base class for all event handlers used throughout the Gen AI applications.
|
|
28
|
+
|
|
29
|
+
Attributes:
|
|
30
|
+
name (str): The name assigned to the event handler.
|
|
31
|
+
color_map (dict[str, str]): The dictionary that maps certain event types to their
|
|
32
|
+
corresponding colors in Rich format.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(
|
|
36
|
+
self,
|
|
37
|
+
name: str | None = None,
|
|
38
|
+
color_map: dict[str, str] | None = None,
|
|
39
|
+
):
|
|
40
|
+
"""Initializes a new instance of the BaseEventHandler class.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
name (str | None, optional): The name assigned to the event handler. Defaults to None,
|
|
44
|
+
in which case the class name will be used.
|
|
45
|
+
color_map (dict[str, str], optional): The dictionary that maps certain event types to their corresponding
|
|
46
|
+
colors in Rich format. Defaults to None, in which case the default color map will be used.
|
|
47
|
+
"""
|
|
48
|
+
self.name = name or self.__class__.__name__
|
|
49
|
+
self.color_map = color_map or DEFAULT_COLOR_MAP
|
|
50
|
+
self._logger = LoggerManager().get_logger(self.name)
|
|
51
|
+
|
|
52
|
+
@abstractmethod
|
|
53
|
+
async def emit(self, event: Event) -> None:
|
|
54
|
+
"""Emits the given event.
|
|
55
|
+
|
|
56
|
+
This abstract method must be implemented by subclasses to define how an event is emitted.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
event (Event): The event to be emitted.
|
|
60
|
+
|
|
61
|
+
Raises:
|
|
62
|
+
NotImplementedError: If the method is not implemented in a subclass.
|
|
63
|
+
"""
|
|
64
|
+
raise NotImplementedError
|
|
65
|
+
|
|
66
|
+
async def close(self) -> None: # noqa: B027
|
|
67
|
+
"""Closes the event handler.
|
|
68
|
+
|
|
69
|
+
By default, this method does nothing. Subclasses can override this method to perform cleanup tasks
|
|
70
|
+
(e.g., closing connections or releasing resources) when needed. Event handlers that do not require
|
|
71
|
+
cleanup can inherit this default behavior without any changes.
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
None
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
def _get_rich_color(self, value: str) -> str:
|
|
78
|
+
"""Gets the assigned Rich color for the given value.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
value (str): The value to get the Rich color for.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
str: The Rich color for the given value.
|
|
85
|
+
"""
|
|
86
|
+
for suffix in EventTypeSuffix:
|
|
87
|
+
value = value.removesuffix(suffix)
|
|
88
|
+
|
|
89
|
+
return self.color_map.get(value, DEFAULT_COLOR)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import abc
|
|
2
|
+
from _typeshed import Incomplete
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from gllm_core.constants import EventType as EventType, EventTypeSuffix as EventTypeSuffix
|
|
5
|
+
from gllm_core.schema import Event as Event
|
|
6
|
+
from gllm_core.utils import LoggerManager as LoggerManager
|
|
7
|
+
|
|
8
|
+
DEFAULT_COLOR_MAP: Incomplete
|
|
9
|
+
DEFAULT_COLOR: str
|
|
10
|
+
|
|
11
|
+
class BaseEventHandler(ABC, metaclass=abc.ABCMeta):
|
|
12
|
+
"""An abstract base class for all event handlers used throughout the Gen AI applications.
|
|
13
|
+
|
|
14
|
+
Attributes:
|
|
15
|
+
name (str): The name assigned to the event handler.
|
|
16
|
+
color_map (dict[str, str]): The dictionary that maps certain event types to their
|
|
17
|
+
corresponding colors in Rich format.
|
|
18
|
+
"""
|
|
19
|
+
name: Incomplete
|
|
20
|
+
color_map: Incomplete
|
|
21
|
+
def __init__(self, name: str | None = None, color_map: dict[str, str] | None = None) -> None:
|
|
22
|
+
"""Initializes a new instance of the BaseEventHandler class.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
name (str | None, optional): The name assigned to the event handler. Defaults to None,
|
|
26
|
+
in which case the class name will be used.
|
|
27
|
+
color_map (dict[str, str], optional): The dictionary that maps certain event types to their corresponding
|
|
28
|
+
colors in Rich format. Defaults to None, in which case the default color map will be used.
|
|
29
|
+
"""
|
|
30
|
+
@abstractmethod
|
|
31
|
+
async def emit(self, event: Event) -> None:
|
|
32
|
+
"""Emits the given event.
|
|
33
|
+
|
|
34
|
+
This abstract method must be implemented by subclasses to define how an event is emitted.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
event (Event): The event to be emitted.
|
|
38
|
+
|
|
39
|
+
Raises:
|
|
40
|
+
NotImplementedError: If the method is not implemented in a subclass.
|
|
41
|
+
"""
|
|
42
|
+
async def close(self) -> None:
|
|
43
|
+
"""Closes the event handler.
|
|
44
|
+
|
|
45
|
+
By default, this method does nothing. Subclasses can override this method to perform cleanup tasks
|
|
46
|
+
(e.g., closing connections or releasing resources) when needed. Event handlers that do not require
|
|
47
|
+
cleanup can inherit this default behavior without any changes.
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
None
|
|
51
|
+
"""
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Defines an event handler class to print events with human readable format.
|
|
2
|
+
|
|
3
|
+
Authors:
|
|
4
|
+
Henry Wicaksono (henry.wicaksono@gdplabs.id)
|
|
5
|
+
|
|
6
|
+
References:
|
|
7
|
+
NONE
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from rich.console import Console
|
|
13
|
+
from rich.panel import Panel
|
|
14
|
+
from rich.style import Style
|
|
15
|
+
|
|
16
|
+
from gllm_core.constants import EventType, EventTypeSuffix
|
|
17
|
+
from gllm_core.event.handler.event_handler import BaseEventHandler
|
|
18
|
+
from gllm_core.schema import Event
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class PrintEventHandler(BaseEventHandler):
|
|
22
|
+
"""An event handler that prints the event with human readable format.
|
|
23
|
+
|
|
24
|
+
Attributes:
|
|
25
|
+
name (str): The name assigned to the event handler.
|
|
26
|
+
padding_char (str): The character to use for padding.
|
|
27
|
+
color_map (dict[str, str]): The dictionary that maps certain event types to their
|
|
28
|
+
corresponding colors in Rich format.
|
|
29
|
+
console (Console): The Rich Console object to use for printing.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
name: str | None = None,
|
|
35
|
+
color_map: dict[str, str] | None = None,
|
|
36
|
+
padding_char: str = "=",
|
|
37
|
+
):
|
|
38
|
+
"""Initializes a new instance of the PrintEventHandler class.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
name (str | None, optional): The name assigned to the event handler. Defaults to None,
|
|
42
|
+
in which case the class name will be used.
|
|
43
|
+
color_map (dict[str, str], optional): The dictionary that maps certain event types to their corresponding
|
|
44
|
+
colors in Rich format. Defaults to None, in which case the default color map will be used.
|
|
45
|
+
padding_char (str, optional): The character to use for padding. Defaults to "=".
|
|
46
|
+
"""
|
|
47
|
+
super().__init__(name, color_map)
|
|
48
|
+
self.padding_char = padding_char
|
|
49
|
+
self.console = Console(highlight=False)
|
|
50
|
+
|
|
51
|
+
async def emit(self, event: Event) -> None:
|
|
52
|
+
"""Emits the given event.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
event (Event): The event to be emitted.
|
|
56
|
+
"""
|
|
57
|
+
if event.type == EventType.STATUS:
|
|
58
|
+
self.console.print(f"[{event.level.name}][{event.timestamp}] {event.value}")
|
|
59
|
+
return
|
|
60
|
+
|
|
61
|
+
if event.type == EventType.ACTIVITY:
|
|
62
|
+
self._print_activity(event.value)
|
|
63
|
+
elif event.type.endswith(EventTypeSuffix.START) or event.type.endswith(EventTypeSuffix.END):
|
|
64
|
+
self._print_start_end(event.type)
|
|
65
|
+
else:
|
|
66
|
+
style = Style(color=self._get_rich_color(event.type))
|
|
67
|
+
self.console.print(event.value, end="", style=style)
|
|
68
|
+
|
|
69
|
+
def _print_activity(self, activity: dict[str, Any]) -> None:
|
|
70
|
+
"""Prints the activity.
|
|
71
|
+
|
|
72
|
+
This method prints the activity with the following format:
|
|
73
|
+
|
|
74
|
+
╭──────────────────╮
|
|
75
|
+
│ ACTIVITY │
|
|
76
|
+
╰──────────────────╯
|
|
77
|
+
>>> key1: value1
|
|
78
|
+
>>> key2: value2
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
activity (dict[str, Any]): The activity to print.
|
|
82
|
+
"""
|
|
83
|
+
color = self._get_rich_color(EventType.ACTIVITY)
|
|
84
|
+
panel = self._create_separator_panel(EventType.ACTIVITY, color)
|
|
85
|
+
self.console.print()
|
|
86
|
+
self.console.print(panel)
|
|
87
|
+
|
|
88
|
+
for key, value in activity.items():
|
|
89
|
+
self.console.print(f">>> {key}: {value}", style=Style(color=color))
|
|
90
|
+
|
|
91
|
+
def _print_start_end(self, event_type: str) -> None:
|
|
92
|
+
"""Prints the start or end of block based events.
|
|
93
|
+
|
|
94
|
+
This method prints the start or end of block based events with the following format:
|
|
95
|
+
|
|
96
|
+
╭─────────────────────╮
|
|
97
|
+
│ EVENT START │
|
|
98
|
+
╰─────────────────────╯
|
|
99
|
+
Event content...
|
|
100
|
+
╭───────────────────╮
|
|
101
|
+
│ EVENT END │
|
|
102
|
+
╰───────────────────╯
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
event_type (str): The type of the event.
|
|
106
|
+
"""
|
|
107
|
+
color = self._get_rich_color(event_type)
|
|
108
|
+
panel = self._create_separator_panel(event_type, color)
|
|
109
|
+
self.console.print()
|
|
110
|
+
self.console.print(panel)
|
|
111
|
+
|
|
112
|
+
def _create_separator_panel(self, value: str, color: str) -> Panel:
|
|
113
|
+
"""Creates a separator panel.
|
|
114
|
+
|
|
115
|
+
This method creates a separator Rich Panel with the following format:
|
|
116
|
+
|
|
117
|
+
╭───────────────╮
|
|
118
|
+
│ VALUE │
|
|
119
|
+
╰───────────────╯
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
value (str): The value to format.
|
|
123
|
+
color (str): The color of the separator.
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
Panel: The separator panel.
|
|
127
|
+
"""
|
|
128
|
+
value = value.replace("_", " ").upper().strip()
|
|
129
|
+
style = Style(color=color, bold=True)
|
|
130
|
+
return Panel.fit(value, style=style, border_style=style, padding=(0, 5))
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from gllm_core.constants import EventType as EventType, EventTypeSuffix as EventTypeSuffix
|
|
3
|
+
from gllm_core.event.handler.event_handler import BaseEventHandler as BaseEventHandler
|
|
4
|
+
from gllm_core.schema import Event as Event
|
|
5
|
+
|
|
6
|
+
class PrintEventHandler(BaseEventHandler):
|
|
7
|
+
"""An event handler that prints the event with human readable format.
|
|
8
|
+
|
|
9
|
+
Attributes:
|
|
10
|
+
name (str): The name assigned to the event handler.
|
|
11
|
+
padding_char (str): The character to use for padding.
|
|
12
|
+
color_map (dict[str, str]): The dictionary that maps certain event types to their
|
|
13
|
+
corresponding colors in Rich format.
|
|
14
|
+
console (Console): The Rich Console object to use for printing.
|
|
15
|
+
"""
|
|
16
|
+
padding_char: Incomplete
|
|
17
|
+
console: Incomplete
|
|
18
|
+
def __init__(self, name: str | None = None, color_map: dict[str, str] | None = None, padding_char: str = '=') -> None:
|
|
19
|
+
'''Initializes a new instance of the PrintEventHandler class.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
name (str | None, optional): The name assigned to the event handler. Defaults to None,
|
|
23
|
+
in which case the class name will be used.
|
|
24
|
+
color_map (dict[str, str], optional): The dictionary that maps certain event types to their corresponding
|
|
25
|
+
colors in Rich format. Defaults to None, in which case the default color map will be used.
|
|
26
|
+
padding_char (str, optional): The character to use for padding. Defaults to "=".
|
|
27
|
+
'''
|
|
28
|
+
async def emit(self, event: Event) -> None:
|
|
29
|
+
"""Emits the given event.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
event (Event): The event to be emitted.
|
|
33
|
+
"""
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""Defines an event handler class to stream events to clients using asynchronous queue.
|
|
2
|
+
|
|
3
|
+
Authors:
|
|
4
|
+
Henry Wicaksono (henry.wicaksono@gdplabs.id)
|
|
5
|
+
|
|
6
|
+
References:
|
|
7
|
+
NONE
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import asyncio
|
|
11
|
+
from typing import AsyncGenerator
|
|
12
|
+
|
|
13
|
+
from gllm_core.event.handler.event_handler import BaseEventHandler
|
|
14
|
+
from gllm_core.schema import Event
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class StreamEventHandler(BaseEventHandler):
|
|
18
|
+
"""A class that manages an asynchronous stream of data using a queue.
|
|
19
|
+
|
|
20
|
+
The StreamEventHandler class provides methods to manage an asynchronous stream, allowing data to be sent and
|
|
21
|
+
retrieved in a non-blocking manner. The stream method yields items from the queue, and the emit method adds
|
|
22
|
+
items to the queue. The stream can be closed by calling the close method, which ensures no further items
|
|
23
|
+
are processed.
|
|
24
|
+
|
|
25
|
+
Attributes:
|
|
26
|
+
name (str): The name assigned to the event handler.
|
|
27
|
+
color_map (dict[str, str]): The dictionary that maps certain event types to their
|
|
28
|
+
corresponding colors in Rich format.
|
|
29
|
+
queue (asyncio.Queue): The queue used to manage an asynchronous stream.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self, name: str | None = None, stream_delay: float = 0.001) -> None:
|
|
33
|
+
"""Initializes a new instance of the StreamEventHandler class.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
name (str | None, optional): The name assigned to the event handler. Defaults to None,
|
|
37
|
+
in which case the class name will be used.
|
|
38
|
+
stream_delay (float, optional): The delay duration after each data stream. Needed in order for the stream
|
|
39
|
+
manager to process the data stream properly. Defaults to 0.001.
|
|
40
|
+
"""
|
|
41
|
+
self.queue: asyncio.Queue = asyncio.Queue()
|
|
42
|
+
self.stream_delay = stream_delay
|
|
43
|
+
super().__init__(name)
|
|
44
|
+
|
|
45
|
+
async def emit(self, event: Event) -> None:
|
|
46
|
+
"""Emits the given event by sending it to the client via an asynchronous queue.
|
|
47
|
+
|
|
48
|
+
This method serializes the event to a JSON and sends it to the client by adding it to an asynchronous
|
|
49
|
+
queue. It also introduces a delay specified by `stream_delay` to make sure that the stream data can
|
|
50
|
+
be processed properly.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
event (Event): The event to be emitted.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
None
|
|
57
|
+
"""
|
|
58
|
+
await self.queue.put(event.model_dump_json())
|
|
59
|
+
await asyncio.sleep(self.stream_delay)
|
|
60
|
+
|
|
61
|
+
async def stream(self) -> AsyncGenerator:
|
|
62
|
+
"""Asynchronously yields items from the queue until a StopIteration item is encountered.
|
|
63
|
+
|
|
64
|
+
This method continuously retrieves items from the queue and yields them. The iteration stops when a
|
|
65
|
+
StopIteration item is encountered, at which point the method returns.
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
AsyncGenerator: An asynchronous generator yielding items from the queue.
|
|
69
|
+
"""
|
|
70
|
+
while True:
|
|
71
|
+
item = await self.queue.get()
|
|
72
|
+
if item is StopIteration:
|
|
73
|
+
return
|
|
74
|
+
yield item
|
|
75
|
+
|
|
76
|
+
async def close(self) -> None:
|
|
77
|
+
"""Immediately stops the stream by placing a StopIteration item in the queue.
|
|
78
|
+
|
|
79
|
+
This method inserts a StopIteration item into the queue without waiting, which signals the stream to stop
|
|
80
|
+
processing further items.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
None
|
|
84
|
+
"""
|
|
85
|
+
self.queue.put_nowait(StopIteration)
|