#!/usr/bin/env python import pandas as pd import re def process(df: pd.DataFrame) -> pd.DataFrame: pattern = r'<\|im_start\|>(\w+)\n(.*?)<\|im_end\|>' def process_text(text): matches = re.findall(pattern, text, re.DOTALL) # remove the 'system' in matches matches = [(k, v) for k, v in matches if k != 'system'] return dict(matches) result = df['text'].apply(process_text) # remove the columns that named 'text' and 'system' result = result.drop(columns=['text', 'system']) return pd.DataFrame(result.tolist()) def main(file: str = None) -> None: if file != None: df = pd.read_json(file, lines=True) result = process(df) print(result) else: data = { "text": [ '<|im_start|>system You are a helpful assistant<|im_end|> <|im_start|>user "阿猛,你觉得做视频最重要的是什么,除了内容好,还有什么能让视频更吸引人呢?"<|im_end|> <|im_start|>assistant 视频做的很好,但美中不足的是。。。时长太短,不!够!看!<|im_end|>', '<|im_start|>system You are a helpful assistant<|im_end|> <|im_start|>user "要如何學會加法"<|im_end|> <|im_start|>assistant 這個問題,我也不太會<|im_end|>', ] } df = pd.DataFrame(data) result = process(df) print(result) if __name__ == "__main__": import fire fire.Fire(main)