Skip to content

Commit a213728

Browse files
committed
Add section in docs for the print method
1 parent 6a0714a commit a213728

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
138 KB
Loading

docs/sections/how_to_guides/basic/task/index.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,75 @@ As shown above, the [`TextGeneration`][distilabel.steps.tasks.TextGeneration] ta
5757
)
5858
```
5959

60+
### Task.print
61+
62+
!!! Info
63+
New since version `1.4.0`, [`Task.print`][distilabel.steps.tasks.base._Task.print] `Task.print` method.
64+
65+
The `Tasks` include a handy method to show what the prompt formatted for an `LLM` would look like, let's see an example with [`UltraFeedback`][distilabel.steps.tasks.ultrafeedback.UltraFeedback], but it applies to any other `Task`.
66+
67+
```python
68+
from distilabel.steps.tasks import UltraFeedback
69+
from distilabel.llms.huggingface import InferenceEndpointsLLM
70+
71+
uf = UltraFeedback(
72+
llm=InferenceEndpointsLLM(
73+
model_id="meta-llama/Meta-Llama-3.1-70B-Instruct",
74+
),
75+
)
76+
uf.load()
77+
uf.print()
78+
```
79+
80+
The result will be a rendered prompt, with the System prompt (if contained for the task) and the User prompt, rendered with rich (it will show exactly the same in a jupyter notebook).
81+
82+
![task-print](../../../../assets/images/sections/how_to_guides/tasks/task_print.png)
83+
84+
In case you want to test with a custom input, you can pass an example to the tasks` `format_input` method (or generate it on your own depending on the task), and pass it to the print method so that it shows your example:
85+
86+
87+
```python
88+
uf.print(
89+
uf.format_input({"instruction": "test", "generations": ["1", "2"]})
90+
)
91+
```
92+
93+
??? "Using a DummyLLM to avoid loading one"
94+
95+
In case you don't want to load an LLM to render the template, you can create a dummy one like the ones we could use for testing.
96+
97+
```python
98+
from distilabel.llms.base import LLM
99+
from distilabel.llms.mixins.magpie import MagpieChatTemplateMixin
100+
101+
class DummyLLM(AsyncLLM, MagpieChatTemplateMixin):
102+
structured_output: Any = None
103+
magpie_pre_query_template: str = "llama3"
104+
105+
def load(self) -> None:
106+
pass
107+
108+
@property
109+
def model_name(self) -> str:
110+
return "test"
111+
112+
def generate(
113+
self, input: "FormattedInput", num_generations: int = 1
114+
) -> "GenerateOutput":
115+
return ["output" for _ in range(num_generations)]
116+
```
117+
118+
You can use this `LLM` just as any of the other ones to `load` your task and call `print`:
119+
120+
```python
121+
uf = UltraFeedback(llm=DummyLLM())
122+
uf.load()
123+
uf.print()
124+
```
125+
126+
!!! Note
127+
When creating a custom task, the `print` method will be available by default, but it is limited to the most common scenarios for the inputs. If you test your new task and find it's not working as expected (for example, if your task contains one input consisting of a list of texts instead of a single one), you should override the `_sample_input` method. You can inspect the [`UltraFeedback`][distilabel.steps.tasks.ultrafeedback.UltraFeedback] source code for this.
128+
60129
## Specifying the number of generations and grouping generations
61130

62131
All the `Task`s have a `num_generations` attribute that allows defining the number of generations that we want to have per input. We can update the example above to generate 3 completions per input:

0 commit comments

Comments
 (0)