-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] Support message logging (#267)
* add a message logging hook * update * change the default logger name --------- Co-authored-by: wangzy <[email protected]>
- Loading branch information
1 parent
5dabf3d
commit 248103b
Showing
6 changed files
with
104 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from .action_preprocessor import ActionPreprocessor, InternLMActionProcessor | ||
from .hook import Hook, RemovableHandle | ||
from .logger import MessageLogger | ||
|
||
__all__ = [ | ||
'Hook', 'RemovableHandle', 'ActionPreprocessor', 'InternLMActionProcessor' | ||
'Hook', 'RemovableHandle', 'ActionPreprocessor', 'InternLMActionProcessor', | ||
'MessageLogger' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import random | ||
from typing import Optional | ||
|
||
from termcolor import COLORS, colored | ||
|
||
from lagent.utils import get_logger | ||
from .hook import Hook | ||
|
||
|
||
class MessageLogger(Hook): | ||
|
||
def __init__(self, name: str = 'lagent'): | ||
self.logger = get_logger(name, 'info') | ||
self.sender2color = {} | ||
|
||
def before_agent(self, agent, messages, session_id): | ||
for message in messages: | ||
self._process_message(message, session_id) | ||
|
||
def after_agent(self, agent, message, session_id): | ||
self._process_message(message, session_id) | ||
|
||
def before_action(self, executor, message, session_id): | ||
self._process_message(message, session_id) | ||
|
||
def after_action(self, executor, message, session_id): | ||
self._process_message(message, session_id) | ||
|
||
def _process_message(self, message, session_id): | ||
sender = message.sender | ||
color = self.sender2color.setdefault(sender, | ||
random.choice(list(COLORS))) | ||
self.logger.info( | ||
colored( | ||
f'session id: {session_id}, message sender: {sender}\n' | ||
f'{message.content}', color)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
from .package import is_module_exist | ||
from .util import GeneratorWithReturn, async_as_completed, create_object, filter_suffix, load_class_from_string | ||
from .util import ( | ||
GeneratorWithReturn, | ||
async_as_completed, | ||
create_object, | ||
filter_suffix, | ||
get_logger, | ||
load_class_from_string, | ||
) | ||
|
||
__all__ = [ | ||
'is_module_exist', 'filter_suffix', 'create_object', | ||
'is_module_exist', 'filter_suffix', 'create_object', 'get_logger', | ||
'load_class_from_string', 'async_as_completed', 'GeneratorWithReturn' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters