INNER CODE UNIT · Python
extract_instructions
KodCode-AI/kodcode · pipeline/step1.3_process_and_sanitize.py:100
def extract_instructions(input_file, output_file):
"""
Extracts instructions from the assistant messages and saves them to an output file.
"""
with open(input_file, 'r') as f_in, open(output_file, 'w') as f_out:
for line in tqdm(f_in, desc="Extracting Instructions"):
data = json.loads(line)
messages = data.get("messages", [])
assistant_message = next((msg for msg in messages if msg["role"] == "assistant"), None)
if not assistant_message:
print("No assistant message found. Skipping.")
continue
instruction = extract_instruction(assistant_message["content"])
if not instruction:
print("Empty instruction after extraction. Skipping.")
continue