|
|
""" |
|
|
csv_to_jsonl.py |
|
|
|
|
|
|
|
|
Converts a CSV file with columns `prompt` and `response` into a JSONL file |
|
|
where each line is a JSON object: {"input": "<prompt>", "output": "<response>"} |
|
|
|
|
|
|
|
|
Run: |
|
|
python csv_to_jsonl.py |
|
|
|
|
|
|
|
|
This will create `dataset.jsonl` in the same folder. |
|
|
""" |
|
|
|
|
|
|
|
|
import csv |
|
|
import json |
|
|
|
|
|
|
|
|
INPUT_CSV = "dataset.csv" |
|
|
OUTPUT_JSONL = "dataset.jsonl" |
|
|
|
|
|
|
|
|
def csv_to_jsonl(in_csv: str = INPUT_CSV, out_jsonl: str = OUTPUT_JSONL): |
|
|
with open(in_csv, "r", encoding="utf-8") as f_in, open(out_jsonl, "w", encoding="utf-8") as f_out: |
|
|
reader = csv.DictReader(f_in) |
|
|
|
|
|
if "prompt" not in reader.fieldnames or "response" not in reader.fieldnames: |
|
|
raise ValueError("CSV must contain 'prompt' and 'response' columns") |
|
|
for row in reader: |
|
|
obj = {"input": row["prompt"].strip(), "output": row["response"].strip()} |
|
|
f_out.write(json.dumps(obj, ensure_ascii=False) + "\n") |
|
|
print(f"Wrote {out_jsonl}") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
csv_to_jsonl() |