-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathutils.py
More file actions
51 lines (42 loc) · 1.9 KB
/
utils.py
File metadata and controls
51 lines (42 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from datetime import datetime
import os
def load_env_vars(filepath='.env'):
with open(filepath) as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'):
# Remove 'export' if present and any leading/trailing whitespace
if line.startswith('export '):
line = line[7:].strip()
# Split on first '=' only
if '=' in line:
key, value = line.split('=', 1)
# Remove any quotes around the value
value = value.strip('\'"')
os.environ[key.strip()] = value
def print_colored(text, color):
if color == "red":
print(f"\033[91m{text}\033[0m")
elif color == "green":
print(f"\033[92m{text}\033[0m")
elif color == "blue":
print(f"\033[94m{text}\033[0m")
elif color == "purple":
print(f"\033[95m{text}\033[0m")
else:
raise Exception(f"Unknown color: {color}")
def extract_conversation(simulation_trace, to_str=False, skip_system=False, only_last_turn=False):
keep_roles = ["system", "assistant", "user"] if not skip_system else ["assistant", "user"]
real_conversation = [msg for msg in simulation_trace if msg["role"] in keep_roles]
if only_last_turn:
# get all the user turns
user_turn_idxs = [i for i, msg in enumerate(real_conversation) if msg["role"] == "user"]
last_user_turn_idx = user_turn_idxs[-1]
real_conversation = real_conversation[last_user_turn_idx + 1:]
real_conversation = [{"role": msg["role"], "content": msg["content"]} for msg in real_conversation] # only keep role and content
if to_str:
return "\n\n".join([f"[{msg['role']}] {msg['content']}" for msg in real_conversation])
else:
return real_conversation
def date_str():
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")