Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: Add logging (#701)

CaelumFb456787edafb
diff --git a/apps/agents/agents.py b/apps/agents/agents.py
index 27c2cce4a..eda55369e 100644
--- a/apps/agents/agents.py
+++ b/apps/agents/agents.py
@@ -27,6 +27,7 @@ from pydantic import BaseModel, ConfigDict
from apps.agents.text_utils import split_markdown_code
from camel.agents import TaskSpecifyAgent
+from camel.logger import get_logger
from camel.messages import BaseMessage
from camel.societies import RolePlaying
from camel.types import TaskType
@@ -38,6 +39,9 @@ REPO_ROOT = os.path.realpath(
ChatBotHistory = List[Tuple[Optional[str], Optional[str]]]
+logger = get_logger(__name__)
+
+
class State(BaseModel):
model_config = ConfigDict(
arbitrary_types_allowed=True,
@@ -110,7 +114,7 @@ def parse_arguments():
)
args, unknown = parser.parse_known_args()
if len(unknown) > 0:
- print("Unknown args: ", unknown)
+ logger.warning("Unknown args: %s", unknown)
return args
@@ -134,7 +138,7 @@ def load_roles(path: str) -> List[str]:
role = match.group(1)
roles.append(role)
else:
- print("Warning: no match")
+ logger.warn("No match")
return roles
@@ -190,11 +194,11 @@ def role_playing_start(
"""
if state.session is not None:
- print("Double click")
+ logger.info("Double click")
return {} # may fail
if society_name not in {"AI Society", "Code"}:
- print(f"Error: unrecognezed society {society_name}")
+ logger.error(f"Unrecognezed society {society_name}")
return {}
meta_dict: Optional[Dict[str, str]]
@@ -230,7 +234,7 @@ def role_playing_start(
output_language=language,
)
except (openai.RateLimitError, RuntimeError) as ex:
- print("OpenAI API exception 0 " + str(ex))
+ logger.info("OpenAI API exception 0 " + str(ex))
return (state, str(ex), "", [], gr.update())
# Can't re-create a state like below since it
@@ -285,7 +289,7 @@ def role_playing_chat_init(
"""
if state.session is None:
- print("Error: session is none on role_playing_chat_init call")
+ logger.error("Session is none on role_playing_chat_init call")
return state, state.chat, gr.update()
session: RolePlaying = state.session
@@ -294,7 +298,7 @@ def role_playing_chat_init(
input_msg: BaseMessage
input_msg = session.init_chat()
except (openai.RateLimitError, RuntimeError) as ex:
- print("OpenAI API exception 1 " + str(ex))
+ logger.error("OpenAI API exception 1 " + str(ex))
state.session = None
return state, state.chat, gr.update()
@@ -338,7 +342,7 @@ def role_playing_chat_cont(
state.saved_assistant_msg
)
except (openai.RateLimitError, RuntimeError) as ex:
- print("OpenAI API exception 2 " + str(ex))
+ logger.error("OpenAI API exception 2 " + str(ex))
state.session = None
return state, state.chat, gr.update(), gr.update()
@@ -639,7 +643,7 @@ def main():
args = parse_arguments()
- print("Getting Agents web server online...")
+ logger.info("Getting Agents web server online...")
blocks = construct_blocks(args.api_key)
@@ -651,7 +655,7 @@ def main():
debug=True,
)
- print("Exiting.")
+ logger.info("Exiting.")
if __name__ == "__main__":
diff --git a/apps/data_explorer/data_explorer.py b/apps/data_explorer/data_explorer.py
index 7fc5f38c2..eb285b499 100644
--- a/apps/data_explorer/data_explorer.py
+++ b/apps/data_explorer/data_explorer.py
@@ -22,6 +22,9 @@ from typing import Dict, List, Optional, Tuple
import gradio as gr
from apps.data_explorer.loader import Datasets, load_datasets
+from camel.logger import get_logger
+
+logger = get_logger(__name__)
def parse_arguments():
@@ -70,7 +73,7 @@ def parse_arguments():
)
args, unknown = parser.parse_known_args()
if len(unknown) > 0:
- print("Unknown args: ", unknown)
+ logger.warn("Unknown args: %s", unknown)
return args
@@ -407,11 +410,11 @@ def construct_blocks(data_path: str, default_dataset: Optional[str]):
gr.Blocks: Blocks instance.
"""
- print("Loading the dataset...")
+ logger.info("Loading the dataset...")
datasets = load_datasets(data_path)
- print("Dataset is loaded")
+ logger.info("Dataset is loaded")
- print("Getting Data Explorer web server online...")
+ logger.info("Getting Data Explorer web server online...")
with gr.Blocks() as blocks:
construct_ui(blocks, datasets, default_dataset)
@@ -433,7 +436,7 @@ def main():
server_port=args.server_port,
)
- print("Exiting.")
+ logger.info("Exiting.")
if __name__ == "__main__":
diff --git a/apps/data_explorer/downloader.py b/apps/data_explorer/downloader.py
index 4c0967bf2..7d1839fc8 100644
--- a/apps/data_explorer/downloader.py
+++ b/apps/data_explorer/downloader.py
@@ -17,13 +17,17 @@ import urllib.request
from huggingface_hub import hf_hub_download
from huggingface_hub.utils._errors import RepositoryNotFoundError
+from camel.logger import get_logger
+
REPO_ROOT = os.path.realpath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "../..")
)
+logger = get_logger(__name__)
+
def download_data():
- print("Downloading...")
+ logger.info("Downloading...")
data_dir = os.path.join(REPO_ROOT, "datasets/")
@@ -61,7 +65,7 @@ def download_data():
file_path = os.path.join(data_dir, os.path.split(data_url)[1])
urllib.request.urlretrieve(data_url, file_path)
- print("Download done")
+ logger.info("Download done")
if __name__ == "__main__":
diff --git a/apps/dilemma/database_connection.py b/apps/dilemma/database_connection.py
index f0ae3619e..ee28ac9e6 100644
--- a/apps/dilemma/database_connection.py
+++ b/apps/dilemma/database_connection.py
@@ -14,15 +14,19 @@
import sqlalchemy
from google.cloud.sql.connector import Connector, IPTypes
+from camel.logger import get_logger
+
# import os
# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = \
# "/path/to/camel-lm-XXXXXXXXX.json"
+logger = get_logger(__name__)
+
class DatabaseConnection:
def __init__(self):
INSTANCE_CONNECTION_NAME = "camel-lm:me-central1:camel-dilemma"
- print(f"Instance connection name is: {INSTANCE_CONNECTION_NAME}")
+ logger.info(f"Instance connection name is: {INSTANCE_CONNECTION_NAME}")
DB_USER = "dkuser"
DB_PASS = "camel230509"
DB_NAME = "dilemma_choices"
diff --git a/apps/dilemma/dilemma.py b/apps/dilemma/dilemma.py
index 76d2cf2bc..c7b6c13fa 100644
--- a/apps/dilemma/dilemma.py
+++ b/apps/dilemma/dilemma.py
@@ -26,6 +26,9 @@ import gradio as gr
from database_connection import DatabaseConnection
from apps.common.auto_zip import AutoZip
+from camel.logger import get_logger
+
+logger = get_logger(__name__)
def parse_arguments():
@@ -74,7 +77,7 @@ def parse_arguments():
)
args, unknown = parser.parse_known_args()
if len(unknown) > 0:
- print("Unknown args: ", unknown)
+ logger.warn("Unknown args: ", unknown)
return args
@@ -171,7 +174,7 @@ def construct_ui(
else:
who_is_better = state[choice]['who']
name = state['name']
- print(
+ logger.info(
"choice=", choice, "who_is_better=", who_is_better, "name=", name
)
if db_conn is not None:
@@ -204,11 +207,11 @@ def construct_blocks(data_path: str, has_connection: bool):
gr.Blocks: Blocks instance.
"""
- print("Loading the dataset...")
+ logger.info("Loading the dataset...")
dataset = load_dataset(data_path)
- print("Dataset is loaded")
+ logger.info("Dataset is loaded")
- print("Getting Dilemma web server online...")
+ logger.info("Getting Dilemma web server online...")
with gr.Blocks() as blocks:
construct_ui(blocks, dataset, has_connection)
@@ -230,7 +233,7 @@ def main():
server_port=args.server_port,
)
- print("Exiting.")
+ logger.info("Exiting.")
if __name__ == "__main__":
diff --git a/camel/__init__.py b/camel/__init__.py
index 1bfa5e757..2314e9810 100644
--- a/camel/__init__.py
+++ b/camel/__init__.py
@@ -12,9 +12,14 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+from camel.logger import disable_logging, enable_logging, set_log_level
+
__version__ = '0.2.10'
__all__ = [
'__version__',
'camel',
+ 'disable_logging',
+ 'enable_logging',
+ 'set_log_level',
]
diff --git a/camel/agents/deductive_reasoner_agent.py b/camel/agents/deductive_reasoner_agent.py
index 99265da7a..c56e3f279 100644
--- a/camel/agents/deductive_reasoner_agent.py
+++ b/camel/agents/deductive_reasoner_agent.py
@@ -15,11 +15,14 @@ import re
from typing import Dict, List, Optional, Union
from camel.agents.chat_agent import ChatAgent
+from camel.logger import get_logger
from camel.messages import BaseMessage
from camel.models import BaseModelBackend
from camel.prompts import TextPrompt
from camel.types import RoleType
+logger = get_logger(__name__)
+
# AgentOps decorator setting
try:
import os
@@ -253,7 +256,7 @@ square brackets)
"Deduction failed. Error:\n" + f"{response.info}"
)
msg: BaseMessage = response.msg
- print(f"Message content:\n{msg.content}")
+ logger.info(f"Message content:\n{msg.content}")
# Extract the conditions from the message
conditions_dict = {
diff --git a/camel/embeddings/vlm_embedding.py b/camel/embeddings/vlm_embedding.py
index 925bf0ca7..005d3802a 100644
--- a/camel/embeddings/vlm_embedding.py
+++ b/camel/embeddings/vlm_embedding.py
@@ -16,6 +16,9 @@ from typing import Any, List, Optional, Union
from PIL import Image
from camel.embeddings import BaseEmbedding
+from camel.logger import get_logger
+
+logger = get_logger(__name__)
class VisionLanguageEmbedding(BaseEmbedding[Union[str, Image.Image]]):
@@ -60,7 +63,7 @@ class VisionLanguageEmbedding(BaseEmbedding[Union[str, Image.Image]]):
"interpolate_pos_encoding",
]
except Exception:
- print("Warning: not typically processor and model structure")
+ logger.warning("not typically processor and model structure")
pass
self.dim: Optional[int] = None
diff --git a/camel/interpreters/docker_interpreter.py b/camel/interpreters/docker_interpreter.py
index 7da02c303..b3ccbf68e 100644
--- a/camel/interpreters/docker_interpreter.py
+++ b/camel/interpreters/docker_interpreter.py
@@ -23,11 +23,14 @@ from colorama import Fore
from camel.interpreters.base import BaseInterpreter
from camel.interpreters.interpreter_error import InterpreterError
+from camel.logger import get_logger
from camel.utils import is_docker_running
if TYPE_CHECKING:
from docker.models.containers import Container
+logger = get_logger(__name__)
+
class DockerInterpreter(BaseInterpreter):
r"""A class for executing code files or code strings in a docker container.
@@ -187,8 +190,10 @@ class DockerInterpreter(BaseInterpreter):
# Print code for security checking
if self.require_confirm:
- print(f"The following {code_type} code will run in container:")
- print(Fore.CYAN + code + Fore.RESET)
+ logger.info(
+ f"The following {code_type} code will run on your "
+ "computer: {code}"
+ )
while True:
choice = input("Running code? [Y/n]:").lower()
if choice in ["y", "yes", "ye", ""]:
diff --git a/camel/interpreters/subprocess_interpreter.py b/camel/interpreters/subprocess_interpreter.py
index 3e9aee940..b5ba04d31 100644
--- a/camel/interpreters/subprocess_interpreter.py
+++ b/camel/interpreters/subprocess_interpreter.py
@@ -22,6 +22,9 @@ from colorama import Fore
from camel.interpreters.base import BaseInterpreter
from camel.interpreters.interpreter_error import InterpreterError
+from camel.logger import get_logger
+
+logger = get_logger(__name__)
class SubprocessInterpreter(BaseInterpreter):
@@ -141,8 +144,10 @@ class SubprocessInterpreter(BaseInterpreter):
# Print code for security checking
if self.require_confirm:
- print(f"The following {code_type} code will run on your computer:")
- print(Fore.CYAN + code + Fore.RESET)
+ logger.info(
+ f"The following {code_type} code will run on your "
+ "computer: {code}"
+ )
while True:
choice = input("Running code? [Y/n]:").lower()
if choice in ["y", "yes", "ye", ""]:
diff --git a/camel/logger.py b/camel/logger.py
new file mode 100644
index 000000000..793bf19dc
--- /dev/null
+++ b/camel/logger.py
@@ -0,0 +1,112 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+
+import logging
+import os
+import sys
+
+# Create a private logger
+_logger = logging.getLogger('camel')
+
+
+def _configure_library_logging():
+ if os.environ.get('CAMEL_LOGGING_DISABLED', 'False').lower() == 'true':
+ return
+
+ if not logging.root.handlers and not _logger.handlers:
+ logging.basicConfig(
+ level=os.environ.get('LOGLEVEL', 'INFO').upper(),
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
+ stream=sys.stdout,
+ )
+ logging.setLoggerClass(logging.Logger)
+ _logger.info("Camel library logging has been configured.")
+ else:
+ _logger.debug("Existing logger configuration found, using that.")
+
+
+def disable_logging():
+ r"""Disable all logging for the Camel library.
+
+ This function sets the log level to a value higher than CRITICAL,
+ effectively disabling all log messages, and adds a NullHandler to
+ suppress any potential warnings about no handlers being found.
+ """
+ os.environ['CAMEL_LOGGING_DISABLED'] = 'true'
+ _logger.setLevel(logging.CRITICAL + 1)
+ # Avoid adding multiple NullHandlers
+ if not any(
+ isinstance(handler, logging.NullHandler)
+ for handler in _logger.handlers
+ ):
+ _logger.addHandler(logging.NullHandler())
+ _logger.debug("Logging has been disabled.")
+
+
+def enable_logging():
+ r"""Enable logging for the Camel library.
+
+ This function re-enables logging if it was previously disabled,
+ and configures the library logging using the default settings.
+ If the logging is already configured,
+ this function does not change its configuration.
+ """
+ os.environ['CAMEL_LOGGING_DISABLED'] = 'false'
+ _configure_library_logging()
+
+
+def set_log_level(level):
+ r"""Set the logging level for the Camel library.
+
+ Args:
+ level (Union[str, int]): The logging level to set. This can be a string
+ (e.g., 'INFO') or a logging level constant (e.g., logging.INFO,
+ logging.DEBUG).
+ See https://docs.python.org/3/library/logging.html#levels
+
+ Raises:
+ ValueError: If the provided level is not a valid logging level.
+ """
+ valid_levels = ['NOTSET', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
+ if isinstance(level, str):
+ if level.upper() not in valid_levels:
+ raise ValueError(
+ f"Invalid logging level."
+ f" Choose from: {', '.join(valid_levels)}"
+ )
+ level = level.upper()
+ elif not isinstance(level, int):
+ raise ValueError(
+ "Logging level must be an option from the logging module."
+ )
+
+ _logger.setLevel(level)
+ _logger.debug(f"Logging level set to: {logging.getLevelName(level)}")
+
+
+def get_logger(name):
+ r"""Get a logger with the specified name, prefixed with 'camel.'.
+
+ Args:
+ name (str): The name to be appended to 'camel.' to create the logger.
+
+ Returns:
+ logging.Logger: A logger instance with the name 'camel.{name}'.
+ """
+ return logging.getLogger(f'camel.{name}')
+
+
+# Lazy configuration: Only configure logging if explicitly enabled.
+if os.environ.get('CAMEL_LOGGING_DISABLED', 'False').strip().lower() != 'true':
+ _configure_library_logging()
diff --git a/camel/societies/babyagi_playing.py b/camel/societies/babyagi_playing.py
index f2fe49148..dde6f393c 100644
--- a/camel/societies/babyagi_playing.py
+++ b/camel/societies/babyagi_playing.py
@@ -22,10 +22,13 @@ from camel.agents import (
)
from camel.agents.chat_agent import ChatAgentResponse
from camel.generators import SystemMessageGenerator
+from camel.logger import get_logger
from camel.messages import BaseMessage
from camel.prompts import TextPrompt
from camel.types import RoleType, TaskType
+logger = get_logger(__name__)
+
class BabyAGI:
r"""The BabyAGI Agent adapted from `"Task-driven Autonomous Agent"
@@ -261,7 +264,7 @@ class BabyAGI:
)
self.subtasks = deque(prioritized_subtask_list)
else:
- print("no new tasks")
+ logger.info("no new tasks")
assistant_response.info['task_name'] = task_name
assistant_response.info['subtasks'] = list(self.subtasks)
if not self.subtasks:
diff --git a/camel/toolkits/twitter_toolkit.py b/camel/toolkits/twitter_toolkit.py
index 41de4d8ae..286ea3d09 100644
--- a/camel/toolkits/twitter_toolkit.py
+++ b/camel/toolkits/twitter_toolkit.py
@@ -20,12 +20,15 @@ from typing import Any, Dict, List, Optional, Union
import requests
from requests_oauthlib import OAuth1
+from camel.logger import get_logger
from camel.toolkits import FunctionTool
from camel.toolkits.base import BaseToolkit
from camel.utils import api_keys_required
TWEET_TEXT_LIMIT = 280
+logger = get_logger(__name__)
+
@api_keys_required(
"TWITTER_CONSUMER_KEY",
diff --git a/camel/utils/commons.py b/camel/utils/commons.py
index 0c0ba3db1..5e1565e16 100644
--- a/camel/utils/commons.py
+++ b/camel/utils/commons.py
@@ -12,6 +12,7 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import importlib
+import logging
import os
import platform
import re
@@ -39,14 +40,19 @@ import pydantic
import requests
from pydantic import BaseModel
+from camel.logger import get_logger
from camel.types import TaskType
from .constants import Constants
F = TypeVar('F', bound=Callable[..., Any])
+logger = get_logger(__name__)
-def print_text_animated(text, delay: float = 0.02, end: str = ""):
+
+def print_text_animated(
+ text, delay: float = 0.02, end: str = "", log_level: int = logging.INFO
+):
r"""Prints the given text with an animated effect.
Args:
@@ -55,11 +61,22 @@ def print_text_animated(text, delay: float = 0.02, end: str = ""):
(default: :obj:`0.02`)
end (str, optional): The end character to print after each
character of text. (default: :obj:`""`)
+ log_level (int, optional): The log level to use.
+ See https://docs.python.org/3/library/logging.html#levels
+ (default: :obj:`logging.INFO`)
"""
- for char in text:
- print(char, end=end, flush=True)
- time.sleep(delay)
- print('\n')
+ if logger.isEnabledFor(log_level):
+ # timestamp and other prefixes
+ logger.log(log_level, '')
+
+ for char in text:
+ print(char, end=end, flush=True)
+ time.sleep(delay)
+ # Close the log entry
+ logger.log(log_level, '')
+ else:
+ # This may be relevant for logging frameworks
+ logger.log(log_level, text)
def get_prompt_template_key_words(template: str) -> Set[str]:
diff --git a/camel/utils/token_counting.py b/camel/utils/token_counting.py
index 7ddb77f15..8cdfae062 100644
--- a/camel/utils/token_counting.py
+++ b/camel/utils/token_counting.py
@@ -22,6 +22,7 @@ from typing import TYPE_CHECKING, List, Optional
from PIL import Image
+from camel.logger import get_logger
from camel.types import (
ModelType,
OpenAIImageType,
@@ -44,6 +45,8 @@ SQUARE_PIXELS = 512
SQUARE_TOKENS = 170
EXTRA_TOKENS = 85
+logger = get_logger(__name__)
+
def get_model_encoding(value_for_tiktoken: str):
r"""Get model encoding from tiktoken.
@@ -65,7 +68,7 @@ def get_model_encoding(value_for_tiktoken: str):
]:
encoding = tiktoken.get_encoding("o200k_base")
else:
- print("Model not found. Using cl100k_base encoding.")
+ logger.info("Model not found. Using cl100k_base encoding.")
encoding = tiktoken.get_encoding("cl100k_base")
return encoding