| import json |
| import os |
| from transformers import AutoTokenizer |
|
|
| |
| MODEL_PATH = "/home/at0842/ycl466704.ai13/.cache/huggingface/hub/models--openai--gpt-oss-20b/snapshots/6cee5e81ee83917806bbde320786a8fb61efebee" |
| INPUT_JSONL = "all_dupcleaned_data_turn.jsonl" |
| OUTPUT_JSONL = "all_dupcleaned_data_turn_gpt_oss_20b_pretokenized.jsonl" |
| MAX_LENGTH = 14436 |
|
|
| if not os.path.exists(MODEL_PATH): |
| print(f"❌ 錯誤:找不到模型路徑 {MODEL_PATH}") |
| exit() |
|
|
| print(f"⏳ 正在初始化 GPT-OSS 20B Tokenizer...") |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True) |
|
|
| |
| RETURN_TOKEN_ID = tokenizer.convert_tokens_to_ids("<|return|>") |
|
|
| def process_entry(entry, index): |
| try: |
| messages = entry.get("messages", []) |
| raw_tools = entry.get("tools", None) |
| |
| |
| formatted_tools = None |
| if raw_tools and isinstance(raw_tools, list): |
| formatted_tools = [] |
| for t in raw_tools: |
| if isinstance(t, dict) and "function" in t: |
| formatted_tools.append(t) |
| else: |
| formatted_tools.append({ |
| "type": "function", |
| "function": t |
| }) |
|
|
| if not isinstance(messages, list) or len(messages) < 2: |
| return None, "對話輪次不足" |
| |
| if messages[-1].get("role") != "assistant": |
| return None, f"最後一則不是 assistant (抓到的是: {messages[-1].get('role')})" |
|
|
| |
| full_ids = tokenizer.apply_chat_template( |
| messages, |
| tools=formatted_tools, |
| tokenize=True, |
| add_generation_prompt=False, |
| truncation=False |
| ) |
| |
| |
| |
| if full_ids[-1] != RETURN_TOKEN_ID: |
| full_ids.append(RETURN_TOKEN_ID) |
| |
| |
| actual_length = len(full_ids) |
| if actual_length > MAX_LENGTH: |
| return None, f"加上結束符後過長 ({actual_length} > {MAX_LENGTH})" |
|
|
| |
| context_ids = tokenizer.apply_chat_template( |
| messages[:-1], |
| tools=formatted_tools, |
| tokenize=True, |
| add_generation_prompt=True, |
| truncation=False |
| ) |
| |
| start_idx = len(context_ids) |
| |
| |
| if start_idx >= actual_length: |
| return None, "計算錯誤:Context 長度大於等於總長度" |
|
|
| |
| labels = [-100] * actual_length |
| for i in range(start_idx, actual_length): |
| labels[i] = full_ids[i] |
| |
| return { |
| "input_ids": full_ids, |
| "attention_mask": [1] * actual_length, |
| "labels": labels |
| }, None |
|
|
| except Exception as e: |
| return None, str(e) |
|
|
| |
| success_count = 0 |
| drop_count = 0 |
|
|
| print(f" 🚀 開始處理資料...") |
|
|
| with open(INPUT_JSONL, "r", encoding="utf-8") as f_in, \ |
| open(OUTPUT_JSONL, "w", encoding="utf-8") as f_out: |
| |
| for i, line in enumerate(f_in): |
| line = line.strip() |
| if not line: continue |
| |
| try: |
| entry = json.loads(line) |
| result, error_msg = process_entry(entry, i) |
| |
| if result: |
| f_out.write(json.dumps(result, ensure_ascii=False) + "\n") |
| success_count += 1 |
| |
| if success_count == 1: |
| print("\n" + "="*60) |
| print("【首筆資料切分預覽 - 修正結尾版】") |
| loss_indices = [idx for idx, val in enumerate(result['labels']) if val != -100] |
| loss_tokens = [result['input_ids'][idx] for idx in loss_indices] |
| target_text = tokenizer.decode(loss_tokens) |
| print(f" Target (計算 Loss 部分): \n{target_text}") |
| print(f" 總長度: {len(result['input_ids'])}") |
| |
| last_tokens = tokenizer.convert_ids_to_tokens(result['input_ids'][-3:]) |
| print(f" 序列末端三個 Token: {last_tokens}") |
| print("="*60 + "\n") |
| else: |
| drop_count += 1 |
| |
| except Exception as e: |
| drop_count += 1 |
|
|
| print(f" ✅ 處理完成!") |
| print(f" 成功筆數: {success_count}") |
| print(f" 丟棄筆數: {drop_count}") |
| print(f" 結果檔案: {OUTPUT_JSONL}") |