LLM¶
This section contains the API reference for the distilabel
LLMs, both for the LLM
synchronous implementation, and for the AsyncLLM
asynchronous one.
For more information and examples on how to use existing LLMs or create custom ones, please refer to Tutorial - LLM.
base
¶
LLM
¶
Bases: RuntimeParametersMixin
, BaseModel
, _Serializable
, ABC
Base class for LLM
s to be used in distilabel
framework.
To implement an LLM
subclass, you need to subclass this class and implement:
- load
method to load the LLM
if needed. Don't forget to call super().load()
,
so the _logger
attribute is initialized.
- model_name
property to return the model name used for the LLM.
- generate
method to generate num_generations
per input in inputs
.
Attributes:
Name | Type | Description |
---|---|---|
generation_kwargs |
Optional[RuntimeParameter[Dict[str, Any]]]
|
the kwargs to be propagated to either |
use_offline_batch_generation |
Optional[RuntimeParameter[bool]]
|
whether to use the |
offline_batch_generation_block_until_done |
Optional[RuntimeParameter[int]]
|
if provided, then polling will be done until
the |
jobs_ids |
Union[Tuple[str, ...], None]
|
the job ids generated by the |
_logger |
Logger
|
the logger to be used for the |
Source code in src/distilabel/models/llms/base.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
|
model_name: str
abstractmethod
property
¶
Returns the model name used for the LLM.
generate_parameters: List[inspect.Parameter]
property
¶
Returns the parameters of the generate
method.
Returns:
Type | Description |
---|---|
List[Parameter]
|
A list containing the parameters of the |
runtime_parameters_names: RuntimeParametersNames
property
¶
Returns the runtime parameters of the LLM
, which are combination of the
attributes of the LLM
type hinted with RuntimeParameter
and the parameters
of the generate
method that are not input
and num_generations
.
Returns:
Type | Description |
---|---|
RuntimeParametersNames
|
A dictionary with the name of the runtime parameters as keys and a boolean |
RuntimeParametersNames
|
indicating if the parameter is optional or not. |
generate_parsed_docstring: Docstring
cached
property
¶
Returns the parsed docstring of the generate
method.
Returns:
Type | Description |
---|---|
Docstring
|
The parsed docstring of the |
load()
¶
Method to be called to initialize the LLM
, its logger and optionally the
structured output generator.
unload()
¶
get_generation_kwargs()
¶
Returns the generation kwargs to be used for the generation. This method can be overridden to provide a more complex logic for the generation kwargs.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
The kwargs to be used for the generation. |
Source code in src/distilabel/models/llms/base.py
generate(inputs, num_generations=1, **kwargs)
abstractmethod
¶
Abstract method to be implemented by each LLM to generate num_generations
per input in inputs
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
List[FormattedInput]
|
the list of inputs to generate responses for which follows OpenAI's API format: |
required |
num_generations
|
int
|
the number of generations to generate per input. |
1
|
**kwargs
|
Any
|
the additional kwargs to be used for the generation. |
{}
|
Source code in src/distilabel/models/llms/base.py
generate_outputs(inputs, num_generations=1, **kwargs)
¶
Generates outputs for the given inputs using either generate
method or the
offine_batch_generate
method if `use_offline_
Source code in src/distilabel/models/llms/base.py
get_runtime_parameters_info()
¶
Gets the information of the runtime parameters of the LLM
such as the name
and the description. This function is meant to include the information of the runtime
parameters in the serialized data of the LLM
.
Returns:
Type | Description |
---|---|
List[RuntimeParameterInfo]
|
A list containing the information for each runtime parameter of the |
Source code in src/distilabel/models/llms/base.py
get_last_hidden_states(inputs)
¶
Method to get the last hidden states of the model for a list of inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
List[StandardInput]
|
the list of inputs to get the last hidden states from. |
required |
Returns:
Type | Description |
---|---|
List[HiddenState]
|
A list containing the last hidden state for each sequence using a NumPy array with shape [num_tokens, hidden_size]. |
Source code in src/distilabel/models/llms/base.py
offline_batch_generate(inputs=None, num_generations=1, **kwargs)
¶
Method to generate a list of outputs for the given inputs using an offline batch
generation method to be implemented by each LLM
.
This method should create jobs the first time is called and store the job ids, so
the second and subsequent calls can retrieve the results of the batch generation.
If subsequent calls are made before the batch generation is finished, then the method
should raise a DistilabelOfflineBatchGenerationNotFinishedException
. This exception
will be handled automatically by the Pipeline
which will store all the required
information for recovering the pipeline execution when the batch generation is finished.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
Union[List[FormattedInput], None]
|
the list of inputs to generate responses for. |
None
|
num_generations
|
int
|
the number of generations to generate per input. |
1
|
**kwargs
|
Any
|
the additional kwargs to be used for the generation. |
{}
|
Returns:
Type | Description |
---|---|
List[GenerateOutput]
|
A list containing the generations for each input. |
Source code in src/distilabel/models/llms/base.py
AsyncLLM
¶
Bases: LLM
Abstract class for asynchronous LLMs, so as to benefit from the async capabilities
of each LLM implementation. This class is meant to be subclassed by each LLM, and the
method agenerate
needs to be implemented to provide the asynchronous generation of
responses.
Attributes:
Name | Type | Description |
---|---|---|
_event_loop |
AbstractEventLoop
|
the event loop to be used for the asynchronous generation of responses. |
Source code in src/distilabel/models/llms/base.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
|
generate_parameters: List[inspect.Parameter]
property
¶
Returns the parameters of the agenerate
method.
Returns:
Type | Description |
---|---|
List[Parameter]
|
A list containing the parameters of the |
generate_parsed_docstring: Docstring
cached
property
¶
Returns the parsed docstring of the agenerate
method.
Returns:
Type | Description |
---|---|
Docstring
|
The parsed docstring of the |
agenerate(input, num_generations=1, **kwargs)
abstractmethod
async
¶
Method to generate a num_generations
responses for a given input asynchronously,
and executed concurrently in generate
method.
Source code in src/distilabel/models/llms/base.py
generate(inputs, num_generations=1, **kwargs)
¶
Method to generate a list of responses asynchronously, returning the output
synchronously awaiting for the response of each input sent to agenerate
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
List[FormattedInput]
|
the list of inputs to generate responses for. |
required |
num_generations
|
int
|
the number of generations to generate per input. |
1
|
**kwargs
|
Any
|
the additional kwargs to be used for the generation. |
{}
|
Returns:
Type | Description |
---|---|
List[GenerateOutput]
|
A list containing the generations for each input. |
Source code in src/distilabel/models/llms/base.py
__del__()
¶
Closes the event loop when the object is deleted.
Source code in src/distilabel/models/llms/base.py
merge_responses(responses, n=1)
¶
Helper function to group the responses from LLM.agenerate
method according
to the number of generations requested.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
responses
|
List[Dict[str, Any]]
|
the responses from the |
required |
n
|
int
|
number of responses to group together. Defaults to 1. |
1
|
Returns:
Type | Description |
---|---|
List[Dict[str, Any]]
|
List of merged responses, where each merged response contains n generations |
List[Dict[str, Any]]
|
and their corresponding statistics. |