列¶
本节包含旨在用于应用于批次的常见列操作的现有步骤。
expand
¶
ExpandColumns
¶
基类: Step
将包含列表的列展开为多行。
ExpandColumns
是一个 Step
,它接受列列表并将它们展开为多行。新行将与原始行具有相同的数据,但展开的列除外,该列将包含原始列表中的单个项目。
属性
名称 | 类型 | 描述 |
---|---|---|
columns |
Union[Dict[str, str], List[str]]
|
一个字典,将要展开的列映射到新列名,或要展开的列的列表。如果提供列表,则新列名将与列名相同。 |
encoded |
Union[bool, List[str]]
|
一个布尔值,用于告知列是否为 JSON 编码的列表。如果此值设置为 True,则将在展开之前解码列。或者,要指定可以编码的列,可以提供列表。在这种情况下,告知的列名必须是要展开的列的子集。 |
split_statistics |
bool
|
一个布尔值,用于告知是否应将 |
输入列
- 动态(由
columns
属性确定):要展开为多行的列。
输出列
- 动态(由
columns
属性确定):展开的列。
类别
- columns
示例
将选定的列展开为多行
from distilabel.steps import ExpandColumns
expand_columns = ExpandColumns(
columns=["generation"],
)
expand_columns.load()
result = next(
expand_columns.process(
[
{
"instruction": "instruction 1",
"generation": ["generation 1", "generation 2"]}
],
)
)
# >>> result
# [{'instruction': 'instruction 1', 'generation': 'generation 1'}, {'instruction': 'instruction 1', 'generation': 'generation 2'}]
将 JSON 编码的选定列展开为多行
from distilabel.steps import ExpandColumns
expand_columns = ExpandColumns(
columns=["generation"],
encoded=True, # It can also be a list of columns that are encoded, i.e. ["generation"]
)
expand_columns.load()
result = next(
expand_columns.process(
[
{
"instruction": "instruction 1",
"generation": '["generation 1", "generation 2"]'}
],
)
)
# >>> result
# [{'instruction': 'instruction 1', 'generation': 'generation 1'}, {'instruction': 'instruction 1', 'generation': 'generation 2'}]
展开选定的列并拆分 distilabel_metadata
列中的统计信息
from distilabel.steps import ExpandColumns
expand_columns = ExpandColumns(
columns=["generation"],
split_statistics=True,
)
expand_columns.load()
result = next(
expand_columns.process(
[
{
"instruction": "instruction 1",
"generation": ["generation 1", "generation 2"],
"distilabel_metadata": {
"statistics_generation": {
"input_tokens": [12],
"output_tokens": [12],
},
},
}
],
)
)
# >>> result
# [{'instruction': 'instruction 1', 'generation': 'generation 1', 'distilabel_metadata': {'statistics_generation': {'input_tokens': [6], 'output_tokens': [6]}}}, {'instruction': 'instruction 1', 'generation': 'generation 2', 'distilabel_metadata': {'statistics_generation': {'input_tokens': [6], 'output_tokens': [6]}}}]
源代码位于 src/distilabel/steps/columns/expand.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
|
inputs
property
¶
要展开的列。
outputs
property
¶
展开的列。
always_dict(value)
classmethod
¶
确保列始终是字典。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
value
|
Union[Dict[str, str], List[str]]
|
要展开的列。 |
必需 |
返回
类型 | 描述 |
---|---|
Dict[str, str]
|
要展开的列作为字典。 |
源代码位于 src/distilabel/steps/columns/expand.py
is_subset()
¶
确保“encoded”列名是所选“columns”的子集。
返回
类型 | 描述 |
---|---|
Self
|
“encoded”属性已更新以在内部工作。 |
源代码位于 src/distilabel/steps/columns/expand.py
process(inputs)
¶
展开输入数据中的列。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
inputs
|
StepInput
|
输入数据。 |
必需 |
产生
类型 | 描述 |
---|---|
StepOutput
|
展开的行。 |
源代码位于 src/distilabel/steps/columns/expand.py
keep
¶
KeepColumns
¶
基类: Step
保留数据集中的选定列。
KeepColumns
是一个 Step
,它实现了 process
方法,该方法仅保留 columns
属性中指定的列。此外,KeepColumns
提供了一个属性 columns
来指定要保留的列,这将覆盖属性 inputs
和 outputs
的默认值。
注意
提供的列的顺序很重要,因为输出将使用提供的顺序进行排序,这在通过 PushToHub
步骤推送 dataset.Dataset
或通过 Pipeline.run
输出变量推送 distilabel.Distiset
之前很有用。
属性
名称 | 类型 | 描述 |
---|---|---|
columns |
List[str]
|
包含要保留的列名称的字符串列表。 |
输入列
- 动态(由
columns
属性确定):要保留的列。
输出列
- 动态(由
columns
属性确定):已保留的列。
类别
- columns
示例
选择要保留的列
from distilabel.steps import KeepColumns
keep_columns = KeepColumns(
columns=["instruction", "generation"],
)
keep_columns.load()
result = next(
keep_columns.process(
[{"instruction": "What's the brightest color?", "generation": "white", "model_name": "my_model"}],
)
)
# >>> result
# [{'instruction': "What's the brightest color?", 'generation': 'white'}]
源代码位于 src/distilabel/steps/columns/keep.py
inputs
property
¶
任务的输入是 columns
中的列名。
outputs
property
¶
任务的输出是 columns
中的列名。
process(*inputs)
¶
process
方法仅保留 columns
属性中指定的列。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
*inputs
|
StepInput
|
包含输入数据的字典列表。 |
()
|
产生
类型 | 描述 |
---|---|
StepOutput
|
包含输出数据的字典列表。 |
源代码位于 src/distilabel/steps/columns/keep.py
merge
¶
MergeColumns
¶
基类: Step
合并行中的列。
MergeColumns
是一个 Step
,它实现了 process
方法,该方法调用 merge_columns
函数来处理和组合 StepInput
中的列。MergeColumns
提供了两个属性 columns
和 output_column
来指定要合并的列和生成的输出列。
如果您有一个 Task
例如生成指令,并且您想要更多这些指令的示例,则此步骤可能很有用。在这种情况下,您可以例如使用另一个 Task
以合成方式倍增您的指令,这将产生两个不同的拆分列。使用 MergeColumns
,您可以合并它们并在数据集中使用它们作为单个列以进行进一步处理。
属性
名称 | 类型 | 描述 |
---|---|---|
columns |
List[str]
|
包含要合并的列名称的字符串列表。 |
output_column |
Optional[str]
|
输出列的字符串名称 |
输入列
- 动态(由
columns
属性确定):要合并的列。
输出列
- 动态(由
columns
和output_column
属性确定):已合并的列。
类别
- columns
示例
组合数据集行中的列
from distilabel.steps import MergeColumns
combiner = MergeColumns(
columns=["queries", "multiple_queries"],
output_column="queries",
)
combiner.load()
result = next(
combiner.process(
[
{
"queries": "How are you?",
"multiple_queries": ["What's up?", "Everything ok?"]
}
],
)
)
# >>> result
# [{'queries': ['How are you?', "What's up?", 'Everything ok?']}]
源代码位于 src/distilabel/steps/columns/merge.py
group
¶
GroupColumns
¶
基类: Step
从 StepInput
列表中组合列。
GroupColumns
是一个 Step
,它实现了 process
方法,该方法调用 group_dicts
函数来处理和组合 StepInput
列表。此外,GroupColumns
提供了两个属性 columns
和 output_columns
来指定要分组的列和输出列,这将分别覆盖属性 inputs
和 outputs
的默认值。
属性
名称 | 类型 | 描述 |
---|---|---|
columns |
List[str]
|
包含要分组的列名称的字符串列表。 |
output_columns |
Optional[List[str]]
|
包含输出列名称的可选字符串列表。 |
输入列
- 动态(由
columns
属性确定):要分组的列。
输出列
- 动态(由
columns
和output_columns
属性确定):已分组的列。
类别
- columns
示例
Group columns of a dataset:
```python
from distilabel.steps import GroupColumns
group_columns = GroupColumns(
name="group_columns",
columns=["generation", "model_name"],
)
group_columns.load()
result = next(
group_columns.process(
[{"generation": "AI generated text"}, {"model_name": "my_model"}],
[{"generation": "Other generated text", "model_name": "my_model"}]
)
)
# >>> result
# [{'merged_generation': ['AI generated text', 'Other generated text'], 'merged_model_name': ['my_model']}]
```
Specify the name of the output columns:
```python
from distilabel.steps import GroupColumns
group_columns = GroupColumns(
name="group_columns",
columns=["generation", "model_name"],
output_columns=["generations", "generation_models"]
)
group_columns.load()
result = next(
group_columns.process(
[{"generation": "AI generated text"}, {"model_name": "my_model"}],
[{"generation": "Other generated text", "model_name": "my_model"}]
)
)
# >>> result
#[{'generations': ['AI generated text', 'Other generated text'], 'generation_models': ['my_model']}]
```
源代码位于 src/distilabel/steps/columns/group.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
inputs
property
¶
任务的输入是 columns
中的列名。
outputs
property
¶
任务的输出是 output_columns
中的列名,或者 columns
中每个列的 grouped_{column}
。
process(*inputs)
¶
process
方法调用 group_dicts
函数来处理和组合 StepInput
列表。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
*inputs
|
StepInput
|
要组合的 |
()
|
产生
类型 | 描述 |
---|---|
StepOutput
|
一个 |
源代码位于 src/distilabel/steps/columns/group.py
utils
¶
merge_distilabel_metadata(*output_dicts)
¶
从多个输出字典中合并 DISTILABEL_METADATA_KEY
。DISTILABEL_METADATA_KEY
可以是包含元数据键的字典,也可以是包含元数据键字典的列表。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
*output_dicts
|
Dict[str, Any]
|
包含 distilabel 元数据的可变数量的字典或列表。 |
()
|
返回
类型 | 描述 |
---|---|
Union[Dict[str, Any], List[Dict[str, Any]]]
|
包含所有 distilabel 元数据的合并字典或列表。 |
源代码位于 src/distilabel/steps/columns/utils.py
group_columns(*inputs, group_columns, output_group_columns=None)
¶
在指定的 group_columns
上将多个字典列表分组为单个字典列表。如果提供了 group_columns
,则它还将重命名 group_columns
。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
inputs
|
StepInput
|
要组合的字典列表。 |
()
|
group_columns
|
List[str]
|
要合并的键列表。 |
必需 |
output_group_columns
|
Optional[List[str]]
|
用于重命名合并键的键列表。默认为 |
None
|
返回
类型 | 描述 |
---|---|
StepInput
|
字典列表,其中 |
StepInput
|
列表并重命名为 |
源代码位于 src/distilabel/steps/columns/utils.py
merge_columns(row, columns, new_column='combined_key')
¶
将字典中的列合并到指定 new_column
上的单个列中。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
row
|
Dict[str, Any]
|
与数据集中的行对应的字典。 |
必需 |
columns
|
List[str]
|
要合并的键列表。 |
必需 |
new_column
|
str
|
创建的新键的名称。 |
'combined_key'
|
返回
类型 | 描述 |
---|---|
Dict[str, Any]
|
包含新合并键的字典。 |