Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
110 lines (95 loc) · 3.18 KB
/
Copy pathutils.py
File metadata and controls
110 lines (95 loc) · 3.18 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import json
import ast
import os
def read_jsonl(path):
data = []
with open(path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
data.append(json.loads(line))
return data
def read_json(path) :
with open(path, "r", encoding='utf-8') as f :
data = json.load(f)
return data
def print_json(path, data) :
dir_path = os.path.dirname(path)
if dir_path:
os.makedirs(dir_path, exist_ok=True)
with open(path, "w", encoding="utf-8") as f :
f.write(json.dumps(data, ensure_ascii=False, indent=2) + "\n")
def extract_bracket_content1(s):
start = s.find('[')
end = s.rfind(']')
if start != -1 and end != -1 and start < end:
return s[start:end+1]
return None
def extract_bracket_content2(s):
start = s.find('{')
end = s.rfind('}')
if start != -1 and end != -1 and start < end:
return s[start:end+1]
return None
def solve_dict(answer_content) :
ans_dict = extract_bracket_content2(answer_content)
if ans_dict :
try :
return dict(ast.literal_eval(ans_dict))
except :
return dict()
else :
return dict()
def solve_list(answer_content) :
ans_list = extract_bracket_content1(answer_content)
if ans_list :
try :
return list(ast.literal_eval(ans_list))
except :
return list()
else :
return list()
import yaml
def read_yaml(file_path) :
with open(file_path, 'r', encoding='utf-8') as file:
data = yaml.safe_load(file)
return data
def has_invalid_utf8(obj):
if isinstance(obj, str):
try:
obj.encode("utf-8")
return False
except UnicodeEncodeError:
return True
elif isinstance(obj, list):
return any(has_invalid_utf8(x) for x in obj)
elif isinstance(obj, dict):
return any(has_invalid_utf8(v) for v in obj.values())
else:
return False
def format_interaction_log(prior_interaction_log):
interaction_log_str = ""
for i, interaction in enumerate(prior_interaction_log):
interaction_log_str += f"### Session {i+1}\n\n"
for message in interaction['dialogue']:
role_str = "User" if message['role'] == "user" else "AI Assistant"
content = message['content']
interaction_log_str += f"#### {role_str}\n\n{content}\n\n"
interaction_log_str += "---\n\n"
return interaction_log_str.strip()
def my_format_interaction_log(prior_interaction_log):
interaction_log_str = ""
for i, interaction in enumerate(prior_interaction_log):
interaction_log_str += f"### Session {i+1}\n\n"
for message in interaction:
role_str = "User" if message['role'] == "user" else "AI Assistant"
content = message['content']
interaction_log_str += f"#### {role_str}\n\n{content}\n\n"
interaction_log_str += "---\n\n"
return interaction_log_str.strip()
def load_prompt(prompt_name) :
path = f"assets/{prompt_name}.txt"
with open(path, "r", encoding="utf-8") as f:
content = f.read()
return content