Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implenent basic SFT pipeline based on synthetic data generator #1059

Merged
merged 13 commits into from
Nov 19, 2024
Merged
18 changes: 17 additions & 1 deletion docs/sections/getting_started/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,23 @@ To install the latest release with `hf-inference-endpoints` extra of the package
pip install distilabel[hf-inference-endpoints] --upgrade
```

## Define a pipeline
## Use a generic pipeline

To use a generic pipeline for an ML task, you can use the `InstructionResponsePipeline` class. This class is a generic pipeline that can be used to generate data for supervised fine-tuning tasks. It uses the `InferenceEndpointsLLM` class to generate data based on the input data and the model.

```python
from distilabel.pipeline import InstructionResponsePipeline

pipeline = InstructionResponsePipeline()
dataset = pipeline.run()
```

The `InstructionResponsePipeline` class will use the `InferenceEndpointsLLM` class with the model `meta-llama/Meta-Llama-3.1-8B-Instruct` to generate data based on the input data. The input data can be provided via the `pipeline.run` method or via the `pipeline.run_from_file` method. The output data will be a dataset with the columns `instruction` and `response`. The class uses a generic system prompt, but you can customize it by passing the `system_prompt` parameter to the class.
burtenshaw marked this conversation as resolved.
Show resolved Hide resolved

!!! note
We're actively working on building more pipelines for different tasks. If you have any suggestions or requests, please let us know! We're currently working on pipelines for classification, Direct Preference Optimizatio, and Information Retrieval tasks.
burtenshaw marked this conversation as resolved.
Show resolved Hide resolved

## Define a Custom pipeline

In this guide we will walk you through the process of creating a simple pipeline that uses the [`InferenceEndpointsLLM`][distilabel.llms.InferenceEndpointsLLM] class to generate text. The [`Pipeline`][distilabel.pipeline.Pipeline] will load a dataset that contains a column named `prompt` from the Hugging Face Hub via the step [`LoadDataFromHub`][distilabel.steps.LoadDataFromHub] and then use the [`InferenceEndpointsLLM`][distilabel.llms.InferenceEndpointsLLM] class to generate text based on the dataset using the [`TextGeneration`](https://distilabel.argilla.io/dev/components-gallery/tasks/textgeneration/) task.

Expand Down
11 changes: 10 additions & 1 deletion src/distilabel/pipeline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,14 @@
routing_batch_function,
sample_n_steps,
)
from distilabel.pipeline.templates import (
InstructionResponsePipeline,
)

__all__ = ["Pipeline", "RayPipeline", "routing_batch_function", "sample_n_steps"]
__all__ = [
"Pipeline",
"RayPipeline",
"InstructionResponsePipeline",
"routing_batch_function",
"sample_n_steps",
]
15 changes: 15 additions & 0 deletions src/distilabel/pipeline/templates/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023-present, Argilla, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .instruction import InstructionResponsePipeline # noqa: F401

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will you also add this to CLI commands?
something like the following would be cool
distilabel pipelines sft --num-rows 100 --n-turns 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's nice, but we should do it another PR.

136 changes: 136 additions & 0 deletions src/distilabel/pipeline/templates/instruction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Copyright 2023-present, Argilla, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional

from distilabel.distiset import Distiset
from distilabel.llms.base import LLM
from distilabel.llms.huggingface import InferenceEndpointsLLM
from distilabel.pipeline import Pipeline
from distilabel.steps import KeepColumns
from distilabel.steps.tasks import MagpieGenerator

MODEL = "meta-llama/Meta-Llama-3.1-8B-Instruct"


class InstructionResponsePipeline:
"""Generates instructions and responses for a given system prompt.

This example pipeline can be used for a Supervised Fine-Tuning dataset which you
could use to train or evaluate a model. The pipeline generates instructions using the
MagpieGenerator and responses for a given system prompt. The pipeline then keeps only
the instruction, response, and model_name columns.

References:
- [Magpie: Alignment Data Synthesis from Scratch by Prompting Aligned LLMs with Nothing](https://arxiv.org/abs/2406.08464)

Example:

Generate instructions and responses for a given system prompt:

```python
from distilabel.pipeline import InstructionResponsePipeline

pipeline = InstructionResponsePipeline()

distiset = pipeline.run()
burtenshaw marked this conversation as resolved.
Show resolved Hide resolved
```

Customizing the pipeline further:

```python
from distilabel.pipeline import InstructionResponsePipeline

pipeline = InstructionResponsePipeline(
system_prompt="You are a creative AI Assistant for writing science fiction.",
llm=InferenceEndpointsLLM(
model_id="meta-llama/Meta-Llama-3.2-3B-Instruct",
tokenizer_id="meta-llama/Meta-Llama-3.2-3B-Instruct",
generation_kwargs={"max_new_tokens": 512, "temperature": 0.7},
),
num_rows=500,
batch_size=2,
n_turns=2,
)
```
"""

def __init__(
self,
llm: Optional[LLM] = None,
system_prompt: str = "You are a creative AI Assistant writer.",
hf_token: Optional[str] = None,
n_turns: int = 1,
num_rows: int = 10,
batch_size: int = 1,
) -> None:
if llm is None:
self.llm: LLM = InferenceEndpointsLLM(
model_id=MODEL,
tokenizer_id=MODEL,
magpie_pre_query_template="llama3",
generation_kwargs={
"temperature": 0.9,
"do_sample": True,
"max_new_tokens": 2048,
"stop_sequences": [
"<|eot_id|>",
"<|start_header_id|>",
"assistant",
" \n\n",
],
},
api_key=hf_token,
)
else:
if isinstance(
llm, InferenceEndpointsLLM
) and llm.magpie_pre_query_template not in [
"llama3",
burtenshaw marked this conversation as resolved.
Show resolved Hide resolved
"qwen2",
]:
raise ValueError(
"The provided pre-query template is not supported by Magpie. Supported templates are 'llama3' and 'qwen2'."
)
self.llm = llm

self.pipeline: Pipeline = self._get_magpie_pipeline(
system_prompt=system_prompt,
n_turns=n_turns,
num_rows=num_rows,
batch_size=batch_size,
)

def run(self) -> Distiset:
"""Runs the pipeline and returns a Distiset."""
return self.pipeline.run()
burtenshaw marked this conversation as resolved.
Show resolved Hide resolved

def _get_magpie_pipeline(
self, system_prompt: str, n_turns: int, num_rows: int, batch_size: int
) -> Pipeline:
"""Returns a pipeline that generates instructions and responses for a given system prompt."""
with Pipeline(name="sft") as pipeline:
magpie = MagpieGenerator(
llm=self.llm,
n_turns=n_turns,
num_rows=num_rows,
batch_size=batch_size,
system_prompt=system_prompt,
)
if n_turns == 1:
burtenshaw marked this conversation as resolved.
Show resolved Hide resolved
keep_columns = KeepColumns(
columns=["instruction", "response", "model_name"],
)
magpie.connect(keep_columns)
return pipeline
Loading