import json from tqdm import tqdm def convert_to_jsonl(input_file: str, output_file: str): """ Convert the dataset from JSON format to JSONL format with prompt-response pairs. Ensures correct ordering of prompt-response pairs. Filters out conversations containing the word 'transcript'. """ print(f"Reading input file: {input_file}") with open(input_file, "r", encoding="utf-8") as f: data = json.load(f) conversations = data["messages"] total_pairs = len(conversations) // 2 print(f"Found {total_pairs} conversation pairs") print(f"Writing to output file: {output_file}") skipped_transcript = 0 with open(output_file, "w", encoding="utf-8") as out: for i in tqdm(range(0, len(conversations), 2), desc="Converting conversations"): if i + 1 >= len(conversations): print(f"Warning: Skipping incomplete conversation at index {i}") continue # Ensure correct role ordering if conversations[i]["role"] != "user" or conversations[i+1]["role"] != "assistant": print(f"Warning: Incorrect role ordering at index {i}") print(f"Expected user-assistant pair, got {conversations[i]['role']}-{conversations[i+1]['role']}") continue prompt = conversations[i]["content"] response = conversations[i+1]["content"] # Skip if 'transcript' appears in either prompt or response if "transcript" in prompt.lower() or "transcript" in response.lower(): skipped_transcript += 1 continue # Create the conversation pair pair = { "prompt": prompt, "response": response } # Write to JSONL file out.write(json.dumps(pair, ensure_ascii=False) + "\n") print(f"Conversion completed! Skipped {skipped_transcript} conversations containing 'transcript'") if __name__ == "__main__": input_file = "final.json" output_file = "dataset.jsonl" convert_to_jsonl(input_file, output_file)