A programming framework for agentic AI
Encapsulate colored into a module (#2057)
Jack Gerritse35db7ef5f5f
diff --git a/autogen/agentchat/chat.py b/autogen/agentchat/chat.pyindex bd4559ff4..d5e127e97 100644--- a/autogen/agentchat/chat.py+++ b/autogen/agentchat/chat.py@@ -7,7 +7,7 @@ from dataclasses import dataclassfrom .utils import consolidate_chat_infoimport datetimeimport warnings-from termcolor import colored+from ..formatting_utils import coloredlogger = logging.getLogger(__name__)diff --git a/autogen/agentchat/contrib/capabilities/teachability.py b/autogen/agentchat/contrib/capabilities/teachability.pyindex 9e18f99a3..58ba35ed4 100644--- a/autogen/agentchat/contrib/capabilities/teachability.py+++ b/autogen/agentchat/contrib/capabilities/teachability.py@@ -6,7 +6,7 @@ import picklefrom autogen.agentchat.assistant_agent import ConversableAgentfrom autogen.agentchat.contrib.capabilities.agent_capability import AgentCapabilityfrom autogen.agentchat.contrib.text_analyzer_agent import TextAnalyzerAgent-from autogen.agentchat.conversable_agent import colored+from ....formatting_utils import coloredclass Teachability(AgentCapability):diff --git a/autogen/agentchat/contrib/compressible_agent.py b/autogen/agentchat/contrib/compressible_agent.pyindex 0264d2656..152cc871a 100644--- a/autogen/agentchat/contrib/compressible_agent.py+++ b/autogen/agentchat/contrib/compressible_agent.py@@ -7,13 +7,7 @@ import loggingimport inspectfrom 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 coloredlogger = logging.getLogger(__name__)diff --git a/autogen/agentchat/contrib/llava_agent.py b/autogen/agentchat/contrib/llava_agent.pyindex c26f576ab..182f72837 100644--- a/autogen/agentchat/contrib/llava_agent.py+++ b/autogen/agentchat/contrib/llava_agent.py@@ -8,7 +8,7 @@ from autogen.agentchat.agent import Agentfrom autogen.agentchat.contrib.img_utils import get_image_data, llava_formatterfrom autogen.agentchat.contrib.multimodal_conversable_agent import MultimodalConversableAgentfrom autogen.code_utils import content_str-from autogen.agentchat.conversable_agent import colored+from ...formatting_utils import coloredlogger = logging.getLogger(__name__)diff --git a/autogen/agentchat/contrib/retrieve_user_proxy_agent.py b/autogen/agentchat/contrib/retrieve_user_proxy_agent.pyindex 5d9657d81..f252f60e5 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.retrieve_utils import create_vector_db_from_dir, query_vector_db, Tfrom autogen.token_count_utils import count_tokenfrom autogen.code_utils import extract_codefrom autogen import logger-from autogen.agentchat.conversable_agent import colored+from ...formatting_utils import coloredPROMPT_DEFAULT = """You're a retrieve augmented chatbot. You answer user's questions based on your own knowledge and thediff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.pyindex 838af6e7d..accb2c54d 100644--- a/autogen/agentchat/conversable_agent.py+++ b/autogen/agentchat/conversable_agent.py@@ -15,6 +15,7 @@ from autogen.exception_utils import InvalidCarryOverType, SenderRequiredfrom ..coding.base import CodeExecutorfrom ..coding.factory import CodeExecutorFactory+from ..formatting_utils import coloredfrom ..oai.client import OpenAIWrapper, ModelClientfrom ..runtime_logging import logging_enabled, log_new_agent@@ -36,14 +37,6 @@ from ..function_utils import get_function_schema, load_basemodels_if_needed, serfrom .agent import Agent, LLMAgentfrom .._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.pynew file mode 100644index 000000000..be8d8aaa5--- /dev/null+++ b/autogen/formatting_utils.py@@ -0,0 +1,70 @@+from __future__ import annotations++from typing import Iterable, Literal++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",+ ]++ 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",+ ]++ 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)diff --git a/test/agentchat/contrib/capabilities/chat_with_teachable_agent.py b/test/agentchat/contrib/capabilities/chat_with_teachable_agent.pyindex 66d61386d..27f030045 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 osimport sys-from termcolor import coloredfrom autogen import UserProxyAgent, config_list_from_jsonfrom autogen.agentchat.contrib.capabilities.teachability import Teachabilityfrom autogen import ConversableAgent+from autogen.formatting_utils import coloredsys.path.append(os.path.join(os.path.dirname(__file__), "../.."))from test_assistant_agent import OAI_CONFIG_LIST, KEY_LOC # noqa: E402diff --git a/test/agentchat/contrib/capabilities/test_teachable_agent.py b/test/agentchat/contrib/capabilities/test_teachable_agent.pyindex 44904b26d..e9fdf68c8 100755--- a/test/agentchat/contrib/capabilities/test_teachable_agent.py+++ b/test/agentchat/contrib/capabilities/test_teachable_agent.py@@ -3,7 +3,7 @@import pytestimport osimport sys-from termcolor import colored+from autogen.formatting_utils import coloredfrom autogen import ConversableAgent, config_list_from_jsonsys.path.append(os.path.join(os.path.dirname(__file__), "../../.."))