-
Notifications
You must be signed in to change notification settings - Fork 126
/
xtuner2sharegpt.py
59 lines (49 loc) · 2.05 KB
/
xtuner2sharegpt.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
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
import json
# Given JSON data in string format
# original_json_data = """
# [
# {
# "conversation": [
# {"system": "system", "input": "input", "output": "output"},
# {"input": "input", "output": "output"},
# {"input": "input", "output": "output"}
# ]
# },
# {
# "conversation": [
# {"system": "system", "input": "input", "output": "output"},
# {"input": "input", "output": "output"},
# {"input": "input", "output": "output"}
# ]
# }
# ]
# """
# Parse the original JSON data into Python objects
def convert_xtuner_to_sharegpt(input_path, output_path):
with open(input_path, 'r', encoding='utf-8') as file:
data = json.load(file)
# Initialize a new list to hold transformed conversations
transformed_conversations = []
for conversation_group in data:
system = conversation_group["conversation"][0]["system"]
# Extract human and GPT inputs and outputs from each conversation pair
transformed_pairs = []
for pair in conversation_group["conversation"]:
# if "system" in pair:
# continue # Skip the initial system entry
transformed_pairs.append({"from": "human", "value": pair["input"]})
transformed_pairs.append({"from": "gpt", "value": pair["output"]})
# print(transformed_pairs)
# Add the transformed conversation group to the result list
transformed_conversation = {
"conversations": transformed_pairs,
"system": system,
}
transformed_conversations.append(transformed_conversation)
# Convert the transformed Python objects back into JSON format
with open(output_path, "w", encoding='utf-8') as output_file:
json.dump(transformed_conversations, output_file, ensure_ascii=False, indent=4)
if __name__ == "__main__":
input_path = "../datasets/scientist.json"
output_path = "../datasets/scientist_sharegpt.json"
convert_xtuner_to_sharegpt(input_path, output_path)