Skip to content

Fix some issues related to Magpie task #783

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

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 24 additions & 13 deletions src/distilabel/steps/tasks/magpie/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ def _append_messages_to_conversations(
conversation.append({"role": role, "content": instruction})
return conversations

def _generate_instruction(
self, inputs: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
prepared_inputs = self._prepare_inputs_for_instruction_generation(inputs)
outputs = self.llm.generate(
inputs=prepared_inputs,
num_generations=1,
**self.llm.generation_kwargs, # type: ignore
)
return [{"instruction": output[0]} for output in outputs]

def _generate_multi_turn_conversation(
self, inputs: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
Expand Down Expand Up @@ -156,17 +167,16 @@ def _generate_with_pre_query_template(
Returns:
The list of generated conversations.
"""
outputs = (
self._generate_instruction(inputs)
if self.only_instruction
else self._generate_multi_turn_conversation(inputs)
)

if self.only_instruction:
prepared_inputs = self._prepare_inputs_for_instruction_generation(inputs)
outputs = self.llm.generate(
inputs=prepared_inputs,
num_generations=1,
**self.llm.generation_kwargs, # type: ignore
)
return [{"instruction": output[0]} for output in outputs]

return self._generate_multi_turn_conversation(inputs)
return [
{**input, **output, "model_name": self.llm.model_name}
for input, output in zip(inputs, outputs)
]


class Magpie(Task, MagpieBase):
Expand Down Expand Up @@ -209,8 +219,9 @@ class Magpie(Task, MagpieBase):

Output columns:
- conversation (`ChatType`): the generated conversation which is a list of chat
items with a role and a message. Only if `only_instructions=False`.
items with a role and a message. Only if `only_instruction=False`.
- instruction (`str`): the generated instructions if `only_instruction=True`.
- model_name (`str`): The model name used to generate the `conversation` or `instruction`.

Categories:
- text-generation
Expand Down Expand Up @@ -352,8 +363,8 @@ def format_input(self, input: Dict[str, Any]) -> "ChatType":
def outputs(self) -> List[str]:
"""Either a multi-turn conversation or the instruction generated."""
if self.only_instruction:
return ["instruction"]
return ["conversation"]
return ["instruction", "model_name"]
return ["conversation", "model_name"]

def format_output(
self,
Expand Down
5 changes: 3 additions & 2 deletions src/distilabel/steps/tasks/magpie/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class MagpieGenerator(GeneratorTask, MagpieBase):
- conversation (`ChatType`): the generated conversation which is a list of chat
items with a role and a message.
- instruction (`str`): the generated instructions if `only_instruction=True`.
- model_name (`str`): The model name used to generate the `conversation` or `instruction`.

Categories:
- text-generation
Expand Down Expand Up @@ -215,8 +216,8 @@ def format_output(
def outputs(self) -> List[str]:
"""Either a multi-turn conversation or the instruction generated."""
if self.only_instruction:
return ["instruction"]
return ["conversation"]
return ["instruction", "model_name"]
return ["conversation", "model_name"]

def process(self, offset: int = 0) -> "GeneratorStepOutput":
"""Generates the desired number of instructions or conversations using Magpie.
Expand Down
31 changes: 26 additions & 5 deletions tests/unit/steps/tasks/magpie/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def test_raise_value_error_llm_no_magpie_mixin(self) -> None:
def test_outputs(self) -> None:
task = Magpie(llm=DummyMagpieLLM(magpie_pre_query_template="llama3"))

assert task.outputs == ["conversation"]
assert task.outputs == ["conversation", "model_name"]

task = Magpie(
llm=DummyMagpieLLM(magpie_pre_query_template="llama3"),
only_instruction=True,
)

assert task.outputs == ["instruction"]
assert task.outputs == ["instruction", "model_name"]

def test_process(self) -> None:
task = Magpie(llm=DummyMagpieLLM(magpie_pre_query_template="llama3"), n_turns=1)
Expand All @@ -50,18 +50,21 @@ def test_process(self) -> None:
{"role": "user", "content": "Hello Magpie"},
{"role": "assistant", "content": "Hello Magpie"},
],
"model_name": "test",
},
{
"conversation": [
{"role": "user", "content": "Hello Magpie"},
{"role": "assistant", "content": "Hello Magpie"},
],
"model_name": "test",
},
{
"conversation": [
{"role": "user", "content": "Hello Magpie"},
{"role": "assistant", "content": "Hello Magpie"},
],
"model_name": "test",
},
]

Expand All @@ -79,6 +82,7 @@ def test_process_with_n_turns(self) -> None:
{"role": "user", "content": "Hello Magpie"},
{"role": "assistant", "content": "Hello Magpie"},
],
"model_name": "test",
},
{
"conversation": [
Expand All @@ -88,6 +92,7 @@ def test_process_with_n_turns(self) -> None:
{"role": "user", "content": "Hello Magpie"},
{"role": "assistant", "content": "Hello Magpie"},
],
"model_name": "test",
},
{
"conversation": [
Expand All @@ -97,6 +102,7 @@ def test_process_with_n_turns(self) -> None:
{"role": "user", "content": "Hello Magpie"},
{"role": "assistant", "content": "Hello Magpie"},
],
"model_name": "test",
},
]

Expand All @@ -115,31 +121,37 @@ def test_process_with_system_prompt_per_row(self) -> None:
)
) == [
{
"system_prompt": "You're a math expert assistant.",
"conversation": [
{"role": "system", "content": "You're a math expert assistant."},
{"role": "user", "content": "Hello Magpie"},
{"role": "assistant", "content": "Hello Magpie"},
{"role": "user", "content": "Hello Magpie"},
{"role": "assistant", "content": "Hello Magpie"},
],
"model_name": "test",
},
{
"system_prompt": "You're a florist expert assistant.",
"conversation": [
{"role": "system", "content": "You're a florist expert assistant."},
{"role": "user", "content": "Hello Magpie"},
{"role": "assistant", "content": "Hello Magpie"},
{"role": "user", "content": "Hello Magpie"},
{"role": "assistant", "content": "Hello Magpie"},
],
"model_name": "test",
},
{
"system_prompt": "You're a plumber expert assistant.",
"conversation": [
{"role": "system", "content": "You're a plumber expert assistant."},
{"role": "user", "content": "Hello Magpie"},
{"role": "assistant", "content": "Hello Magpie"},
{"role": "user", "content": "Hello Magpie"},
{"role": "assistant", "content": "Hello Magpie"},
],
"model_name": "test",
},
]

Expand All @@ -152,9 +164,18 @@ def test_process_only_instruction(self) -> None:
task.load()

assert next(task.process(inputs=[{}, {}, {}])) == [
{"instruction": "Hello Magpie"},
{"instruction": "Hello Magpie"},
{"instruction": "Hello Magpie"},
{
"instruction": "Hello Magpie",
"model_name": "test",
},
{
"instruction": "Hello Magpie",
"model_name": "test",
},
{
"instruction": "Hello Magpie",
"model_name": "test",
},
]

def test_serialization(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/steps/tasks/magpie/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def test_raise_value_error_llm_no_magpie_mixin(self) -> None:
def test_outputs(self) -> None:
task = MagpieGenerator(llm=DummyMagpieLLM(magpie_pre_query_template="llama3"))

assert task.outputs == ["conversation"]
assert task.outputs == ["conversation", "model_name"]

task = MagpieGenerator(
llm=DummyMagpieLLM(magpie_pre_query_template="llama3"),
only_instruction=True,
)

assert task.outputs == ["instruction"]
assert task.outputs == ["instruction", "model_name"]

def test_serialization(self) -> None:
task = MagpieGenerator(llm=DummyMagpieLLM(magpie_pre_query_template="llama3"))
Expand Down
Loading