TRL documentation
データユーティリティ
はじめに
コンセプトガイド
Trainers
ハウツーガイド
コマンドラインインターフェース (CLI)Jobs を使用したトレーニングトレーニングのカスタマイズメモリ使用量の削減トレーニングの高速化トレーニングの分散AWS TrainiumでのトレーニングAMD GPUでのトレーニング100万トークン超のトレーニング学習済みモデルの使用
統合
例
API
実験的
データユーティリティ
is_conversational
trl.is_conversational
< source >( example: dict[str, typing.Any] ) → bool
例が会話形式かどうかを確認します。
maybe_convert_to_chatml
trl.maybe_convert_to_chatml
< source >( example: dict[str, list] ) → dict[str, list]
from と value フィールドを持つ対話データセットを ChatML 形式に変換します。
この関数は、OpenAIのChatML形式に合わせるために会話データを修正します:
- メッセージ辞書内のキー
"from"を"role"に置き換えます。 - メッセージ辞書内のキー
"value"を"content"に置き換えます。 - ChatMLとの一貫性を保つため、
"conversations"を"messages"に名前変更します。
例:
>>> from trl import maybe_convert_to_chatml
>>> example = {
... "conversations": [
... {"from": "user", "value": "What color is the sky?"},
... {"from": "assistant", "value": "It is blue."},
... ]
... }
>>> maybe_convert_to_chatml(example)
{'messages': [{'role': 'user', 'content': 'What color is the sky?'},
{'role': 'assistant', 'content': 'It is blue.'}]}extract_prompt
trl.extract_prompt
< source >( example: dict[str, collections.abc.Sequence] ) → dict[str, list]
プロンプトが選択された補完と拒否された補完の両方に暗黙的に含まれている設定データのエグザンプルから、共有プロンプトを抽出します。
この関数は、“chosen”と“rejected”の補完における会話ターンの共通部分(プレフィックス)のうち最長のシーケンスを特定し、これをプロンプトとして抽出します。その後、対応する“chosen”および“rejected”の補完からこのプロンプトを削除します。
例:
>>> example = {
... "chosen": [
... {"role": "user", "content": "What color is the sky?"},
... {"role": "assistant", "content": "It is blue."},
... ],
... "rejected": [
... {"role": "user", "content": "What color is the sky?"},
... {"role": "assistant", "content": "It is green."},
... ],
... }
>>> extract_prompt(example)
{'prompt': [{'role': 'user', 'content': 'What color is the sky?'}],
'chosen': [{'role': 'assistant', 'content': 'It is blue.'}],
'rejected': [{'role': 'assistant', 'content': 'It is green.'}]}または、Datasetのmapメソッドを使用します:
>>> from trl import extract_prompt
>>> from datasets import Dataset
>>> dataset_dict = {
... "chosen": [
... [
... {"role": "user", "content": "What color is the sky?"},
... {"role": "assistant", "content": "It is blue."},
... ],
... [
... {"role": "user", "content": "Where is the sun?"},
... {"role": "assistant", "content": "In the sky."},
... ],
... ],
... "rejected": [
... [
... {"role": "user", "content": "What color is the sky?"},
... {"role": "assistant", "content": "It is green."},
... ],
... [
... {"role": "user", "content": "Where is the sun?"},
... {"role": "assistant", "content": "In the sea."},
... ],
... ],
... }
>>> dataset = Dataset.from_dict(dataset_dict)
>>> dataset = dataset.map(extract_prompt)
>>> dataset[0]
{'prompt': [{'role': 'user', 'content': 'What color is the sky?'}],
'chosen': [{'role': 'assistant', 'content': 'It is blue.'}],
'rejected': [{'role': 'assistant', 'content': 'It is green.'}]}unpair_preference_dataset
trl.unpair_preference_dataset
< source >( dataset: typing.Union[~DatasetType, ~IterableDatasetType]**map_kwargs )
パラメータ
- dataset (Dataset または DatasetDict または IterableDataset または IterableDatasetDict) —
ペアを解除する好意度データセットです。データセットには
"chosen"、"rejected"の列が必要で、"prompt"は任意です。 - **map_kwargs (
dict、任意) — 好みをペアリング解除する際に、データセットのmapメソッドに渡す追加のキーワード引数。
Unpair a preference dataset.
出力には"prompt"、"completion"、"label"に加えて、追加のカラムが含まれ、これらは各chosen行とrejected行に対して複製されます。
例:
>>> from datasets import Dataset
>>> dataset_dict = {
... "prompt": ["The sky is", "The sun is"],
... "chosen": [" blue.", "in the sky."],
... "rejected": [" green.", " in the sea."],
... }
>>> dataset = Dataset.from_dict(dataset_dict)
>>> dataset = unpair_preference_dataset(dataset)
>>> dataset
Dataset({
features: ['prompt', 'completion', 'label'],
num_rows: 4
})
>>> dataset[0]
{'prompt': 'The sky is', 'completion': ' blue.', 'label': True}