-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject_instructions.py
33 lines (26 loc) · 1006 Bytes
/
inject_instructions.py
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
import sys
import json
def modify_json(json_file, text_file):
try:
with open(json_file, 'r', encoding='utf-8') as file:
data = json.load(file)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
sys.exit(1)
with open(text_file, 'r', encoding='utf-8') as file:
prefix = file.read().strip()
for item in data:
for conversation in item['conversations']:
if conversation['from'] == 'human':
conversation['value'] = f"{prefix}\n{conversation['value']}"
output_file = "modified_data.json"
with open(output_file, 'w', encoding='utf-8') as file:
json.dump(data, file, indent=2, ensure_ascii=False)
print(f"Modified data saved to {output_file}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python app.py <json_file> <text_file>")
sys.exit(1)
json_file = sys.argv[1]
text_file = sys.argv[2]
modify_json(json_file, text_file)