Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Refactor/decouple logger from global configuration (#3171)

James Collins7e5afd874410
diff --git a/autogpt/agent/agent.py b/autogpt/agent/agent.py
index 8c2bfb743f..bb5b873de1 100644
--- a/autogpt/agent/agent.py
+++ b/autogpt/agent/agent.py
@@ -107,7 +107,9 @@ class Agent:
validate_json(assistant_reply_json, "llm_response_format_1")
# Get command name and arguments
try:
- print_assistant_thoughts(self.ai_name, assistant_reply_json)
+ print_assistant_thoughts(
+ self.ai_name, assistant_reply_json, cfg.speak_mode
+ )
command_name, arguments = get_command(assistant_reply_json)
if cfg.speak_mode:
say_text(f"I want to execute {command_name}")
diff --git a/autogpt/logs.py b/autogpt/logs.py
index 9d66193326..3e231d8dd5 100644
--- a/autogpt/logs.py
+++ b/autogpt/logs.py
@@ -10,12 +10,9 @@ from logging import LogRecord
from colorama import Fore, Style
-from autogpt.config import Config
from autogpt.singleton import Singleton
from autogpt.speech import say_text
-CFG = Config()
-
class Logger(metaclass=Singleton):
"""
@@ -79,10 +76,12 @@ class Logger(metaclass=Singleton):
self.logger.addHandler(error_handler)
self.logger.setLevel(logging.DEBUG)
+ self.speak_mode = False
+
def typewriter_log(
self, title="", title_color="", content="", speak_text=False, level=logging.INFO
):
- if speak_text and CFG.speak_mode:
+ if speak_text and self.speak_mode:
say_text(f"{title}. {content}")
if content:
@@ -203,100 +202,10 @@ def remove_color_codes(s: str) -> str:
logger = Logger()
-def print_assistant_thoughts(ai_name, assistant_reply):
- """Prints the assistant's thoughts to the console"""
- from autogpt.json_utils.json_fix_llm import (
- attempt_to_fix_json_by_finding_outermost_brackets,
- fix_and_parse_json,
- )
-
- try:
- try:
- # Parse and print Assistant response
- assistant_reply_json = fix_and_parse_json(assistant_reply)
- except json.JSONDecodeError:
- logger.error("Error: Invalid JSON in assistant thoughts\n", assistant_reply)
- assistant_reply_json = attempt_to_fix_json_by_finding_outermost_brackets(
- assistant_reply
- )
- if isinstance(assistant_reply_json, str):
- assistant_reply_json = fix_and_parse_json(assistant_reply_json)
-
- # Check if assistant_reply_json is a string and attempt to parse
- # it into a JSON object
- if isinstance(assistant_reply_json, str):
- try:
- assistant_reply_json = json.loads(assistant_reply_json)
- except json.JSONDecodeError:
- logger.error("Error: Invalid JSON\n", assistant_reply)
- assistant_reply_json = (
- attempt_to_fix_json_by_finding_outermost_brackets(
- assistant_reply_json
- )
- )
-
- assistant_thoughts_reasoning = None
- assistant_thoughts_plan = None
- assistant_thoughts_speak = None
- assistant_thoughts_criticism = None
- if not isinstance(assistant_reply_json, dict):
- assistant_reply_json = {}
- assistant_thoughts = assistant_reply_json.get("thoughts", {})
- assistant_thoughts_text = assistant_thoughts.get("text")
-
- if assistant_thoughts:
- assistant_thoughts_reasoning = assistant_thoughts.get("reasoning")
- assistant_thoughts_plan = assistant_thoughts.get("plan")
- assistant_thoughts_criticism = assistant_thoughts.get("criticism")
- assistant_thoughts_speak = assistant_thoughts.get("speak")
-
- logger.typewriter_log(
- f"{ai_name.upper()} THOUGHTS:", Fore.YELLOW, f"{assistant_thoughts_text}"
- )
- logger.typewriter_log(
- "REASONING:", Fore.YELLOW, f"{assistant_thoughts_reasoning}"
- )
-
- if assistant_thoughts_plan:
- logger.typewriter_log("PLAN:", Fore.YELLOW, "")
- # If it's a list, join it into a string
- if isinstance(assistant_thoughts_plan, list):
- assistant_thoughts_plan = "\n".join(assistant_thoughts_plan)
- elif isinstance(assistant_thoughts_plan, dict):
- assistant_thoughts_plan = str(assistant_thoughts_plan)
-
- # Split the input_string using the newline character and dashes
- lines = assistant_thoughts_plan.split("\n")
- for line in lines:
- line = line.lstrip("- ")
- logger.typewriter_log("- ", Fore.GREEN, line.strip())
-
- logger.typewriter_log(
- "CRITICISM:", Fore.YELLOW, f"{assistant_thoughts_criticism}"
- )
- # Speak the assistant's thoughts
- if CFG.speak_mode and assistant_thoughts_speak:
- say_text(assistant_thoughts_speak)
- else:
- logger.typewriter_log("SPEAK:", Fore.YELLOW, f"{assistant_thoughts_speak}")
-
- return assistant_reply_json
- except json.decoder.JSONDecodeError:
- logger.error("Error: Invalid JSON\n", assistant_reply)
- if CFG.speak_mode:
- say_text(
- "I have received an invalid JSON response from the OpenAI API."
- " I cannot ignore this response."
- )
-
- # All other errors, return "Error: + error message"
- except Exception:
- call_stack = traceback.format_exc()
- logger.error("Error: \n", call_stack)
-
-
def print_assistant_thoughts(
- ai_name: object, assistant_reply_json_valid: object
+ ai_name: object,
+ assistant_reply_json_valid: object,
+ speak_mode: bool = False,
) -> None:
assistant_thoughts_reasoning = None
assistant_thoughts_plan = None
@@ -329,5 +238,5 @@ def print_assistant_thoughts(
logger.typewriter_log("- ", Fore.GREEN, line.strip())
logger.typewriter_log("CRITICISM:", Fore.YELLOW, f"{assistant_thoughts_criticism}")
# Speak the assistant's thoughts
- if CFG.speak_mode and assistant_thoughts_speak:
+ if speak_mode and assistant_thoughts_speak:
say_text(assistant_thoughts_speak)
diff --git a/autogpt/main.py b/autogpt/main.py
index 08b610926b..b3ffb01ebb 100644
--- a/autogpt/main.py
+++ b/autogpt/main.py
@@ -34,6 +34,10 @@ def run_auto_gpt(
workspace_directory: str,
install_plugin_deps: bool,
):
+ # Configure logging before we do anything else.
+ logger.set_level(logging.DEBUG if debug else logging.INFO)
+ logger.speak_mode = speak
+
cfg = Config()
# TODO: fill in llm values here
check_openai_api_key()
@@ -51,7 +55,7 @@ def run_auto_gpt(
allow_downloads,
skip_news,
)
- logger.set_level(logging.DEBUG if cfg.debug_mode else logging.INFO)
+
if not cfg.skip_news:
motd = get_latest_bulletin()
if motd:
diff --git a/autogpt/speech/say.py b/autogpt/speech/say.py
index 727983d12b..917fa65cbb 100644
--- a/autogpt/speech/say.py
+++ b/autogpt/speech/say.py
@@ -3,39 +3,44 @@ import threading
from threading import Semaphore
from autogpt.config import Config
+from autogpt.speech.base import VoiceBase
from autogpt.speech.brian import BrianSpeech
from autogpt.speech.eleven_labs import ElevenLabsSpeech
from autogpt.speech.gtts import GTTSVoice
from autogpt.speech.macos_tts import MacOSTTS
-CFG = Config()
-DEFAULT_VOICE_ENGINE = GTTSVoice()
-VOICE_ENGINE = None
-if CFG.elevenlabs_api_key:
- VOICE_ENGINE = ElevenLabsSpeech()
-elif CFG.use_mac_os_tts == "True":
- VOICE_ENGINE = MacOSTTS()
-elif CFG.use_brian_tts == "True":
- VOICE_ENGINE = BrianSpeech()
-else:
- VOICE_ENGINE = GTTSVoice()
-
-
-QUEUE_SEMAPHORE = Semaphore(
+_QUEUE_SEMAPHORE = Semaphore(
1
) # The amount of sounds to queue before blocking the main thread
def say_text(text: str, voice_index: int = 0) -> None:
"""Speak the given text using the given voice index"""
+ cfg = Config()
+ default_voice_engine, voice_engine = _get_voice_engine(cfg)
def speak() -> None:
- success = VOICE_ENGINE.say(text, voice_index)
+ success = voice_engine.say(text, voice_index)
if not success:
- DEFAULT_VOICE_ENGINE.say(text)
+ default_voice_engine.say(text)
- QUEUE_SEMAPHORE.release()
+ _QUEUE_SEMAPHORE.release()
- QUEUE_SEMAPHORE.acquire(True)
+ _QUEUE_SEMAPHORE.acquire(True)
thread = threading.Thread(target=speak)
thread.start()
+
+
+def _get_voice_engine(config: Config) -> tuple[VoiceBase, VoiceBase]:
+ """Get the voice engine to use for the given configuration"""
+ default_voice_engine = GTTSVoice()
+ if config.elevenlabs_api_key:
+ voice_engine = ElevenLabsSpeech()
+ elif config.use_mac_os_tts == "True":
+ voice_engine = MacOSTTS()
+ elif config.use_brian_tts == "True":
+ voice_engine = BrianSpeech()
+ else:
+ voice_engine = GTTSVoice()
+
+ return default_voice_engine, voice_engine