Hugging Face¶
This section contains the reference for Hugging Face integrations:
InferenceEndpointsLLM
¶
Bases: AsyncLLM
, MagpieChatTemplateMixin
InferenceEndpoints LLM implementation running the async API client.
This LLM will internally use huggingface_hub.AsyncInferenceClient
.
Attributes:
Name | Type | Description |
---|---|---|
model_id |
Optional[str]
|
the model ID to use for the LLM as available in the Hugging Face Hub, which
will be used to resolve the base URL for the serverless Inference Endpoints API requests.
Defaults to |
endpoint_name |
Optional[RuntimeParameter[str]]
|
the name of the Inference Endpoint to use for the LLM. Defaults to |
endpoint_namespace |
Optional[RuntimeParameter[str]]
|
the namespace of the Inference Endpoint to use for the LLM. Defaults to |
base_url |
Optional[RuntimeParameter[str]]
|
the base URL to use for the Inference Endpoints API requests. |
api_key |
Optional[RuntimeParameter[SecretStr]]
|
the API key to authenticate the requests to the Inference Endpoints API. |
tokenizer_id |
Optional[str]
|
the tokenizer ID to use for the LLM as available in the Hugging Face Hub.
Defaults to |
model_display_name |
Optional[str]
|
the model display name to use for the LLM. Defaults to |
use_magpie_template |
Optional[str]
|
a flag used to enable/disable applying the Magpie pre-query
template. Defaults to |
magpie_pre_query_template |
Optional[str]
|
the pre-query template to be applied to the prompt or
sent to the LLM to generate an instruction or a follow up user message. Valid
values are "llama3", "qwen2" or another pre-query template provided. Defaults
to |
structured_output |
Optional[RuntimeParameter[StructuredOutputType]]
|
a dictionary containing the structured output configuration or
if more fine-grained control is needed, an instance of |
Icon
:hugging:
Examples:
Free serverless Inference API:
```python
from distilabel.llms.huggingface import InferenceEndpointsLLM
llm = InferenceEndpointsLLM(
model_id="mistralai/Mistral-7B-Instruct-v0.2",
)
llm.load()
output = llm.generate(inputs=[[{"role": "user", "content": "Hello world!"}]])
```
Dedicated Inference Endpoints:
```python
from distilabel.llms.huggingface import InferenceEndpointsLLM
llm = InferenceEndpointsLLM(
endpoint_name="<ENDPOINT_NAME>",
api_key="<HF_API_KEY>",
endpoint_namespace="<USER|ORG>",
)
llm.load()
output = llm.generate(inputs=[[{"role": "user", "content": "Hello world!"}]])
```
Dedicated Inference Endpoints or TGI:
```python
from distilabel.llms.huggingface import InferenceEndpointsLLM
llm = InferenceEndpointsLLM(
api_key="<HF_API_KEY>",
base_url="<BASE_URL>",
)
llm.load()
output = llm.generate(inputs=[[{"role": "user", "content": "Hello world!"}]])
```
Generate structured data:
```python
from pydantic import BaseModel
from distilabel.llms import InferenceEndpointsLLM
class User(BaseModel):
name: str
last_name: str
id: int
llm = InferenceEndpointsLLM(
model_id="meta-llama/Meta-Llama-3-70B-Instruct",
tokenizer_id="meta-llama/Meta-Llama-3-70B-Instruct",
api_key="api.key",
structured_output={"format": "json", "schema": User.model_json_schema()}
)
llm.load()
output = llm.generate(inputs=[[{"role": "user", "content": "Create a user profile for the Tour De France"}]])
```
Source code in src/distilabel/llms/huggingface/inference_endpoints.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 383 384 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 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
|
model_name: Union[str, None]
property
¶
Returns the model name used for the LLM.
agenerate(input, max_new_tokens=128, frequency_penalty=None, logit_bias=None, presence_penalty=None, seed=None, stop_sequences=None, temperature=1.0, tool_choice=None, tool_prompt=None, tools=None, top_p=None, do_sample=False, repetition_penalty=None, return_full_text=False, top_k=None, typical_p=None, watermark=False)
async
¶
Generates completions for the given input using the async client. This method
uses two methods of the huggingface_hub.AsyncClient
: chat_completion
and text_generation
.
chat_completion
method will be used only if no tokenizer_id
has been specified.
Some arguments of this function are specific to the text_generation
method, while
some others are specific to the chat_completion
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
FormattedInput
|
a single input in chat format to generate responses for. |
required |
max_new_tokens |
int
|
the maximum number of new tokens that the model will generate.
Defaults to |
128
|
frequency_penalty |
Optional[Annotated[float, Field(ge=-2.0, le=2.0)]]
|
a value between |
None
|
logit_bias |
Optional[List[float]]
|
modify the likelihood of specified tokens appearing in the completion.
This argument is exclusive to the |
None
|
presence_penalty |
Optional[Annotated[float, Field(ge=-2.0, le=2.0)]]
|
a value between |
None
|
seed |
Optional[int]
|
the seed to use for the generation. Defaults to |
None
|
stop_sequences |
Optional[List[str]]
|
either a single string or a list of strings containing the sequences
to stop the generation at. Defaults to |
None
|
temperature |
float
|
the temperature to use for the generation. Defaults to |
1.0
|
tool_choice |
Optional[Union[Dict[str, str], Literal['auto']]]
|
the name of the tool the model should call. It can be a dictionary
like |
None
|
tool_prompt |
Optional[str]
|
A prompt to be appended before the tools. This argument is exclusive
to the |
None
|
tools |
Optional[List[Dict[str, Any]]]
|
a list of tools definitions that the LLM can use.
This argument is exclusive to the |
None
|
top_p |
Optional[float]
|
the top-p value to use for the generation. Defaults to |
None
|
do_sample |
bool
|
whether to use sampling for the generation. This argument is exclusive
of the |
False
|
repetition_penalty |
Optional[float]
|
the repetition penalty to use for the generation. This argument
is exclusive of the |
None
|
return_full_text |
bool
|
whether to return the full text of the completion or just
the generated text. Defaults to |
False
|
top_k |
Optional[int]
|
the top-k value to use for the generation. This argument is exclusive
of the |
None
|
typical_p |
Optional[float]
|
the typical-p value to use for the generation. This argument is exclusive
of the |
None
|
watermark |
bool
|
whether to add the watermark to the generated text. This argument
is exclusive of the |
False
|
Returns:
Type | Description |
---|---|
GenerateOutput
|
A list of lists of strings containing the generated responses for each input. |
Source code in src/distilabel/llms/huggingface/inference_endpoints.py
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 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
|
load()
¶
Loads the AsyncInferenceClient
client to connect to the Hugging Face Inference
Endpoint.
Raises:
Type | Description |
---|---|
ImportError
|
if the |
ValueError
|
if the model is not currently deployed or is not running the TGI framework. |
ImportError
|
if the |
Source code in src/distilabel/llms/huggingface/inference_endpoints.py
only_one_of_model_id_endpoint_name_or_base_url_provided()
¶
Validates that only one of model_id
or endpoint_name
is provided; and if base_url
is also
provided, a warning will be shown informing the user that the provided base_url
will be ignored in
favour of the dynamically calculated one..
Source code in src/distilabel/llms/huggingface/inference_endpoints.py
prepare_input(input)
¶
Prepares the input (applying the chat template and tokenization) for the provided input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
StandardInput
|
the input list containing chat items. |
required |
Returns:
Type | Description |
---|---|
str
|
The prompt to send to the LLM. |
Source code in src/distilabel/llms/huggingface/inference_endpoints.py
TransformersLLM
¶
Bases: LLM
, MagpieChatTemplateMixin
, CudaDevicePlacementMixin
Hugging Face transformers
library LLM implementation using the text generation
pipeline.
Attributes:
Name | Type | Description |
---|---|---|
model |
str
|
the model Hugging Face Hub repo id or a path to a directory containing the model weights and configuration files. |
revision |
str
|
if |
torch_dtype |
str
|
the torch dtype to use for the model e.g. "float16", "float32", etc.
Defaults to |
trust_remote_code |
bool
|
whether to allow fetching and executing remote code fetched
from the repository in the Hub. Defaults to |
model_kwargs |
Optional[Dict[str, Any]]
|
additional dictionary of keyword arguments that will be passed to
the |
tokenizer |
Optional[str]
|
the tokenizer Hugging Face Hub repo id or a path to a directory containing
the tokenizer config files. If not provided, the one associated to the |
use_fast |
bool
|
whether to use a fast tokenizer or not. Defaults to |
chat_template |
Optional[str]
|
a chat template that will be used to build the prompts before
sending them to the model. If not provided, the chat template defined in the
tokenizer config will be used. If not provided and the tokenizer doesn't have
a chat template, then ChatML template will be used. Defaults to |
device |
Optional[Union[str, int]]
|
the name or index of the device where the model will be loaded. Defaults
to |
device_map |
Optional[Union[str, Dict[str, Any]]]
|
a dictionary mapping each layer of the model to a device, or a mode
like |
token |
Optional[SecretStr]
|
the Hugging Face Hub token that will be used to authenticate to the Hugging
Face Hub. If not provided, the |
structured_output |
Optional[RuntimeParameter[OutlinesStructuredOutputType]]
|
a dictionary containing the structured output configuration or if more
fine-grained control is needed, an instance of |
use_magpie_template |
Optional[RuntimeParameter[OutlinesStructuredOutputType]]
|
a flag used to enable/disable applying the Magpie pre-query
template. Defaults to |
magpie_pre_query_template |
Optional[RuntimeParameter[OutlinesStructuredOutputType]]
|
the pre-query template to be applied to the prompt or
sent to the LLM to generate an instruction or a follow up user message. Valid
values are "llama3", "qwen2" or another pre-query template provided. Defaults
to |
Icon
:hugging:
Examples:
Generate text:
```python
from distilabel.llms import TransformersLLM
llm = TransformersLLM(model="microsoft/Phi-3-mini-4k-instruct")
llm.load()
# Call the model
output = llm.generate(inputs=[[{"role": "user", "content": "Hello world!"}]])
```
Source code in src/distilabel/llms/huggingface/transformers.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 |
|
model_name: str
property
¶
Returns the model name used for the LLM.
generate(inputs, num_generations=1, max_new_tokens=128, temperature=0.1, repetition_penalty=1.1, top_p=1.0, top_k=0, do_sample=True)
¶
Generates num_generations
responses for each input using the text generation
pipeline.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
List[StandardInput]
|
a list of inputs in chat format to generate responses for. |
required |
num_generations |
int
|
the number of generations to create per input. Defaults to
|
1
|
max_new_tokens |
int
|
the maximum number of new tokens that the model will generate.
Defaults to |
128
|
temperature |
float
|
the temperature to use for the generation. Defaults to |
0.1
|
repetition_penalty |
float
|
the repetition penalty to use for the generation. Defaults
to |
1.1
|
top_p |
float
|
the top-p value to use for the generation. Defaults to |
1.0
|
top_k |
int
|
the top-k value to use for the generation. Defaults to |
0
|
do_sample |
bool
|
whether to use sampling or not. Defaults to |
True
|
Returns:
Type | Description |
---|---|
List[GenerateOutput]
|
A list of lists of strings containing the generated responses for each input. |
Source code in src/distilabel/llms/huggingface/transformers.py
get_last_hidden_states(inputs)
¶
Gets the last hidden_states
of the model for the given inputs. It doesn't
execute the task head.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
List[StandardInput]
|
a list of inputs in chat format to generate the embeddings for. |
required |
Returns:
Type | Description |
---|---|
List[HiddenState]
|
A list containing the last hidden state for each sequence using a NumPy array |
List[HiddenState]
|
with shape [num_tokens, hidden_size]. |
Source code in src/distilabel/llms/huggingface/transformers.py
load()
¶
Loads the model and tokenizer and creates the text generation pipeline. In addition, it will configure the tokenizer chat template.
Source code in src/distilabel/llms/huggingface/transformers.py
prepare_input(input)
¶
Prepares the input (applying the chat template and tokenization) for the provided input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
StandardInput
|
the input list containing chat items. |
required |
Returns:
Type | Description |
---|---|
str
|
The prompt to send to the LLM. |