TRL documentation

Tiện ích dữ liệu

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Tiện ích dữ liệu

is_conversational

trl.is_conversational

< >

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

Tham số

  • example (dict[str, Any]) — Một mục dữ liệu đơn lẻ của tập dữ liệu. Mục này có thể có các khóa khác nhau tùy thuộc vào loại tập dữ liệu.

Trả về

bool

True nếu dữ liệu ở định dạng hội thoại, False trong trường hợp ngược lại.

Kiểm tra xem ví dụ có ở định dạng hội thoại hay không.

Ví dụ:

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

Tham số

  • example (dict[str, list]) — Một mục dữ liệu duy nhất chứa một danh sách các tin nhắn.

Trả về

dict[str, list]

Ví dụ được định dạng lại theo phong cách ChatML.

Chuyển đổi bộ dữ liệu hội thoại có các trường from và value sang định dạng ChatML.

Hàm này chỉnh sửa dữ liệu hội thoại để phù hợp với định dạng ChatML của OpenAI:

  • Thay thế khóa "from" bằng "role" trong các từ điển tin nhắn.
  • Thay thế khóa "value" bằng "content" trong các từ điển tin nhắn.
  • Đổi tên "conversations" thành "messages" để nhất quán với ChatML.

Ví dụ:

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

Tham số

  • example (dict[str, list]) — Một từ điển đại diện cho một mục dữ liệu duy nhất trong bộ dữ liệu preference. Nó phải chứa các khóa "chosen" và "rejected", trong đó mỗi giá trị là dạng hội thoại hoặc chuẩn (str).

Trả về

dict[str, list]

Một từ điển chứa:

  • "prompt": Tiền tố chung dài nhất giữa các phần hoàn thành “chosen” và “rejected”.
  • "chosen": Phần còn lại của hoàn thành “chosen”, với prompt đã được loại bỏ.
  • "rejected": Phần còn lại của hoàn thành “rejected”, với prompt đã được loại bỏ.

Trích xuất prompt chung từ một ví dụ dữ liệu ưu tiên, trong đó prompt được ngụ ý trong cả phần hoàn thành được chọn và phần hoàn thành bị từ chối.

Hàm này xác định chuỗi chung dài nhất (tiền tố) của các lượt hội thoại giữa các phần hoàn thành “chosen” và “rejected”, sau đó trích xuất chuỗi này làm prompt. Tiếp theo, hàm loại bỏ prompt này khỏi các phần hoàn thành “chosen” và “rejected” tương ứng.

Ví dụ:

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

Hoặc, với phương thức map của 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 )

Tham số

  • dataset (Dataset hoặc DatasetDict hoặc IterableDataset hoặc IterableDatasetDict) — Bộ dữ liệu preference để tách cặp. Bộ dữ liệu phải có các cột "chosen", "rejected" và tùy chọn "prompt".
  • **map_kwargs (dict, tuỳ chọn) — Các đối số từ khóa bổ sung để truyền vào phương thức map của bộ dữ liệu khi tách cặp sở thích.

Gỡ ghép cặp một bộ dữ liệu ưu tiên.

Đầu ra bao gồm "prompt", "completion" và "label" cùng bất kỳ cột bổ sung nào, trong đó các cột này được nhân đôi cho mỗi hàng được chọn và hàng bị từ chối.

Ví dụ:

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