# データユーティリティ

## is_conversational[[trl.is_conversational]]

#### trl.is_conversational[[trl.is_conversational]]

```python
trl.is_conversational(example: dict[str, typing.Any])
```

[ソース](https://github.com/huggingface/trl/blob/v1.18.2/trl/data_utils.py#L126)

**パラメータ:**

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

**戻り値:** `bool`

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

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

例:

```python
>>> 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]]

#### trl.maybe_convert_to_chatml[[trl.maybe_convert_to_chatml]]

```python
trl.maybe_convert_to_chatml(example: dict[str, list])
```

[ソース](https://github.com/huggingface/trl/blob/v1.18.2/trl/data_utils.py#L967)

**パラメータ:**

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

**戻り値:** `dict[str, list]`

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

`from` と `value` フィールドを持つ対話データセットを ChatML 形式に変換します。

この関数は、OpenAIのChatML形式に合わせるために会話データを修正します:
- メッセージ辞書内のキー`"from"`を`"role"`に置き換えます。
- メッセージ辞書内のキー`"value"`を`"content"`に置き換えます。
- ChatMLとの一貫性を保つため、`"conversations"`を`"messages"`に名前変更します。

例:
```python
>>> 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]]

#### trl.extract_prompt[[trl.extract_prompt]]

```python
trl.extract_prompt(example: dict[str, collections.abc.Sequence])
```

[ソース](https://github.com/huggingface/trl/blob/v1.18.2/trl/data_utils.py#L548)

**パラメータ:**

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

**戻り値:** `dict[str, list]`

以下の内容を含む辞書:
- `"prompt"`: "chosen"と"rejected"の補完の間の最長共通接頭辞。
- `"chosen"`: プロンプトを除いた "chosen" 補完の残り部分。
- `"rejected"`: プロンプトを除去した "rejected" 補完の残り部分。

好意的データの例から共有プロンプトを抽出します。この場合、プロンプトは選択された補完と拒否された補完の両方に暗黙的に含まれています。

この関数は、"chosen" と "rejected" の補完における会話ターンの共通部分（プレフィックス）のうち最長のシーケンスを特定し、これをプロンプトとして抽出します。その後、それぞれの "chosen" と "rejected" の補完からこのプロンプトを削除します。

例:

```python
>>> 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](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.Dataset)の`map`メソッドを使用します:

```python
>>> 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]]

#### trl.unpair_preference_dataset[[trl.unpair_preference_dataset]]

```python
trl.unpair_preference_dataset(dataset: typing.Union[~DatasetType, ~IterableDatasetType], **map_kwargs)
```

[ソース](https://github.com/huggingface/trl/blob/v1.18.2/trl/data_utils.py#L445)

**パラメータ:**

dataset ([Dataset](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.Dataset) または [DatasetDict](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.DatasetDict) または [IterableDataset](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.IterableDataset) または [IterableDatasetDict](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.IterableDatasetDict)) : 対を解除する好意データセット。データセットには `"chosen"`、`"rejected"` の列が必要で、`"prompt"` は任意です。

- ****map_kwargs** (`dict`、*任意*) : 好みをペアから外す際に、データセットの map メソッドに渡す追加のキーワード引数。

**戻り値:**

[Dataset](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.Dataset) または [DatasetDict](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.DatasetDict) または [IterableDataset](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.IterableDataset) または [IterableDatasetDict](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.IterableDatasetDict): 非ペアの好みデータセット。

Unpair a preference dataset.

出力には`"prompt"`、`"completion"`、`"label"`が含まれ、追加の列があればそれも含まれます。これらは各chosen行とrejected行に対して複製されます。

例:

```python
>>> 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}
```

