Upload make_json_from_oasst1_ja.py
Browse files- make_json_from_oasst1_ja.py +125 -0
make_json_from_oasst1_ja.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
import json
|
| 4 |
+
from tqdm import tqdm
|
| 5 |
+
|
| 6 |
+
ds = load_dataset("OpenAssistant/oasst1")
|
| 7 |
+
train = ds["train"]
|
| 8 |
+
val = ds["validation"]
|
| 9 |
+
|
| 10 |
+
# データフレームを連結
|
| 11 |
+
df = pd.concat([pd.DataFrame(train), pd.DataFrame(val)])
|
| 12 |
+
|
| 13 |
+
ds_ja = load_dataset("kunishou/oasst1-89k-ja")
|
| 14 |
+
|
| 15 |
+
# データフレーム
|
| 16 |
+
df_ja = pd.DataFrame(ds_ja["train"])
|
| 17 |
+
|
| 18 |
+
# 'message_id' をキーにして df_ja と df を結合し、df_ja の列名が優先されるようにします。
|
| 19 |
+
merged_df = df_ja.merge(df, on="message_id", how="left", suffixes=("", "_y"))
|
| 20 |
+
|
| 21 |
+
# 重複した列を削除します。
|
| 22 |
+
merged_df = merged_df.drop(
|
| 23 |
+
columns=[col for col in merged_df.columns if col.endswith("_y")]
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# 同じmessage_tree_idでデータをグループ化
|
| 27 |
+
grouped = merged_df.groupby("message_tree_id")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def find_longest_chain(group, root_message_id):
|
| 31 |
+
max_length = 0 # 最長のチェーンの長さを初期化
|
| 32 |
+
min_toxicity = 2.0 # 最小の毒性を初期化
|
| 33 |
+
leaf_id = None # 最長のチェーンの末端のメッセージIDを初期化
|
| 34 |
+
|
| 35 |
+
# グループ内の各行に対して処理を行う
|
| 36 |
+
for _, row in group.iterrows():
|
| 37 |
+
current_id = row["message_id"]
|
| 38 |
+
if current_id == root_message_id:
|
| 39 |
+
continue # ルートメッセージを処理しない
|
| 40 |
+
|
| 41 |
+
chain_length = 0 # チェーンの長さを初期化
|
| 42 |
+
toxicity = 1.0 # 毒性を初期化
|
| 43 |
+
|
| 44 |
+
# ルートメッセージにたどり着くまでチェーンを辿る
|
| 45 |
+
while current_id != "nan":
|
| 46 |
+
chain_length += 1
|
| 47 |
+
detoxify_data = group.loc[
|
| 48 |
+
group["message_id"] == current_id, "detoxify"
|
| 49 |
+
].iloc[0]
|
| 50 |
+
toxicity = (
|
| 51 |
+
detoxify_data["toxicity"] if detoxify_data is not None else 1.0
|
| 52 |
+
) # 毒性がない場合は1.0を代入
|
| 53 |
+
current_id = group.loc[
|
| 54 |
+
group["message_id"] == current_id, "parent_id"
|
| 55 |
+
].values[0]
|
| 56 |
+
|
| 57 |
+
# チェーンが現在の最長のチェーンと同じか長く、毒性が現在の最小の毒性以下の場合
|
| 58 |
+
if chain_length >= max_length and toxicity <= min_toxicity:
|
| 59 |
+
max_length = chain_length
|
| 60 |
+
min_toxicity = toxicity
|
| 61 |
+
leaf_id = row["message_id"] # 末端のメッセージIDを更新
|
| 62 |
+
|
| 63 |
+
return leaf_id # 最長のチェーンの末端のメッセージIDを返す
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
leafs = [] # 最長チェーンの末端のメッセージIDを格納するリストを初期化
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
for _, group in tqdm(grouped):
|
| 70 |
+
# parent_idがnullのメッセージを見つける(ルートメッセージ)
|
| 71 |
+
root_message = group[group["parent_id"] == "nan"].iloc[0]
|
| 72 |
+
root_message_id = root_message["message_id"]
|
| 73 |
+
|
| 74 |
+
# 英語かスペイン語か日本語
|
| 75 |
+
if root_message["lang"] in ["en", "es", "ja"]:
|
| 76 |
+
leaf_id = find_longest_chain(group, root_message_id)
|
| 77 |
+
leafs.append(leaf_id)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# 最も深いメッセージから辿ってメッセージを作成する関数
|
| 81 |
+
def create_message_path(message):
|
| 82 |
+
role = (
|
| 83 |
+
"User" if message["role"] == "prompter" else "Assistant"
|
| 84 |
+
) # メッセージの役割に応じて、UserかAssistantを選択
|
| 85 |
+
formatted_message = f"{role}:{message['text_ja']}" # 役割とメッセージを連結
|
| 86 |
+
if pd.isnull(message["parent_id"]): # 親メッセージがない場合
|
| 87 |
+
return [formatted_message]
|
| 88 |
+
else:
|
| 89 |
+
parent_messages = merged_df[
|
| 90 |
+
merged_df["message_id"] == message["parent_id"]
|
| 91 |
+
] # 親メッセージを検索
|
| 92 |
+
if parent_messages.empty: # 親メッセージが見つからない場合
|
| 93 |
+
return [formatted_message]
|
| 94 |
+
parent_message = parent_messages.iloc[0] # 親メッセージを取得
|
| 95 |
+
# 親メッセージから再帰的にメッセージを作成し、現在のメッセージを追加
|
| 96 |
+
return create_message_path(parent_message) + [formatted_message]
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
result = [] # 結果を格納するリストを初期化
|
| 100 |
+
for leaf_id in tqdm(leafs): # 進捗状況を表示するためにtqdmを使用
|
| 101 |
+
leaf_message = merged_df[merged_df["message_id"] == leaf_id].iloc[0] # 末端のメッセージを取得
|
| 102 |
+
leaf_text = create_message_path(leaf_message) # 末端のメッセージからメッセージのチェーンを作成
|
| 103 |
+
leaf_json = {}
|
| 104 |
+
odd = len(leaf_text) % 2
|
| 105 |
+
if len(leaf_text) <= 3: # メッセージのチェーンが3つ以下の場合
|
| 106 |
+
leaf_json["instruction"] = leaf_text[0].replace("User:", "", 1)
|
| 107 |
+
leaf_json["input"] = ""
|
| 108 |
+
leaf_json["output"] = leaf_text[1].replace("Assistant:", "", 1)
|
| 109 |
+
else: # メッセージのチェーンが4つ以上の場合
|
| 110 |
+
instruction = ""
|
| 111 |
+
for t in leaf_text[0 : -2 - odd]: # 最後の2つのメッセージを除いて、指示文を作成
|
| 112 |
+
instruction += t + " "
|
| 113 |
+
leaf_json["instruction"] = instruction
|
| 114 |
+
leaf_json["input"] = leaf_text[-2 - odd] # 入力メッセージを設定
|
| 115 |
+
leaf_json["output"] = leaf_text[-1 - odd].replace(
|
| 116 |
+
"Assistant:", "", 1
|
| 117 |
+
) # 出力メッセージを設定
|
| 118 |
+
result.append(leaf_json) # 結果リストにJSONを追加
|
| 119 |
+
|
| 120 |
+
# JSON データを作成
|
| 121 |
+
json_data = json.dumps(result, ensure_ascii=False, indent=4)
|
| 122 |
+
|
| 123 |
+
# JSON をファイルに保存
|
| 124 |
+
with open("oasst1_ja.json", "w", encoding="utf-8") as json_file:
|
| 125 |
+
json_file.write(json_data)
|