From 354e5407e1868405facc5b9b5d046797eeb13c7b Mon Sep 17 00:00:00 2001 From: Jack Gerrits Date: Mon, 18 Mar 2024 12:14:02 -0400 Subject: [PATCH 1/6] Encapsulate colored into a module --- autogen/agentchat/chat.py | 2 +- .../contrib/capabilities/teachability.py | 2 +- .../agentchat/contrib/compressible_agent.py | 8 +-- autogen/agentchat/contrib/llava_agent.py | 2 +- .../contrib/retrieve_user_proxy_agent.py | 2 +- autogen/agentchat/conversable_agent.py | 9 +-- autogen/formatting_utils.py | 72 +++++++++++++++++++ .../capabilities/chat_with_teachable_agent.py | 2 +- .../capabilities/test_teachable_agent.py | 2 +- 9 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 autogen/formatting_utils.py diff --git a/autogen/agentchat/chat.py b/autogen/agentchat/chat.py index bd4559ff425c..d5e127e971f3 100644 --- a/autogen/agentchat/chat.py +++ b/autogen/agentchat/chat.py @@ -7,7 +7,7 @@ from .utils import consolidate_chat_info import datetime import warnings -from termcolor import colored +from ..formatting_utils import colored logger = logging.getLogger(__name__) diff --git a/autogen/agentchat/contrib/capabilities/teachability.py b/autogen/agentchat/contrib/capabilities/teachability.py index 9e18f99a3454..58ba35ed4256 100644 --- a/autogen/agentchat/contrib/capabilities/teachability.py +++ b/autogen/agentchat/contrib/capabilities/teachability.py @@ -6,7 +6,7 @@ from autogen.agentchat.assistant_agent import ConversableAgent from autogen.agentchat.contrib.capabilities.agent_capability import AgentCapability from autogen.agentchat.contrib.text_analyzer_agent import TextAnalyzerAgent -from autogen.agentchat.conversable_agent import colored +from ....formatting_utils import colored class Teachability(AgentCapability): diff --git a/autogen/agentchat/contrib/compressible_agent.py b/autogen/agentchat/contrib/compressible_agent.py index 0264d265654e..152cc871a56a 100644 --- a/autogen/agentchat/contrib/compressible_agent.py +++ b/autogen/agentchat/contrib/compressible_agent.py @@ -7,13 +7,7 @@ import inspect from autogen.token_count_utils import count_token, get_max_token_limit, num_tokens_from_functions -try: - from termcolor import colored -except ImportError: - - def colored(x, *args, **kwargs): - return x - +from ...formatting_utils import colored logger = logging.getLogger(__name__) diff --git a/autogen/agentchat/contrib/llava_agent.py b/autogen/agentchat/contrib/llava_agent.py index c26f576ab398..182f72837b75 100644 --- a/autogen/agentchat/contrib/llava_agent.py +++ b/autogen/agentchat/contrib/llava_agent.py @@ -8,7 +8,7 @@ from autogen.agentchat.contrib.img_utils import get_image_data, llava_formatter from autogen.agentchat.contrib.multimodal_conversable_agent import MultimodalConversableAgent from autogen.code_utils import content_str -from autogen.agentchat.conversable_agent import colored +from ...formatting_utils import colored logger = logging.getLogger(__name__) diff --git a/autogen/agentchat/contrib/retrieve_user_proxy_agent.py b/autogen/agentchat/contrib/retrieve_user_proxy_agent.py index 5d9657d81431..f252f60e5ec1 100644 --- a/autogen/agentchat/contrib/retrieve_user_proxy_agent.py +++ b/autogen/agentchat/contrib/retrieve_user_proxy_agent.py @@ -12,7 +12,7 @@ from autogen.token_count_utils import count_token from autogen.code_utils import extract_code from autogen import logger -from autogen.agentchat.conversable_agent import colored +from ...formatting_utils import colored PROMPT_DEFAULT = """You're a retrieve augmented chatbot. You answer user's questions based on your own knowledge and the diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 838af6e7df70..accb2c54d9b3 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -15,6 +15,7 @@ from ..coding.base import CodeExecutor from ..coding.factory import CodeExecutorFactory +from ..formatting_utils import colored from ..oai.client import OpenAIWrapper, ModelClient from ..runtime_logging import logging_enabled, log_new_agent @@ -36,14 +37,6 @@ from .agent import Agent, LLMAgent from .._pydantic import model_dump -try: - from termcolor import colored -except ImportError: - - def colored(x, *args, **kwargs): - return x - - __all__ = ("ConversableAgent",) logger = logging.getLogger(__name__) diff --git a/autogen/formatting_utils.py b/autogen/formatting_utils.py new file mode 100644 index 000000000000..9e17e582aa9e --- /dev/null +++ b/autogen/formatting_utils.py @@ -0,0 +1,72 @@ +from __future__ import annotations + +from typing import Iterable, Literal + +Attribute = Literal[ + "bold", + "dark", + "underline", + "blink", + "reverse", + "concealed", +] + +Highlight = Literal[ + "on_black", + "on_grey", + "on_red", + "on_green", + "on_yellow", + "on_blue", + "on_magenta", + "on_cyan", + "on_light_grey", + "on_dark_grey", + "on_light_red", + "on_light_green", + "on_light_yellow", + "on_light_blue", + "on_light_magenta", + "on_light_cyan", + "on_white", +] + +Color = Literal[ + "black", + "grey", + "red", + "green", + "yellow", + "blue", + "magenta", + "cyan", + "light_grey", + "dark_grey", + "light_red", + "light_green", + "light_yellow", + "light_blue", + "light_magenta", + "light_cyan", + "white", +] + + +# termcolor is an optional dependency - if it cannot be imported then no color is used. +# Alternatively the envvar NO_COLOR can be used to disable color. +# To allow for proper typing and for termcolor to be optional we need to re-define the types used in the lib here. +# This is the direct function definition from termcolor. +def colored( + text: object, + color: Color | None = None, + on_color: Highlight | None = None, + attrs: Iterable[Attribute] | None = None, + *, + no_color: bool | None = None, + force_color: bool | None = None, +) -> str: + try: + from termcolor import colored + return colored(text=text, color=color, on_color=on_color, attrs=attrs, no_color=no_color, force_color=force_color) + except ImportError: + return text \ No newline at end of file diff --git a/test/agentchat/contrib/capabilities/chat_with_teachable_agent.py b/test/agentchat/contrib/capabilities/chat_with_teachable_agent.py index 66d61386d615..27f030045a75 100755 --- a/test/agentchat/contrib/capabilities/chat_with_teachable_agent.py +++ b/test/agentchat/contrib/capabilities/chat_with_teachable_agent.py @@ -2,10 +2,10 @@ import os import sys -from termcolor import colored from autogen import UserProxyAgent, config_list_from_json from autogen.agentchat.contrib.capabilities.teachability import Teachability from autogen import ConversableAgent +from autogen.formatting_utils import colored sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) from test_assistant_agent import OAI_CONFIG_LIST, KEY_LOC # noqa: E402 diff --git a/test/agentchat/contrib/capabilities/test_teachable_agent.py b/test/agentchat/contrib/capabilities/test_teachable_agent.py index 44904b26d362..e9fdf68c813c 100755 --- a/test/agentchat/contrib/capabilities/test_teachable_agent.py +++ b/test/agentchat/contrib/capabilities/test_teachable_agent.py @@ -3,7 +3,7 @@ import pytest import os import sys -from termcolor import colored +from autogen.formatting_utils import colored from autogen import ConversableAgent, config_list_from_json sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) From 3db3e3348ad97818060fee961178fc4780210d87 Mon Sep 17 00:00:00 2001 From: Jack Gerrits Date: Mon, 18 Mar 2024 12:18:34 -0400 Subject: [PATCH 2/6] lint fix --- autogen/formatting_utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/autogen/formatting_utils.py b/autogen/formatting_utils.py index 9e17e582aa9e..8777b0aab8c0 100644 --- a/autogen/formatting_utils.py +++ b/autogen/formatting_utils.py @@ -67,6 +67,9 @@ def colored( ) -> str: try: from termcolor import colored - return colored(text=text, color=color, on_color=on_color, attrs=attrs, no_color=no_color, force_color=force_color) + + return colored( + text=text, color=color, on_color=on_color, attrs=attrs, no_color=no_color, force_color=force_color + ) except ImportError: - return text \ No newline at end of file + return text From 9c8d424adc1f8e6c608a4c3eb776e6e019bcc39a Mon Sep 17 00:00:00 2001 From: Jack Gerrits Date: Mon, 18 Mar 2024 12:18:59 -0400 Subject: [PATCH 3/6] add missing file --- website/process_notebooks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/process_notebooks.py b/website/process_notebooks.py index 04b5da699036..1d57184032b3 100755 --- a/website/process_notebooks.py +++ b/website/process_notebooks.py @@ -17,7 +17,7 @@ from typing import Dict, Optional, Tuple, Union from dataclasses import dataclass from multiprocessing import current_process -from termcolor import colored +from autogen.formatting_utils import colored try: import yaml From b1dbabaceff91ce13ef588d8bf6ee4254070ac9d Mon Sep 17 00:00:00 2001 From: Jack Gerrits Date: Mon, 18 Mar 2024 12:30:49 -0400 Subject: [PATCH 4/6] undo change --- website/process_notebooks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/process_notebooks.py b/website/process_notebooks.py index 1d57184032b3..04b5da699036 100755 --- a/website/process_notebooks.py +++ b/website/process_notebooks.py @@ -17,7 +17,7 @@ from typing import Dict, Optional, Tuple, Union from dataclasses import dataclass from multiprocessing import current_process -from autogen.formatting_utils import colored +from termcolor import colored try: import yaml From fb43792ba948592d56173076edae8a286c918490 Mon Sep 17 00:00:00 2001 From: Jack Gerrits Date: Mon, 18 Mar 2024 12:37:30 -0400 Subject: [PATCH 5/6] conform with original colored func --- autogen/formatting_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogen/formatting_utils.py b/autogen/formatting_utils.py index 8777b0aab8c0..16612d785b8d 100644 --- a/autogen/formatting_utils.py +++ b/autogen/formatting_utils.py @@ -72,4 +72,4 @@ def colored( text=text, color=color, on_color=on_color, attrs=attrs, no_color=no_color, force_color=force_color ) except ImportError: - return text + return str(text) From 76b6461b0ed79afa4b7f4f9adabd017b348f35bd Mon Sep 17 00:00:00 2001 From: Jack Gerrits Date: Mon, 18 Mar 2024 13:20:52 -0400 Subject: [PATCH 6/6] change import strategy --- autogen/formatting_utils.py | 129 +++++++++++++++++------------------- 1 file changed, 62 insertions(+), 67 deletions(-) diff --git a/autogen/formatting_utils.py b/autogen/formatting_utils.py index 16612d785b8d..be8d8aaa562b 100644 --- a/autogen/formatting_utils.py +++ b/autogen/formatting_utils.py @@ -2,74 +2,69 @@ from typing import Iterable, Literal -Attribute = Literal[ - "bold", - "dark", - "underline", - "blink", - "reverse", - "concealed", -] +try: + from termcolor import colored +except ImportError: + # termcolor is an optional dependency - if it cannot be imported then no color is used. + # Alternatively the envvar NO_COLOR can be used to disable color. + # To allow for proper typing and for termcolor to be optional we need to re-define the types used in the lib here. + # This is the direct function definition from termcolor. + Attribute = Literal[ + "bold", + "dark", + "underline", + "blink", + "reverse", + "concealed", + ] -Highlight = Literal[ - "on_black", - "on_grey", - "on_red", - "on_green", - "on_yellow", - "on_blue", - "on_magenta", - "on_cyan", - "on_light_grey", - "on_dark_grey", - "on_light_red", - "on_light_green", - "on_light_yellow", - "on_light_blue", - "on_light_magenta", - "on_light_cyan", - "on_white", -] + Highlight = Literal[ + "on_black", + "on_grey", + "on_red", + "on_green", + "on_yellow", + "on_blue", + "on_magenta", + "on_cyan", + "on_light_grey", + "on_dark_grey", + "on_light_red", + "on_light_green", + "on_light_yellow", + "on_light_blue", + "on_light_magenta", + "on_light_cyan", + "on_white", + ] -Color = Literal[ - "black", - "grey", - "red", - "green", - "yellow", - "blue", - "magenta", - "cyan", - "light_grey", - "dark_grey", - "light_red", - "light_green", - "light_yellow", - "light_blue", - "light_magenta", - "light_cyan", - "white", -] + Color = Literal[ + "black", + "grey", + "red", + "green", + "yellow", + "blue", + "magenta", + "cyan", + "light_grey", + "dark_grey", + "light_red", + "light_green", + "light_yellow", + "light_blue", + "light_magenta", + "light_cyan", + "white", + ] - -# termcolor is an optional dependency - if it cannot be imported then no color is used. -# Alternatively the envvar NO_COLOR can be used to disable color. -# To allow for proper typing and for termcolor to be optional we need to re-define the types used in the lib here. -# This is the direct function definition from termcolor. -def colored( - text: object, - color: Color | None = None, - on_color: Highlight | None = None, - attrs: Iterable[Attribute] | None = None, - *, - no_color: bool | None = None, - force_color: bool | None = None, -) -> str: - try: - from termcolor import colored - - return colored( - text=text, color=color, on_color=on_color, attrs=attrs, no_color=no_color, force_color=force_color - ) - except ImportError: + def colored( + text: object, + color: Color | None = None, + on_color: Highlight | None = None, + attrs: Iterable[Attribute] | None = None, + *, + no_color: bool | None = None, + force_color: bool | None = None, + ) -> str: return str(text)