TRL documentation
Veri Yardımcıları
Veri Yardımcıları
is_conversational
trl.is_conversational
< source >( example: dict[str, typing.Any] ) → bool
Örneğin konuşma formatında olup olmadığını kontrol edin.
maybe_convert_to_chatml
trl.maybe_convert_to_chatml
< source >( example: dict[str, list] ) → dict[str, list]
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:
>>> 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]
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ğerin içeriği konuşma tabanlı veya standart (str) olabilir.
Dönüş değeri
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” tamamlamanın, istem çıkarıldıktan sonra kalan kısmı."rejected": “rejected” tamamlamanın, prompt çıkarıldıktan sonra kalan kısmı.
Bir tercih veri ö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 promptu ilgili “chosen” ve “rejected” tamamlamalarından kaldırır.
Örnekler:
>>> 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'in map yöntemiyle:
>>> 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 )
Parametreler
- dataset (Dataset veya DatasetDict veya IterableDataset veya 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 iletilecek ek anahtar kelime argümanları.
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:
>>> 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}