Skip to content

Commit

Permalink
Fixed Time Travel output for multithreading and clarity (#388)
Browse files Browse the repository at this point in the history
* Fixed for multithreading and clarity

* dont need threading

* leftover line

* fixed for threading

* cleaning up time travel output

* Added prepend string
  • Loading branch information
HowieG authored Sep 17, 2024
1 parent 8161133 commit e06bf61
Showing 1 changed file with 17 additions and 36 deletions.
53 changes: 17 additions & 36 deletions agentops/time_travel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from .exceptions import ApiServerException
from .singleton import singleton

ttd_prepend_string = "🖇️ Agentops: ⏰ Time Travel |"


@singleton
class TimeTravel:
Expand Down Expand Up @@ -47,9 +49,9 @@ def fetch_time_travel_id(ttd_id):

set_time_travel_active_state(True)
except ApiServerException as e:
manage_time_travel_state(activated=False, error=e)
print(f"{ttd_prepend_string} Error - {e}")
except Exception as e:
manage_time_travel_state(activated=False, error=e)
print(f"{ttd_prepend_string} Error - {e}")


def fetch_completion_override_from_time_travel_cache(kwargs):
Expand All @@ -64,14 +66,14 @@ def fetch_completion_override_from_time_travel_cache(kwargs):
def find_cache_hit(prompt_messages, completion_overrides):
if not isinstance(prompt_messages, (list, tuple)):
print(
"Time Travel Error - unexpected type for prompt_messages. Expected 'list' or 'tuple'. Got ",
f"{ttd_prepend_string} Error - unexpected type for prompt_messages. Expected 'list' or 'tuple'. Got ",
type(prompt_messages),
)
return None

if not isinstance(completion_overrides, dict):
print(
"Time Travel Error - unexpected type for completion_overrides. Expected 'dict'. Got ",
f"{ttd_prepend_string} Error - unexpected type for completion_overrides. Expected 'dict'. Got ",
type(completion_overrides),
)
return None
Expand All @@ -80,15 +82,15 @@ def find_cache_hit(prompt_messages, completion_overrides):
completion_override_dict = eval(key)
if not isinstance(completion_override_dict, dict):
print(
"Time Travel Error - unexpected type for completion_override_dict. Expected 'dict'. Got ",
f"{ttd_prepend_string} Error - unexpected type for completion_override_dict. Expected 'dict'. Got ",
type(completion_override_dict),
)
continue

cached_messages = completion_override_dict.get("messages")
if not isinstance(cached_messages, list):
print(
"Time Travel Error - unexpected type for cached_messages. Expected 'list'. Got ",
f"{ttd_prepend_string} Error - unexpected type for cached_messages. Expected 'list'. Got ",
type(cached_messages),
)
continue
Expand All @@ -105,10 +107,12 @@ def find_cache_hit(prompt_messages, completion_overrides):
return value
except (SyntaxError, ValueError, TypeError) as e:
print(
f"Time Travel Error - Error processing completion_overrides item: {e}"
f"{ttd_prepend_string} Error - Error processing completion_overrides item: {e}"
)
except Exception as e:
print(f"Time Travel Error - Unexpected error in find_cache_hit: {e}")
print(
f"{ttd_prepend_string} Error - Unexpected error in find_cache_hit: {e}"
)
return None


Expand All @@ -120,14 +124,10 @@ def check_time_travel_active():
try:
with open(config_file_path, "r") as config_file:
config = yaml.safe_load(config_file)
if config.get("Time_Travel_Debugging_Active", True):
manage_time_travel_state(activated=True)
return True
return config.get("Time_Travel_Debugging_Active", False)
except FileNotFoundError:
return False

return False


def set_time_travel_active_state(is_active: bool):
config_path = ".agentops_time_travel.yaml"
Expand All @@ -144,30 +144,11 @@ def set_time_travel_active_state(is_active: bool):
yaml.dump(config, config_file)
except:
print(
f"🖇 AgentOps: Unable to write to {config_path}. Time Travel not activated"
f"{ttd_prepend_string} Error - Unable to write to {config_path}. Time Travel not activated"
)
return

if is_active:
manage_time_travel_state(activated=True)
print("🖇 AgentOps: Time Travel Activated")
else:
manage_time_travel_state(activated=False)
print("🖇 AgentOps: Time Travel Deactivated")


def add_time_travel_terminal_indicator():
print(f"🖇️ ⏰ | ", end="")


def reset_terminal():
print("\033[0m", end="")


def manage_time_travel_state(activated=False, error=None):
if activated:
add_time_travel_terminal_indicator()
if is_active:
print(f"{ttd_prepend_string} Activated")
else:
reset_terminal()
if error is not None:
print(f"🖇 Deactivating Time Travel. Error with configuration: {error}")
print(f"{ttd_prepend_string} Deactivated")

0 comments on commit e06bf61

Please sign in to comment.