From 603e27410ce4de8e0ef920b18048cd0b27401680 Mon Sep 17 00:00:00 2001 From: Vivian Fang Date: Fri, 13 Oct 2023 00:39:53 -0700 Subject: [PATCH] add debug flag --- interface.py | 54 +++++++++++++++++++++++++++++------------------ main.py | 6 ++++++ memgpt/presets.py | 3 ++- memgpt/utils.py | 2 +- 4 files changed, 42 insertions(+), 23 deletions(-) diff --git a/interface.py b/interface.py index 52bdc389..be9951dc 100644 --- a/interface.py +++ b/interface.py @@ -3,12 +3,13 @@ import re from colorama import Fore, Style, init +from memgpt.utils import printd + init(autoreset=True) # DEBUG = True # puts full message outputs in the terminal DEBUG = False # only dumps important messages in the terminal - async def internal_monologue(msg): # ANSI escape code for italic is '\x1B[3m' print(f'\x1B[3m{Fore.LIGHTBLACK_EX}💭 {msg}{Style.RESET_ALL}') @@ -20,65 +21,76 @@ async def memory_message(msg): print(f'{Fore.LIGHTMAGENTA_EX}{Style.BRIGHT}🧠 {Fore.LIGHTMAGENTA_EX}{msg}{Style.RESET_ALL}') async def system_message(msg): - print(f'{Fore.MAGENTA}{Style.BRIGHT}🖥️ [system] {Fore.MAGENTA}{msg}{Style.RESET_ALL}') + printd(f'{Fore.MAGENTA}{Style.BRIGHT}🖥️ [system] {Fore.MAGENTA}{msg}{Style.RESET_ALL}') async def user_message(msg, raw=False): if isinstance(msg, str): if raw: - print(f'{Fore.GREEN}{Style.BRIGHT}🧑 {Fore.GREEN}{msg}{Style.RESET_ALL}') + printd(f'{Fore.GREEN}{Style.BRIGHT}🧑 {Fore.GREEN}{msg}{Style.RESET_ALL}') return else: try: msg_json = json.loads(msg) except: - print(f"Warning: failed to parse user message into json") - print(f'{Fore.GREEN}{Style.BRIGHT}🧑 {Fore.GREEN}{msg}{Style.RESET_ALL}') + printd(f"Warning: failed to parse user message into json") + printd(f'{Fore.GREEN}{Style.BRIGHT}🧑 {Fore.GREEN}{msg}{Style.RESET_ALL}') return if msg_json['type'] == 'user_message': msg_json.pop('type') - print(f'{Fore.GREEN}{Style.BRIGHT}🧑 {Fore.GREEN}{msg_json}{Style.RESET_ALL}') + printd(f'{Fore.GREEN}{Style.BRIGHT}🧑 {Fore.GREEN}{msg_json}{Style.RESET_ALL}') elif msg_json['type'] == 'heartbeat': if DEBUG: msg_json.pop('type') - print(f'{Fore.GREEN}{Style.BRIGHT}💓 {Fore.GREEN}{msg_json}{Style.RESET_ALL}') + printd(f'{Fore.GREEN}{Style.BRIGHT}💓 {Fore.GREEN}{msg_json}{Style.RESET_ALL}') elif msg_json['type'] == 'system_message': msg_json.pop('type') - print(f'{Fore.GREEN}{Style.BRIGHT}🖥️ {Fore.GREEN}{msg_json}{Style.RESET_ALL}') + printd(f'{Fore.GREEN}{Style.BRIGHT}🖥️ {Fore.GREEN}{msg_json}{Style.RESET_ALL}') else: - print(f'{Fore.GREEN}{Style.BRIGHT}🧑 {Fore.GREEN}{msg_json}{Style.RESET_ALL}') + printd(f'{Fore.GREEN}{Style.BRIGHT}🧑 {Fore.GREEN}{msg_json}{Style.RESET_ALL}') async def function_message(msg): if isinstance(msg, dict): - print(f'{Fore.RED}{Style.BRIGHT}⚡ [function] {Fore.RED}{msg}{Style.RESET_ALL}') + printd(f'{Fore.RED}{Style.BRIGHT}⚡ [function] {Fore.RED}{msg}{Style.RESET_ALL}') return if msg.startswith('Success: '): - if DEBUG: - print(f'{Fore.RED}{Style.BRIGHT}⚡🟢 [function] {Fore.RED}{msg}{Style.RESET_ALL}') + printd(f'{Fore.RED}{Style.BRIGHT}⚡🟢 [function] {Fore.RED}{msg}{Style.RESET_ALL}') elif msg.startswith('Error: '): - print(f'{Fore.RED}{Style.BRIGHT}⚡🔴 [function] {Fore.RED}{msg}{Style.RESET_ALL}') + printd(f'{Fore.RED}{Style.BRIGHT}⚡🔴 [function] {Fore.RED}{msg}{Style.RESET_ALL}') elif msg.startswith('Running '): if DEBUG: - print(f'{Fore.RED}{Style.BRIGHT}⚡ [function] {Fore.RED}{msg}{Style.RESET_ALL}') + printd(f'{Fore.RED}{Style.BRIGHT}⚡ [function] {Fore.RED}{msg}{Style.RESET_ALL}') else: if 'memory' in msg: - match = re.search(r'Running (\w+)\(', msg) + match = re.search(r'Running (\w+)\((.*)\)', msg) if match: function_name = match.group(1) - print(f'{Fore.RED}{Style.BRIGHT}⚡🧠 [function] {Fore.RED}updating memory with {function_name}{Style.RESET_ALL}') + function_args = match.group(2) + print(f'{Fore.RED}{Style.BRIGHT}⚡🧠 [function] {Fore.RED}updating memory with {function_name}{Style.RESET_ALL}:') + try: + msg_dict = eval(function_args) + print(f'{Fore.RED}{Style.BRIGHT}\t{Fore.RED} {msg_dict["old_content"]}\n\t→ {msg_dict["new_content"]}') + except Exception as e: + print(e) + pass else: - print(f"Warning: did not recognize function message") - print(f'{Fore.RED}{Style.BRIGHT}⚡ [function] {Fore.RED}{msg}{Style.RESET_ALL}') + printd(f"Warning: did not recognize function message") + printd(f'{Fore.RED}{Style.BRIGHT}⚡ [function] {Fore.RED}{msg}{Style.RESET_ALL}') elif 'send_message' in msg: # ignore in debug mode pass else: - print(f'{Fore.RED}{Style.BRIGHT}⚡ [function] {Fore.RED}{msg}{Style.RESET_ALL}') + printd(f'{Fore.RED}{Style.BRIGHT}⚡ [function] {Fore.RED}{msg}{Style.RESET_ALL}') else: - print(f"Warning: did not recognize function message") - print(f'{Fore.RED}{Style.BRIGHT}⚡ [function] {Fore.RED}{msg}{Style.RESET_ALL}') + try: + msg_dict = json.loads(msg) + if "status" in msg_dict and msg_dict["status"] == "OK": + printd(f'{Fore.GREEN}{Style.BRIGHT}⚡ [function] {Fore.GREEN}{msg}{Style.RESET_ALL}') + except Exception: + printd(f"Warning: did not recognize function message {type(msg)} {msg}") + printd(f'{Fore.RED}{Style.BRIGHT}⚡ [function] {Fore.RED}{msg}{Style.RESET_ALL}') async def print_messages(message_sequence): for msg in message_sequence: diff --git a/main.py b/main.py index bc0ca699..4e2c8aa5 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ import asyncio from absl import app, flags +import logging import os import sys import pickle @@ -21,6 +22,7 @@ FLAGS = flags.FLAGS flags.DEFINE_string("persona", default=personas.DEFAULT, required=False, help="Specify persona") flags.DEFINE_string("human", default=humans.DEFAULT, required=False, help="Specify human") flags.DEFINE_boolean("first", default=False, required=False, help="Use -first to send the first message in the sequence") +flags.DEFINE_boolean("debug", default=False, required=False, help="Use -debug to enable debugging output") def clear_line(): @@ -34,6 +36,10 @@ def clear_line(): async def main(): + utils.DEBUG = FLAGS.debug + logging.getLogger().setLevel(logging.CRITICAL) + if FLAGS.debug: + logging.getLogger().setLevel(logging.DEBUG) print("Running... [exit by typing 'exit']") memgpt_agent = presets.use_preset(presets.DEFAULT, personas.get_persona_text(FLAGS.persona), humans.get_human_text(), interface, persistence_manager()) diff --git a/memgpt/presets.py b/memgpt/presets.py index a3eccb42..e42f9122 100644 --- a/memgpt/presets.py +++ b/memgpt/presets.py @@ -2,6 +2,7 @@ from .prompts import gpt_functions from .prompts import gpt_system from .agent import AgentAsync +from .utils import printd DEFAULT = 'memgpt_chat' @@ -19,7 +20,7 @@ def use_preset(preset_name, persona, human, interface, persistence_manager): 'archival_memory_insert', 'archival_memory_search', ] available_functions = [v for k,v in gpt_functions.FUNCTIONS_CHAINING.items() if k in functions] - print(f"Available functions:\n", [x['name'] for x in available_functions]) + printd(f"Available functions:\n", [x['name'] for x in available_functions]) assert len(functions) == len(available_functions) return AgentAsync( diff --git a/memgpt/utils.py b/memgpt/utils.py index 7a99adeb..d9b1a4ae 100644 --- a/memgpt/utils.py +++ b/memgpt/utils.py @@ -10,7 +10,7 @@ import pytz DEBUG = False def printd(*args, **kwargs): if DEBUG: - printd(*args, **kwargs) + print(*args, **kwargs) def cosine_similarity(a, b): return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))