TRL documentation

أدوات البيانات

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v1.17.1).
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]) — إدخال بيانات واحد من مجموعة البيانات. يمكن أن يحتوي الإدخال على مفاتيح مختلفة اعتمادًا على نوع مجموعة البيانات.

يعيد

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.

تعديل هذه الدالة لبيانات المحادثة لتتوافق مع تنسيق ChatML الخاص بـ OpenAI:

  • يستبدل المفتاح "from" بـ "role" في قواميس الرسائل.
  • يستبدل المفتاح "value" بـ "content" في قواميس الرسائل.
  • يعيد تسمية "conversations" إلى "messages" من أجل الاتساق مع ChatML.

مثال:

>>> 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": الجزء المتبقي من إكمال “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.'}]}

أو، باستخدام طريقة map الخاصة بـ Dataset:

>>> 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 في مجموعة البيانات عند فصل التفضيلات.

إلغاء إقران مجموعة بيانات التفضيلات.

تحتوي المخرجات على "prompt" و"completion" و"label" بالإضافة إلى أي أعمدة إضافية، حيث يتم تكرارها لكل صف مختار ومرفوض.

مثال:

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