-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_oai_to_autotrain.py
More file actions
35 lines (30 loc) · 1.18 KB
/
Copy pathconvert_oai_to_autotrain.py
File metadata and controls
35 lines (30 loc) · 1.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
import pandas as pd
import json
import os
def jsonl_to_dataframe(jsonl_path):
data = []
with open(jsonl_path, 'r') as file:
for line in file:
obj = json.loads(line)
record = {
'instruction': '',
'input': '', # This is left blank as its intended usage is instruction + input --> output
'output': '',
'text': ''
}
# Parse the jsonl
for message in obj['messages']:
if message['role'] == 'user':
record['instruction'] = message['content']
elif message['role'] == 'assistant':
record['output'] = message['content']
# Format text in the way the model expects it (This is the Mistral format)
record['text'] = f"<s>[INST] {record['instruction']} [/INST] {record['output']}</s>"
data.append(record)
df = pd.DataFrame(data)
return df
# Load OpenAI formatted jsonl
proj_path = os.getcwd()
jsonl_path = os.path.join(proj_path, 'data', 'all_messages.jsonl')
df = jsonl_to_dataframe(jsonl_path)
df.to_csv(os.path.join(proj_path, 'data/train.csv'), index=False)