Spaces:
Sleeping
Sleeping
File size: 1,005 Bytes
10c1f9c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
def transform_data(data):
# Create the base structure for the transformed data
transformed = {'conversations': []}
# Check for system message type, if any, before human input and output
system_msg = next((msg for msg in data.get('messages', []) if msg.get('role') == 'system'), None)
input_msg = next((msg for msg in data.get('messages', []) if msg.get('role') == 'user'), None)
output_msg = next((msg for msg in data.get('messages', []) if msg.get('role') == 'assistant'), None)
# Include system message if present
if system_msg:
transformed['conversations'].append({'from': 'system', 'value': system_msg['content']})
# Handle input and instruction
if input_msg:
transformed['conversations'].append({'from': 'human', 'value': input_msg['content']})
# Include GPT message if present and after human input
if output_msg:
transformed['conversations'].append({'from': 'gpt', 'value': output_msg['content']})
return transformed
|