TRL 文件
資料工具
並獲得增強的文件體驗
開始使用
資料工具
is_conversational
trl.is_conversational
< 原始碼 >( example: dict ) → bool
檢查示例是否為對話格式。
apply_chat_template
trl.apply_chat_template
< 原始碼 >( example: dict tokenizer: PreTrainedTokenizerBase tools: typing.Optional[list[typing.Union[dict, typing.Callable]]] = None **template_kwargs )
將聊天模板應用於對話示例,同時應用 tools
中函式列表的模式。
更多詳情,請參見 maybe_apply_chat_template()。
maybe_apply_chat_template
trl.maybe_apply_chat_template
< 原始碼 >( example: dict tokenizer: PreTrainedTokenizerBase tools: typing.Optional[list[typing.Union[dict, typing.Callable]]] = None **template_kwargs: typing.Any ) → dict[str, str]
引數
- example (
dict[str, list[dict[str, str]]
) — 表示對話資料集單個數據條目的字典。每個資料條目可以有不同的鍵,具體取決於資料集型別。支援的資料集型別有:- 語言建模資料集:
"messages"
。 - 僅提示資料集:
"prompt"
。 - 提示-完成資料集:
"prompt"
和"completion"
。 - 偏好資料集:
"prompt"
、"chosen"
和"rejected"
。 - 帶隱式提示的偏好資料集:
"chosen"
和"rejected"
。 - 未配對偏好資料集:
"prompt"
、"completion"
和"label"
。
對於鍵
"messages"
、"prompt"
、"chosen"
、"rejected"
和"completion"
,其值是訊息列表,其中每個訊息是一個包含鍵"role"
和"content"
的字典。 - 語言建模資料集:
- tokenizer (
PreTrainedTokenizerBase
) — 用於應用聊天模板的分詞器。 - tools (
list[Union[dict, Callable]]
或None
,可選,預設為None
) — 模型可訪問的工具列表(可呼叫函式)。如果模板不支援函式呼叫,此引數將無效。 - **template_kwargs (
Any
,可選) — 傳遞給模板渲染器的額外關鍵字引數。聊天模板將可以訪問這些引數。
返回
dict[str, str]
應用了聊天模板的格式化示例。
如果示例是對話格式,則對其應用聊天模板。
備註
此函式不改變鍵,但語言建模資料集除外,其中
"messages"
會被替換為"text"
。對於僅提示的資料,如果最後一個角色是
"user"
,則將生成提示新增到提示中。否則,如果最後一個角色是"assistant"
,則繼續最後一條訊息。
示例
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-128k-instruct")
>>> example = {
... "prompt": [{"role": "user", "content": "What color is the sky?"}],
... "completion": [{"role": "assistant", "content": "It is blue."}],
... }
>>> apply_chat_template(example, tokenizer)
{'prompt': '<|user|>\nWhat color is the sky?<|end|>\n<|assistant|>\n', 'completion': 'It is blue.<|end|>\n<|endoftext|>'}
maybe_convert_to_chatml
trl.maybe_convert_to_chatml
< 原始碼 >( example: dict ) → dict[str, list]
將包含欄位 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
從偏好資料示例中提取共享提示,其中提示隱含在“選擇的”和“拒絕的”兩個完成中。
更多詳情,請參見 maybe_extract_prompt()。
maybe_extract_prompt
trl.maybe_extract_prompt
< 原始碼 >( example: dict ) → dict[str, list]
從偏好資料示例中提取共享提示,其中提示隱含在“選擇的”和“拒絕的”兩個完成中。
如果示例已經包含 "prompt"
鍵,函式將按原樣返回該示例。否則,函式會識別“選擇的”和“拒絕的”兩個完成之間最長的公共對話輪次序列(字首),並將其提取為提示。然後,它會從各自的“選擇的”和“拒絕的”完成中移除這個提示。
示例
>>> 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.'}]}
或者,使用 datasets.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: ~DatasetType num_proc: typing.Optional[int] = None desc: typing.Optional[str] = None ) → Dataset
取消偏好資料集的配對。
示例
>>> 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}
maybe_unpair_preference_dataset
trl.maybe_unpair_preference_dataset
< 原始碼 >( dataset: ~DatasetType num_proc: typing.Optional[int] = None desc: typing.Optional[str] = None ) → Dataset
或 DatasetDict
如果偏好資料集已配對,則取消配對。
示例
>>> 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}
pack_dataset
trl.pack_dataset
< 原始碼 >( dataset: ~DatasetType seq_length: int strategy: str = 'bfd' map_kwargs: typing.Optional[dict[str, typing.Any]] = None ) → Dataset
或 DatasetDict
引數
- dataset (
Dataset
或DatasetDict
) — 要打包的資料集 - seq_length (
int
) — 目標打包序列長度。 - strategy (
str
,可選,預設為"bfd"
) — 使用的打包策略。可以是:"bfd"
(最佳擬合遞減法):較慢但保留序列邊界。序列永遠不會在中間被切斷。"wrapped"
:更快但更具侵略性。忽略序列邊界,會為了完全填充每個打包序列而切斷序列。
- map_kwargs (
dict
或None
,可選,預設為None
) — 在打包示例時傳遞給資料集的 map 方法的額外關鍵字引數。
返回
Dataset
或 DatasetDict
打包了序列的資料集。由於序列被合併,示例數量可能會減少。
將資料集中的序列打包成大小為 seq_length
的塊。
示例
>>> from datasets import Dataset
>>> from trl import pack_dataset
>>> examples = {
... "input_ids": [[1, 2, 3], [4, 5], [6, 7, 8], [9]],
... "attention_mask": [[1, 1, 0], [1, 0], [1, 0, 0], [1]],
... }
>>> dataset = Dataset.from_dict(examples)
>>> packed_dataset = pack_dataset(dataset, seq_length=4, strategy="bfd")
>>> packed_dataset[:]
{'input_ids': [[1, 2, 3, 9], [6, 7, 8, 4, 5]],
'attention_mask': [[1, 1, 0, 1], [1, 0, 0, 1, 0]]}
truncate_dataset
trl.truncate_dataset
< 原始碼 >( dataset: ~DatasetType max_length: int map_kwargs: typing.Optional[dict[str, typing.Any]] = None ) → Dataset
或 DatasetDict
將資料集中的序列截斷到指定的 max_length
。
示例
>>> from datasets import Dataset
>>> examples = {
... "input_ids": [[1, 2, 3], [4, 5, 6, 7], [8]],
... "attention_mask": [[0, 1, 1], [0, 0, 1, 1], [1]],
... }
>>> dataset = Dataset.from_dict(examples)
>>> truncated_dataset = truncate_dataset(dataset, max_length=2)
>>> truncated_dataset[:]
{'input_ids': [[1, 2], [4, 5], [8]],
'attention_mask': [[0, 1], [0, 0], [1]]}