From 6b699c94e31030523c9b8371edec92e49732c4d7 Mon Sep 17 00:00:00 2001 From: Paulo V C Medeiros Date: Fri, 10 Nov 2023 10:01:15 +0100 Subject: [PATCH] Use logger instead of print where applicable --- gpt_buddy_bot/chat.py | 5 ++++- gpt_buddy_bot/command_definitions.py | 4 +++- gpt_buddy_bot/openai_utils.py | 9 ++++++--- pyproject.toml | 3 ++- tests/conftest.py | 4 +++- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/gpt_buddy_bot/chat.py b/gpt_buddy_bot/chat.py index 4ea7bc7..15acad2 100644 --- a/gpt_buddy_bot/chat.py +++ b/gpt_buddy_bot/chat.py @@ -6,6 +6,8 @@ from collections import defaultdict from pathlib import Path +from loguru import logger + from . import GeneralConstants from .chat_configs import ChatOptions from .chat_context import EmbeddingBasedChatContext, FullHistoryChatContext @@ -275,7 +277,8 @@ def start(self): print() print() except (KeyboardInterrupt, EOFError): - print("Exiting chat.") + print("", end="\r") + logger.info("Exiting chat.") def report_token_usage(self, current_chat: bool = True): """Report token usage and associated costs.""" diff --git a/gpt_buddy_bot/command_definitions.py b/gpt_buddy_bot/command_definitions.py index 98f1fde..f2de117 100644 --- a/gpt_buddy_bot/command_definitions.py +++ b/gpt_buddy_bot/command_definitions.py @@ -2,6 +2,8 @@ """Commands supported by the package's script.""" import subprocess +from loguru import logger + from . import GeneralConstants from .chat import Chat from .chat_configs import ChatOptions @@ -36,4 +38,4 @@ def run_on_ui(args): check=True, ) except (KeyboardInterrupt, EOFError): - print("Exiting.") + logger.info("Exiting.") diff --git a/gpt_buddy_bot/openai_utils.py b/gpt_buddy_bot/openai_utils.py index 579e18c..9a05e90 100644 --- a/gpt_buddy_bot/openai_utils.py +++ b/gpt_buddy_bot/openai_utils.py @@ -5,6 +5,7 @@ from typing import TYPE_CHECKING import openai +from loguru import logger from .chat_configs import OpenAiApiCallOptions @@ -26,9 +27,11 @@ def retry_api_call(max_n_attempts=5, auth_error_msg="Problems connecting to Open def on_error(error, n_attempts): if n_attempts < max_n_attempts: - print( - f"\n > {error}. " - + f"Making new attempt ({n_attempts+1}/{max_n_attempts})..." + logger.warning( + " > {}. Making new attempt ({}/{})...", + error, + n_attempts + 1, + max_n_attempts, ) time.sleep(1) else: diff --git a/pyproject.toml b/pyproject.toml index b13bf43..3993d1c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ scikit-learn = "^1.3.2" scipy = "^1.11.3" # Other dependencies + loguru = "^0.7.2" numpy = "^1.26.1" openai = "^0.28.1" pandas = "^2.1.2" @@ -44,12 +45,12 @@ pytest = "^7.4.3" pytest-cov = "^4.1.0" pytest-mock = "^3.12.0" + pytest-order = "^1.1.0" python-lorem = "^1.3.0.post1" ################## # Linter configs # ################## - pytest-order = "^1.1.0" [tool.black] line-length = 90 diff --git a/tests/conftest.py b/tests/conftest.py index 3e54dd5..8bad2b4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -83,7 +83,9 @@ def _mock_input(*args, **kwargs): # noqa: ARG001 raise KeyboardInterrupt return user_input - mocker.patch("builtins.input", new=lambda _: _mock_input(user_input=user_input)) + mocker.patch( # noqa: PT008 + "builtins.input", new=lambda _: _mock_input(user_input=user_input) + ) @pytest.fixture(params=ChatOptions.get_allowed_values("model"))