TRL documentation

データユーティリティ

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

データユーティリティ

is_conversational

trl.is_conversational

< >

( example: dict[str, typing.Any] ) → bool

パラメータ

  • example (dict[str, Any]) — データセットの単一のデータエントリです。データセットの種類によって、example に含まれるキーが異なります。

戻り値

bool

データが会話形式の場合はTrue、それ以外の場合はFalse。

例が会話形式かどうかを確認します。

例:

>>> example = {"prompt": [{"role": "user", "content": "What color is the sky?"}]}
>>> is_conversational(example)
True

>>> example = {"prompt": "The sky is"}
>>> is_conversational(example)
False

maybe_convert_to_chatml

trl.maybe_convert_to_chatml

< >

( example: dict[str, list] ) → dict[str, list]

パラメータ

  • example (dict[str, list]) — メッセージのリストを含む単一のデータエントリです。

戻り値

dict[str, list]

ChatMLスタイルに整形された例。

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

< >

( example: dict[str, collections.abc.Sequence] ) → dict[str, list]

パラメータ

  • example (dict[str, list]) — 好意データセット内の単一のデータエントリを表す辞書です。"chosen" と "rejected" というキーを含まなければなりません。それぞれの値は、会話形式または標準形式(str)のいずれかです。

戻り値

dict[str, list]

以下の内容を含む辞書:

  • "prompt": “chosen”と“rejected”の補完の間の最長共通接頭辞。
  • "chosen": プロンプトを除去した「chosen」補完の残り部分。
  • "rejected": プロンプトを除去した「rejected」補完の残り部分。

プロンプトが選択された補完と拒否された補完の両方に暗黙的に含まれている設定データのエグザンプルから、共有プロンプトを抽出します。

この関数は、“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

< >

( 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}
Update on GitHub