Columns¶
This section contains the existing steps intended to be used for common column operations to apply to the batches.
expand
¶
ExpandColumns
¶
Bases: Step
Expand columns that contain lists into multiple rows.
ExpandColumns
is a Step
that takes a list of columns and expands them into multiple
rows. The new rows will have the same data as the original row, except for the expanded
column, which will contain a single item from the original list.
Attributes:
Name | Type | Description |
---|---|---|
columns |
Union[Dict[str, str], List[str]]
|
A dictionary that maps the column to be expanded to the new column name or a list of columns to be expanded. If a list is provided, the new column name will be the same as the column name. |
encoded |
Union[bool, List[str]]
|
A bool to inform Whether the columns are JSON encoded lists. If this value is set to True, the columns will be decoded before expanding. Alternatively, to specify columns that can be encoded, a list can be provided. In this case, the column names informed must be a subset of the columns selected for expansion. |
split_statistics |
bool
|
A bool to inform whether the statistics in the |
Input columns
- dynamic (determined by
columns
attribute): The columns to be expanded into multiple rows.
Output columns
- dynamic (determined by
columns
attribute): The expanded columns.
Categories
- columns
Examples:
Expand the selected columns into multiple rows:
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'}]
Expand the selected columns which are JSON encoded into multiple rows:
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'}]
Expand the selected columns and split the statistics in the distilabel_metadata
column:
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]}}}]
Source code in 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: StepColumns
property
¶
The columns to be expanded.
outputs: StepColumns
property
¶
The expanded columns.
always_dict(value)
classmethod
¶
Ensure that the columns are always a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Union[Dict[str, str], List[str]]
|
The columns to be expanded. |
required |
Returns:
Type | Description |
---|---|
Dict[str, str]
|
The columns to be expanded as a dictionary. |
Source code in src/distilabel/steps/columns/expand.py
is_subset()
¶
Ensure the "encoded" column names are a subset of the "columns" selected.
Returns:
Type | Description |
---|---|
Self
|
The "encoded" attribute updated to work internally. |
Source code in src/distilabel/steps/columns/expand.py
process(inputs)
¶
Expand the columns in the input data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
StepInput
|
The input data. |
required |
Yields:
Type | Description |
---|---|
StepOutput
|
The expanded rows. |
Source code in src/distilabel/steps/columns/expand.py
keep
¶
KeepColumns
¶
Bases: Step
Keeps selected columns in the dataset.
KeepColumns
is a Step
that implements the process
method that keeps only the columns
specified in the columns
attribute. Also KeepColumns
provides an attribute columns
to
specify the columns to keep which will override the default value for the properties inputs
and outputs
.
Note
The order in which the columns are provided is important, as the output will be sorted
using the provided order, which is useful before pushing either a dataset.Dataset
via
the PushToHub
step or a distilabel.Distiset
via the Pipeline.run
output variable.
Attributes:
Name | Type | Description |
---|---|---|
columns |
List[str]
|
List of strings with the names of the columns to keep. |
Input columns
- dynamic (determined by
columns
attribute): The columns to keep.
Output columns
- dynamic (determined by
columns
attribute): The columns that were kept.
Categories
- columns
Examples:
Select the columns to keep:
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'}]
Source code in src/distilabel/steps/columns/keep.py
inputs: StepColumns
property
¶
The inputs for the task are the column names in columns
.
outputs: StepColumns
property
¶
The outputs for the task are the column names in columns
.
process(*inputs)
¶
The process
method keeps only the columns specified in the columns
attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*inputs
|
StepInput
|
A list of dictionaries with the input data. |
()
|
Yields:
Type | Description |
---|---|
StepOutput
|
A list of dictionaries with the output data. |
Source code in src/distilabel/steps/columns/keep.py
merge
¶
MergeColumns
¶
Bases: Step
Merge columns from a row.
MergeColumns
is a Step
that implements the process
method that calls the merge_columns
function to handle and combine columns in a StepInput
. MergeColumns
provides two attributes
columns
and output_column
to specify the columns to merge and the resulting output column.
This step can be useful if you have a Task
that generates instructions for example, and you
want to have more examples of those. In such a case, you could for example use another Task
to multiply your instructions synthetically, what would yield two different columns splitted.
Using MergeColumns
you can merge them and use them as a single column in your dataset for
further processing.
Attributes:
Name | Type | Description |
---|---|---|
columns |
List[str]
|
List of strings with the names of the columns to merge. |
output_column |
Optional[str]
|
str name of the output column |
Input columns
- dynamic (determined by
columns
attribute): The columns to merge.
Output columns
- dynamic (determined by
columns
andoutput_column
attributes): The columns that were merged.
Categories
- columns
Examples:
Combine columns in rows of a dataset:
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?']}]
Source code in src/distilabel/steps/columns/merge.py
group
¶
GroupColumns
¶
Bases: Step
Combines columns from a list of StepInput
.
GroupColumns
is a Step
that implements the process
method that calls the group_dicts
function to handle and combine a list of StepInput
. Also GroupColumns
provides two attributes
columns
and output_columns
to specify the columns to group and the output columns
which will override the default value for the properties inputs
and outputs
, respectively.
Attributes:
Name | Type | Description |
---|---|---|
columns |
List[str]
|
List of strings with the names of the columns to group. |
output_columns |
Optional[List[str]]
|
Optional list of strings with the names of the output columns. |
Input columns
- dynamic (determined by
columns
attribute): The columns to group.
Output columns
- dynamic (determined by
columns
andoutput_columns
attributes): The columns that were grouped.
Categories
- columns
Examples:
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']}]
```
Source code in src/distilabel/steps/columns/group.py
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 127 |
|
inputs: List[str]
property
¶
The inputs for the task are the column names in columns
.
outputs: List[str]
property
¶
The outputs for the task are the column names in output_columns
or
grouped_{column}
for each column in columns
.
process(*inputs)
¶
The process
method calls the group_dicts
function to handle and combine a list of StepInput
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*inputs
|
StepInput
|
A list of |
()
|
Yields:
Type | Description |
---|---|
StepOutput
|
A |
Source code in src/distilabel/steps/columns/group.py
CombineColumns
¶
Bases: GroupColumns
CombineColumns
is deprecated and will be removed in version 1.5.0, use GroupColumns
instead.
Source code in src/distilabel/steps/columns/group.py
utils
¶
merge_distilabel_metadata(*output_dicts)
¶
Merge the DISTILABEL_METADATA_KEY
from multiple output dictionaries. DISTILABEL_METADATA_KEY
can be either a dictionary containing metadata keys or a list containing dictionaries
of metadata keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*output_dicts
|
Dict[str, Any]
|
Variable number of dictionaries or lists containing distilabel metadata. |
()
|
Returns:
Type | Description |
---|---|
Union[Dict[str, Any], List[Dict[str, Any]]]
|
A merged dictionary or list containing all the distilabel metadata. |
Source code in src/distilabel/steps/columns/utils.py
group_columns(*inputs, group_columns, output_group_columns=None)
¶
Groups multiple list of dictionaries into a single list of dictionaries on the
specified group_columns
. If group_columns
are provided, then it will also rename
group_columns
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
StepInput
|
list of dictionaries to combine. |
()
|
group_columns
|
List[str]
|
list of keys to merge on. |
required |
output_group_columns
|
Optional[List[str]]
|
list of keys to rename the merge keys to. Defaults to |
None
|
Returns:
Type | Description |
---|---|
StepInput
|
A list of dictionaries where the values of the |
StepInput
|
list and renamed to |
Source code in src/distilabel/steps/columns/utils.py
merge_columns(row, columns, new_column='combined_key')
¶
Merge columns in a dictionary into a single column on the specified new_column
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row
|
Dict[str, Any]
|
Dictionary corresponding to a row in a dataset. |
required |
columns
|
List[str]
|
List of keys to merge. |
required |
new_column
|
str
|
Name of the new key created. |
'combined_key'
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary with the new merged key. |