-
Notifications
You must be signed in to change notification settings - Fork 148
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6ee1c92
feat: implenent basic SFT pipeline based on synthetic data generator
burtenshaw b18a98f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a0dfa76
feat: expose more params in init for config
burtenshaw 2cecb13
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e463b11
feat: rename and document
burtenshaw 7fe42f9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4244dd7
docs: add to quickstart in docs
burtenshaw e631e07
feat: expand logic for turns and llms
burtenshaw a30bd19
feat: drop llm validation
burtenshaw 03b6dd5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 048ebf0
docs: improve documentation
burtenshaw 26c7e53
open kwargs in run
burtenshaw 79a13d5
fix unused variable
burtenshaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# 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.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: | ||
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, **kwargs) -> Distiset: | ||
"""Runs the pipeline and returns a Distiset.""" | ||
return self.pipeline.run(**kwargs) | ||
|
||
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: | ||
MagpieGenerator( | ||
llm=self.llm, | ||
n_turns=n_turns, | ||
num_rows=num_rows, | ||
batch_size=batch_size, | ||
system_prompt=system_prompt, | ||
) | ||
|
||
return pipeline | ||
|
||
def _get_output_columns(self, n_turns: int) -> list: | ||
"""Returns the output mappings for the pipeline.""" | ||
if n_turns == 1: | ||
return ["instruction", "response", "model_name"] | ||
else: | ||
return ["instruction", "conversation", "model_name"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.