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

Add PairRM task for ranking responses #450

Merged
merged 11 commits into from
Mar 26, 2024
Merged

Add PairRM task for ranking responses #450

merged 11 commits into from
Mar 26, 2024

Conversation

plaguss
Copy link
Contributor

@plaguss plaguss commented Mar 20, 2024

Description

This PR adds PairRM to rank responses from different LLMs.

Example pipeline:

from typing import Any, Dict, Generator, List

from distilabel.pipeline.local import Pipeline
from distilabel.steps.base import RuntimeParameter, Step, StepInput
from distilabel.steps.generators.huggingface import LoadHubDataset
from distilabel.steps.combine import CombineColumns

from distilabel.steps.task.pair_rm import PairRM


class GenerateResponse(Step):
    @property
    def inputs(self) -> List[str]:
        return ["instruction"]

    @property
    def outputs(self) -> List[str]:
        return ["response"]

    def process(self, inputs: StepInput) -> Generator[List[Dict[str, Any]], None, None]:
        for input in inputs:
            input["response"] = "Response here."

        yield inputs


class RenameColumns(Step):
    rename_mappings: RuntimeParameter[Dict[str, str]]

    @property
    def inputs(self) -> List[str]:
        return []

    @property
    def outputs(self) -> List[str]:
        return list(self.rename_mappings.values())  # type: ignore

    def process(self, inputs: StepInput) -> Generator[List[Dict[str, Any]], None, None]:
        outputs = []
        for input in inputs:
            outputs.append(
                {self.rename_mappings.get(k, k): v for k, v in input.items()}  # type: ignore
            )
        yield outputs


if __name__ == "__main__":
    with Pipeline() as pipeline:
        load_hub_dataset = LoadHubDataset(name="load_dataset", batch_size=8)
        rename_columns = RenameColumns(
            name="rename_columns",
            input_batch_size=12,
        )
    
        text_generator_1 = GenerateResponse(
            name="generate_response_1", input_batch_size=16
        )
        text_generator_2 = GenerateResponse(
            name="generate_response_2", input_batch_size=16
        )
    
        load_hub_dataset.connect(rename_columns)
        rename_columns.connect(text_generator_1)
        rename_columns.connect(text_generator_2)

        # Will combine the outputs from the different LLMs (the candidates to rank)
        combine_columns = CombineColumns(
            name="combine_columns",
            merge_columns=["response"],
            output_merge_columns=["candidates"],
        )
        
        text_generator_1.connect(combine_columns)
        text_generator_2.connect(combine_columns)

        # Prepares the column names to the name expected from `PairRM`
        rename_for_pairrm = RenameColumns(
            name="rename_for_pairrm",
            input_batch_size=12,
        )
        combine_columns.connect(rename_for_pairrm)

        ranker = PairRM(name="pair_rm_ranker")
        rename_for_pairrm.connect(ranker)

        ds = pipeline.run(
            parameters={
                "load_dataset": {
                    "repo_id": "plaguss/test",
                    "split": "train",
                },
                "rename_columns": {
                    "rename_mappings": {"prompt": "instruction"}
                },
                "rename_for_pairrm": {
                    "rename_mappings": {"instruction": "input"}
                },
            }
        )
        print(ds)
        print(ds["pair_rm_ranker"][0])

Closes #433

@plaguss plaguss self-assigned this Mar 20, 2024
@plaguss plaguss added this to the 1.0.0 milestone Mar 20, 2024
@plaguss plaguss added the enhancement New feature or request label Mar 20, 2024
@plaguss plaguss linked an issue Mar 20, 2024 that may be closed by this pull request
@plaguss plaguss marked this pull request as ready for review March 25, 2024 08:35
@gabrielmbmb gabrielmbmb merged commit 509ae1b into core-refactor Mar 26, 2024
4 checks passed
@gabrielmbmb gabrielmbmb deleted the pairrm branch March 26, 2024 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add PairRM
2 participants