Implementation:PacktPublishing LLM Engineers Handbook HfApi Model Info
Overview
HfApi Model Info implements the Principle:PacktPublishing_LLM_Engineers_Handbook_Model_Registry_Validation principle by querying the HuggingFace Hub API to verify model existence before evaluation, returning a fallback default when the requested model is not found.
| Aspect | Detail |
|---|---|
| Implementation Name | HfApi Model Info |
| Workflow | Model_Evaluation |
| Type | Wrapper Doc (HuggingFace Hub) |
| Source File | llm_engineering/model/evaluation/evaluate.py (Lines 168–206) |
| Implements | Principle:PacktPublishing_LLM_Engineers_Handbook_Model_Registry_Validation |
API Signature
def check_if_huggingface_model_exists(model_id: str, default_value: str) -> str
Internally calls:
HfApi().model_info(model_id)
Key Code
def check_if_huggingface_model_exists(model_id: str, default_value: str) -> str:
api = HfApi()
try:
api.model_info(model_id)
return model_id
except RepositoryNotFoundError:
logger.warning(
f"Model '{model_id}' does not exist on Hugging Face. "
f"Using default model: '{default_value}'"
)
return default_value
Imports
from huggingface_hub import HfApi
from huggingface_hub.utils import RepositoryNotFoundError
Inputs
| Parameter | Type | Description |
|---|---|---|
model_id |
str |
The HuggingFace Hub model identifier to check (e.g., "pauliusztin/llm-twin-7b")
|
default_value |
str |
The fallback model identifier to use if the requested model does not exist (e.g., "mlabonne/TwinLlama-3.1-8B")
|
Outputs
| Return | Type | Description |
|---|---|---|
| Validated model ID | str |
Either the original model_id if it exists on HuggingFace Hub, or the default_value if the model was not found
|
Behavior
- An
HfApiclient is instantiated (uses the default authentication token from the environment or~/.huggingface/token) - The
model_info()method is called with the givenmodel_id, which performs a lightweight HTTP HEAD request to the Hub API - If the model exists: the method returns successfully and the original
model_idis returned to the caller - If the model does not exist: a
RepositoryNotFoundErroris raised, caught, and:- A warning is logged indicating the model was not found and the default is being used
- The
default_valueis returned instead
Usage Context
This function is called at the start of the evaluation pipeline, before any GPU-intensive work begins:
# Validate models before evaluation
model_id = check_if_huggingface_model_exists(
model_id=f"{workspace}/llm-twin-7b",
default_value="mlabonne/TwinLlama-3.1-8B"
)
dataset_model_id = check_if_huggingface_model_exists(
model_id=f"{workspace}/llm-twin-7b-results",
default_value="mlabonne/TwinLlama-3.1-8B-results"
)
Both the model weights and the results dataset are validated, each with their own fallback default.
External Dependencies
| Dependency | Purpose |
|---|---|
huggingface_hub |
Provides HfApi for querying Hub metadata and RepositoryNotFoundError for handling missing models
|
Error Handling
The function specifically catches RepositoryNotFoundError and treats it as a non-fatal condition by returning the default. Other exceptions (e.g., network errors, authentication failures) are not caught and will propagate up, which is intentional — those represent infrastructure failures that should halt the pipeline.
See Also
- Principle:PacktPublishing_LLM_Engineers_Handbook_Model_Registry_Validation — the principle this implements
- Implementation:PacktPublishing_LLM_Engineers_Handbook_VLLM_LLM_Generate — the downstream inference step that uses the validated model ID
- Implementation:PacktPublishing_LLM_Engineers_Handbook_Dataset_Push_To_Hub — the aggregation step that uses the validated results dataset ID
- Environment:PacktPublishing_LLM_Engineers_Handbook_VLLM_Evaluation_Environment