Skip to content

Commit

Permalink
better log organization
Browse files Browse the repository at this point in the history
  • Loading branch information
maeste committed Oct 9, 2024
1 parent a9251fd commit 8c60460
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/wiseagents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def enforce_no_abstract_class_instances(cls: type, check: type):


def log_messages_exchanged(messages: List[ChatCompletionMessageParam], agent_name: str,
context_name: str, dir_path: Optional[str] = './log'):
context_name: str, dir_path: Optional[str] = './log/messages'):
"""
Log the messages exchanged in a conversation to the given file.
Expand All @@ -35,13 +35,17 @@ def log_messages_exchanged(messages: List[ChatCompletionMessageParam], agent_nam
context_name: the name of the context in which the conversation took place
dir_path: the directory path to output the log file to, defaults to './log'
"""
if not os.path.exists(dir_path):
os.makedirs(dir_path)
with open(f'{dir_path}/messages-{agent_name}-{context_name}.md', 'w') as file:
context_name = context_name.replace("_", "/")
if not os.path.exists(f'{dir_path}/{context_name}/md') :
os.makedirs(f'{dir_path}/{context_name}/md')
if not os.path.exists(f'{dir_path}/{context_name}/json') :
os.makedirs(f'{dir_path}/{context_name}/json')

with open(f'{dir_path}/{context_name}/md/{agent_name}.md', 'w') as file:
message : ChatCompletionMessageParam
for message in messages:
file.write(f"## {message.get('role')}\n {message.get('content')}\n\n\n")
#file.write(f"{message}\n\n\n")
with open(f'{dir_path}/messages-{agent_name}-{context_name}.log', 'w') as file:
with open(f'{dir_path}/{context_name}/json/{agent_name}.json', 'w') as file:
json.dump(messages, file, indent=2)
print(f"[{agent_name}] Logged messages to {dir_path}/messages-{agent_name}-{context_name}.log for the current request")
print(f"[{agent_name}] Logged messages to {dir_path}/{context_name}/ for the current request")

0 comments on commit 8c60460

Please sign in to comment.