From fe383863154ee13c1c9226b7c73082e27896f58d Mon Sep 17 00:00:00 2001 From: James Braza Date: Wed, 25 Sep 2024 17:41:45 -0700 Subject: [PATCH 1/4] Added rich to mypy dependencies --- .pre-commit-config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0f38755b..19d9b9af 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -92,6 +92,7 @@ repos: - pandas-stubs - pydantic~=2.0 # Match pyproject.toml - pydantic-settings + - rich - tantivy - tenacity - tiktoken>=0.4.0 # Match pyproject.toml From fe105b9f20177636e4392dbc9368a937d719feab Mon Sep 17 00:00:00 2001 From: James Braza Date: Wed, 25 Sep 2024 11:49:07 -0700 Subject: [PATCH 2/4] Exposed and organized log verbosity map as a module-level variable --- paperqa/agents/__init__.py | 75 ++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/paperqa/agents/__init__.py b/paperqa/agents/__init__.py index b6b6f86b..d6ab2c1d 100644 --- a/paperqa/agents/__init__.py +++ b/paperqa/agents/__init__.py @@ -19,48 +19,45 @@ logger = logging.getLogger(__name__) +LOG_VERBOSITY_MAP = { + 0: { + "paperqa.agents": logging.INFO, + "paperqa.agents.helpers": logging.WARNING, + "paperqa.agents.main": logging.WARNING, + "paperqa.agents.main.agent_callers": logging.INFO, + "paperqa.agents.models": logging.WARNING, + "paperqa.agents.search": logging.INFO, + "anthropic": logging.WARNING, + "openai": logging.WARNING, + "httpx": logging.WARNING, + "LiteLLM": logging.WARNING, + "LiteLLM Router": logging.WARNING, + "LiteLLM Proxy": logging.WARNING, + } +} +LOG_VERBOSITY_MAP[1] = LOG_VERBOSITY_MAP[0] | { + "paperqa.models": logging.INFO, + "paperqa.agents.main": logging.INFO, +} +LOG_VERBOSITY_MAP[2] = LOG_VERBOSITY_MAP[1] | { + "paperqa.models": logging.DEBUG, + "paperqa.agents.helpers": logging.DEBUG, + "paperqa.agents.main": logging.DEBUG, + "paperqa.agents.main.agent_callers": logging.DEBUG, + "paperqa.agents.search": logging.DEBUG, + "LiteLLM": logging.INFO, + "LiteLLM Router": logging.INFO, + "LiteLLM Proxy": logging.INFO, +} +LOG_VERBOSITY_MAP[3] = LOG_VERBOSITY_MAP[2] | { + "LiteLLM": logging.DEBUG, # <-- every single LLM call +} + def configure_cli_logging(verbosity: int = 0) -> None: """Suppress loquacious loggers according to verbosity level.""" setup_default_logs() - verbosity_map = { - 0: { - "paperqa.agents": logging.INFO, - "paperqa.agents.helpers": logging.WARNING, - "paperqa.agents.main": logging.WARNING, - "paperqa.agents.main.agent_callers": logging.INFO, - "anthropic": logging.WARNING, - "openai": logging.WARNING, - "httpx": logging.WARNING, - "paperqa.agents.models": logging.WARNING, - "paperqa.agents.search": logging.INFO, - "litellm": logging.WARNING, - "LiteLLM Router": logging.WARNING, - "LiteLLM Proxy": logging.WARNING, - } - } - - verbosity_map[1] = verbosity_map[0] | { - "paperqa.agents.main": logging.INFO, - "paperqa.models": logging.INFO, - } - - verbosity_map[2] = verbosity_map[1] | { - "paperqa.agents.helpers": logging.DEBUG, - "paperqa.agents.main": logging.DEBUG, - "paperqa.agents.main.agent_callers": logging.DEBUG, - "paperqa.models": logging.DEBUG, - "paperqa.agents.search": logging.DEBUG, - "litellm": logging.INFO, - "LiteLLM Router": logging.INFO, - "LiteLLM Proxy": logging.INFO, - } - - verbosity_map[3] = verbosity_map[2] | { - "litellm": logging.DEBUG, # <-- every single LLM call - } - rich_handler = RichHandler( rich_tracebacks=True, markup=True, @@ -68,17 +65,15 @@ def configure_cli_logging(verbosity: int = 0) -> None: show_level=False, console=Console(force_terminal=True), ) - rich_handler.setFormatter(logging.Formatter("%(message)s", datefmt="[%X]")) module_logger = logging.getLogger(__name__.split(".", maxsplit=1)[0]) - if not any(isinstance(h, RichHandler) for h in module_logger.handlers): module_logger.addHandler(rich_handler) for logger_name, logger_ in logging.Logger.manager.loggerDict.items(): if isinstance(logger_, logging.Logger) and ( - log_level := verbosity_map.get(min(verbosity, 2), {}).get(logger_name) + log_level := LOG_VERBOSITY_MAP.get(min(verbosity, 2), {}).get(logger_name) ): logger_.setLevel(log_level) From 01af51bbafd1d3ec993baaa136369cd57224d47e Mon Sep 17 00:00:00 2001 From: James Braza Date: Wed, 25 Sep 2024 16:09:07 -0700 Subject: [PATCH 3/4] Decomposed set_up_rich_handler function --- paperqa/agents/__init__.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/paperqa/agents/__init__.py b/paperqa/agents/__init__.py index d6ab2c1d..9a895636 100644 --- a/paperqa/agents/__init__.py +++ b/paperqa/agents/__init__.py @@ -53,11 +53,16 @@ "LiteLLM": logging.DEBUG, # <-- every single LLM call } +_PAPERQA_ROOT_LOGGER = logging.getLogger(__name__.split(".", maxsplit=1)[0]) + + +def is_running_under_cli() -> bool: + """Check if the current Python process comes from the CLI.""" + return any(isinstance(h, RichHandler) for h in _PAPERQA_ROOT_LOGGER.handlers) -def configure_cli_logging(verbosity: int = 0) -> None: - """Suppress loquacious loggers according to verbosity level.""" - setup_default_logs() +def set_up_rich_handler() -> RichHandler: + """Add a RichHandler to the paper-qa "root" logger, and return it.""" rich_handler = RichHandler( rich_tracebacks=True, markup=True, @@ -66,10 +71,15 @@ def configure_cli_logging(verbosity: int = 0) -> None: console=Console(force_terminal=True), ) rich_handler.setFormatter(logging.Formatter("%(message)s", datefmt="[%X]")) + if not is_running_under_cli(): + _PAPERQA_ROOT_LOGGER.addHandler(rich_handler) + return rich_handler - module_logger = logging.getLogger(__name__.split(".", maxsplit=1)[0]) - if not any(isinstance(h, RichHandler) for h in module_logger.handlers): - module_logger.addHandler(rich_handler) + +def configure_cli_logging(verbosity: int = 0) -> None: + """Suppress loquacious loggers according to verbosity level.""" + setup_default_logs() + set_up_rich_handler() for logger_name, logger_ in logging.Logger.manager.loggerDict.items(): if isinstance(logger_, logging.Logger) and ( From 57b57f3cfd712915aee378d11f26bc42beefacc3 Mon Sep 17 00:00:00 2001 From: James Braza Date: Thu, 26 Sep 2024 22:35:24 -0700 Subject: [PATCH 4/4] Made max pre-set verbosity level per PR comment --- paperqa/agents/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/paperqa/agents/__init__.py b/paperqa/agents/__init__.py index 9a895636..718819ca 100644 --- a/paperqa/agents/__init__.py +++ b/paperqa/agents/__init__.py @@ -81,9 +81,12 @@ def configure_cli_logging(verbosity: int = 0) -> None: setup_default_logs() set_up_rich_handler() + max_preset_verbosity: int = max(list(LOG_VERBOSITY_MAP.keys())) for logger_name, logger_ in logging.Logger.manager.loggerDict.items(): if isinstance(logger_, logging.Logger) and ( - log_level := LOG_VERBOSITY_MAP.get(min(verbosity, 2), {}).get(logger_name) + log_level := LOG_VERBOSITY_MAP.get( + min(verbosity, max_preset_verbosity), {} + ).get(logger_name) ): logger_.setLevel(log_level)