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

Implement "Improving Text Embeddings with LLMs" #683

Merged
merged 19 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/distilabel/steps/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
from distilabel.steps.tasks.evol_quality.base import EvolQuality
from distilabel.steps.tasks.generate_embeddings import GenerateEmbeddings
from distilabel.steps.tasks.genstruct import Genstruct
from distilabel.steps.tasks.improving_text_embeddings import (
BrainstormEmbeddingTasks,
GenerateTextClassificationData,
GenerateTextMatchingData,
GenerateTextRetrievalData,
)
from distilabel.steps.tasks.instruction_backtranslation import (
InstructionBacktranslation,
)
Expand All @@ -38,6 +44,7 @@
__all__ = [
"Task",
"GeneratorTask",
"BrainstormEmbeddingTasks",
"ChatGeneration",
"ChatItem",
"ChatType",
Expand All @@ -48,6 +55,9 @@
"EvolInstructGenerator",
"EvolQuality",
"GenerateEmbeddings",
"GenerateTextClassificationData",
"GenerateTextMatchingData",
"GenerateTextRetrievalData",
"Genstruct",
"InstructionBacktranslation",
"PairRM",
Expand Down
19 changes: 13 additions & 6 deletions src/distilabel/steps/tasks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def load(self) -> None:

@abstractmethod
def format_output(
self, output: Union[str, None], input: Dict[str, Any]
self,
output: Union[str, None],
input: Union[Dict[str, Any], None] = None,
) -> Dict[str, Any]:
"""Abstract method to format the outputs of the task. It needs to receive an output
as a string, and generates a Python dictionary with the outputs of the task. In
Expand All @@ -74,7 +76,9 @@ def format_output(
pass

def _format_outputs(
self, outputs: "GenerateOutput", inputs: List[Dict[str, Any]]
self,
outputs: "GenerateOutput",
inputs: Union[List[Dict[str, Any]], None] = None,
) -> List[Dict[str, Any]]:
"""Formats the outputs of the task using the `format_output` method. If the output
is `None` (i.e. the LLM failed to generate a response), then the outputs will be
Expand All @@ -87,8 +91,11 @@ def _format_outputs(
Returns:
A list containing a dictionary with the outputs of the task for each input.
"""
if inputs is None:
inputs = [None] # type: ignore

formatted_outputs = []
for output, input in zip(outputs, inputs * len(outputs)):
for output, input in zip(outputs, inputs * len(outputs)): # type: ignore
try:
formatted_output = self.format_output(output, input)
formatted_output = self._maybe_add_raw_output(
Expand All @@ -103,7 +110,7 @@ def _format_outputs(
return formatted_outputs

def _output_on_failure(
self, output: Union[str, None], input: Dict[str, Any]
self, output: Union[str, None], input: Union[Dict[str, Any], None] = None
) -> Dict[str, Any]:
"""In case of failure to format the output, this method will return a dictionary including
a new field `distilabel_meta` with the raw output of the LLM.
Expand Down Expand Up @@ -183,14 +190,14 @@ def process(self, inputs: StepInput) -> "StepOutput": # type: ignore
if self.group_generations:
combined = combine_dicts(*formatted_outputs)
task_outputs.append(
{**input, "model_name": self.llm.model_name, **combined}
{**input, **combined, "model_name": self.llm.model_name}
)
continue

# Create a row per generation
for formatted_output in formatted_outputs:
task_outputs.append(
{**input, "model_name": self.llm.model_name, **formatted_output}
{**input, **formatted_output, "model_name": self.llm.model_name}
)

yield task_outputs
Expand Down
Loading
Loading