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]) — 데이터셋의 단일 데이터 항목입니다. 데이터셋 유형에 따라 example의 키가 달라질 수 있습니다.

반환값

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 형식으로 변환합니다.

이 함수는 대화 데이터를 OpenAI의 ChatML 형식에 맞게 수정합니다:

  • 메시지 딕셔너리에서 키 "from"를 "role"로 대체합니다.
  • 메시지 딕셔너리에서 키 "value"의 값을 "content"로 대체합니다.
  • ChatML과의 일관성을 위해 "conversations"를 "messages"로 이름 변경합니다.

예:

>>> 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”과 “rejected” 완료 문장 사이의 가장 긴 공통 접두사.
  • "chosen": 프롬프트를 제거한 “chosen” 완료 응답의 나머지 부분.
  • "rejected": 프롬프트를 제거한 “rejected” completion의 나머지 부분.

선호 데이터 예시에서 공유 프롬프트를 추출합니다. 여기서 프롬프트는 선택된 완료와 거부된 완료 모두에 암묵적으로 포함되어 있습니다.

이 함수는 “chosen”와 “rejected” 완료 결과 간의 대화 턴에서 가장 긴 공통 시퀀스(접두사)를 식별하고 이를 프롬프트로 추출합니다. 그런 다음 해당 프롬프트를 각각의 “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.'}]}

또는 Dataset의 map 메서드를 사용하여:

>>> 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) — unpair할 preference dataset입니다. dataset에는 "chosen", "rejected" 컬럼이 필수이며, 선택적으로 "prompt" 컬럼을 포함할 수 있습니다.
  • **map_kwargs (dict, 선택 사항) — 선호도를 분리할 때 데이터셋의 map 메서드에 전달할 추가 키워드 인자입니다.

선호 데이터셋을 unpair합니다.

출력에는 "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