Columns¶
This section contains the existing steps intended to be used for common column operations to apply to the batches.
CombineColumns
¶
Bases: Step
Combines columns from a list of StepInput
.
CombineColumns
is a Step
that implements the process
method that calls the combine_dicts
function to handle and combine a list of StepInput
. Also CombineColumns
provides two attributes
columns
and output_columns
to specify the columns to merge 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 merge. |
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 merge.
Output columns
- dynamic (determined by
columns
andoutput_columns
attributes): The columns that were merged.
Examples:
Combine columns of a dataset:
```python
from distilabel.steps import CombineColumns
combine_columns = CombineColumns(
name="combine_columns",
columns=["generation", "model_name"],
)
combine_columns.load()
result = next(
combine_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 CombineColumns
combine_columns = CombineColumns(
name="combine_columns",
columns=["generation", "model_name"],
output_columns=["generations", "generation_models"]
)
combine_columns.load()
result = next(
combine_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/combine.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 |
|
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
merged_{column}
for each column in columns
.
process(*inputs)
¶
The process
method calls the combine_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/combine.py
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. |
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.
Examples:
Expand the selected columns into multiple rows:
```python
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'}]
```
Source code in src/distilabel/steps/expand.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 127 128 129 |
|
inputs: List[str]
property
¶
The columns to be expanded.
outputs: List[str]
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/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/expand.py
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.
Examples:
Select the columns to keep:
```python
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/keep.py
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 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. |