# Veri Yardımcıları

## is_conversational[[trl.is_conversational]]

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

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

[Kaynak](https://github.com/huggingface/trl/blob/v1.18.0/trl/data_utils.py#L126)

**Parametreler:**

example (`dict[str, Any]`) : Bir veri setinin tek bir veri girişi. Örnek, veri seti türüne bağlı olarak farklı anahtarlara sahip olabilir.

**Dönüş:** `bool`

Veriler bir konuşma formatındaysa `True`, aksi takdirde `False`.

Örneğin konuşma formatında olup olmadığını kontrol edin.

Örnekler:

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

[Kaynak](https://github.com/huggingface/trl/blob/v1.18.0/trl/data_utils.py#L967)

**Parametreler:**

example (`dict[str, list]`) : Mesaj listesini içeren tek bir veri girişi.

**Dönüş:** `dict[str, list]`

ChatML stiline yeniden biçimlendirilmiş örnek.

Alanları `from` ve `value` olan bir sohbet veri setini ChatML formatına dönüştürün.

Bu fonksiyon, konuşma verilerini OpenAI'nin ChatML biçimiyle uyumlu hale getirmek için değiştirir:
- Mesaj sözlüklerinde `"from"` anahtarını `"role"` ile değiştirir.
- Mesaj sözlüklerinde `"value"` anahtarını `"content"` ile değiştirir.
- Tutarlılık için `"conversations"` adını ChatML ile uyumlu olacak şekilde `"messages"` olarak değiştirir.

Örnek:
```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])
```

[Kaynak](https://github.com/huggingface/trl/blob/v1.18.0/trl/data_utils.py#L548)

**Parametreler:**

example (`dict[str, list]`) : Tercih veri setindeki tek bir veri girdisini temsil eden sözlük. `"chosen"` ve `"rejected"` anahtarlarını içermelidir; her bir değer konuşma veya standart (`str`) biçiminde olabilir.

**Dönüş:** `dict[str, list]`

Şu içeriğe sahip bir sözlük:
- `"prompt"`: "chosen" ve "rejected" tamamlamalar arasındaki en uzun ortak önek.
- `"chosen"`: "chosen" tamamlamasının, istem çıkarıldıktan sonra kalan kısmı.
- `"rejected"`: "rejected" tamamlamanın, prompt çıkarıldıktan sonra kalan kısmı.

Bir tercih verisi örneğinden, hem seçilen hem de reddedilen tamamlamalar içinde örtük olarak bulunan ortak istemciyi (prompt) çıkarır.

Fonksiyon, "chosen" ve "rejected" tamamlamaları arasındaki konuşma turlarının en uzun ortak dizisini (önek) belirler ve bunu prompt olarak çıkarır. Ardından bu prompt'u ilgili "chosen" ve "rejected" tamamlamalarından kaldırır.

Örnekler:

```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.'}]}
```

Veya [Dataset](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.Dataset)'in `map` yöntemiyle:

```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)
```

[Kaynak](https://github.com/huggingface/trl/blob/v1.18.0/trl/data_utils.py#L445)

**Parametreler:**

dataset ([Dataset](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.Dataset) veya [DatasetDict](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.DatasetDict) veya [IterableDataset](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.IterableDataset) veya [IterableDatasetDict](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.IterableDatasetDict)) : Eşleştirilmeyecek tercih verisi kümesi. Veri kümesinin `"chosen"`, `"rejected"` ve isteğe bağlı olarak `"prompt"` sütunları olmalıdır.

- ****map_kwargs** (`dict`, *isteğe bağlı*) : Tercihleri eşleştirmede (unpairing) veri setinin map yöntemine geçirilecek ek anahtar kelime argümanları.

**Dönüş:**

[Dataset](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.Dataset) veya [DatasetDict](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.DatasetDict) veya [IterableDataset](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.IterableDataset) veya [IterableDatasetDict](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.IterableDatasetDict): Eşleşmemiş tercih veri seti.

Bir tercih veri setini eşleştirmeden ayırın.

Çıktı, `"prompt"`, `"completion"` ve `"label"` ile birlikte her seçilen ve reddedilen satır için çoğaltılan ek sütunları içerir.

Örnek:

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

