mellea.formatters.granite.intrinsics.util
Common utility functions for this package.
Functions
FUNC make_config_dict
make_config_dict(config_file: str | pathlib.Path | None = None, config_dict: dict | None = None) -> dict
Create a configuration dictionary from YAML file or dict.
This function is not a public API and is not intended for use outside this library.
Common initialization code for reading YAML config files in factory classes. Also parses JSON fields.
Args:
config_file: Path to a YAML configuration file. Exactly one ofconfig_fileandconfig_dictmust be provided.config_dict: Pre-parsed configuration dict (fromyaml.safe_load()). Exactly one ofconfig_fileandconfig_dictmust be provided.
Returns:
- Validated configuration dict with optional fields set to
Noneand JSON - string fields parsed to Python objects.
Raises:
ValueError: If both or neither ofconfig_fileandconfig_dictare provided, if a required field is missing, if an unexpected top-level field is encountered, or if a JSON field cannot be parsed.
FUNC adapter_subpath
adapter_subpath() -> str
Return the Hugging Face Hub subpath where an intrinsic's adapter lives.
Encapsulates the layout convention used by the Granite Intrinsics Library and
related repositories so callers don't replicate the rules. Both obtain_lora
and out-of-tree consumers (e.g. drift checks in tests) should call this function
rather than building the path themselves.
Args:
intrinsic_name: Short name of the intrinsic model, such as"certainty".target_model_name: Name of the base model for the adapter, or"any"for a model-agnostic prompt fallback. May be a raw HF repo ID; canonical normalization is applied ("any"is not normalized and passes through).repo_id: Hugging Face Hub repository containing the adapter collection. Used to select between old and new directory layouts.adapter_type: Adapter-type slot —"lora","alora", or"prompt".
Returns:
- Subpath relative to the repo root, e.g.
"certainty/granite-4.1-3b/lora"or "answerability/any/prompt".
Raises:
ValueError:adapter_typeis not a validAdapterTypevalue.
FUNC obtain_lora
obtain_lora() -> pathlib.Path
Download and cache an adapter that implements an intrinsic.
Downloads a LoRA, aLoRA, or prompt adapter from a collection of adapters that follow the same layout as the Granite Intrinsics Library. Caches the downloaded adapter files on local disk.
Args:
intrinsic_name: Short name of the intrinsic model, such as"certainty".target_model_name: Name of the base model for the adapter, or"any"for a model-agnostic prompt fallback.repo_id: Hugging Face Hub repository containing a collection of LoRA and/or aLoRA adapters for intrinsics.revision: Git revision of the repository to download from.cache_dir: Local directory to use as a cache (Hugging Face Hub format), orNoneto use the default location.file_glob: Only files matching this glob will be downloaded to the cache.adapter_type: Adapter-type slot —"lora","alora", or"prompt".
Returns:
- Full path to the local copy of the specified adapter, suitable for
- passing to commands that serve the adapter.
Raises:
AdapterNotFoundError: If the specified intrinsic adapter cannot be found in the Hugging Face Hub repository at the expected path.
FUNC obtain_io_yaml
obtain_io_yaml() -> pathlib.Path
Download cached io.yaml configuration file for an intrinsic.
Downloads an io.yaml configuration file for an intrinsic
with a model repository that follows the format of the
Granite Intrinsics Library if one is not
already in the local cache.
Args:
intrinsic_name: Short name of the intrinsic model, such as"certainty".target_model_name: Name of the base model for the adapter, or"any"for a model-agnostic prompt fallback.repo_id: Hugging Face Hub repository containing a collection of LoRA and/or aLoRA adapters for intrinsics.revision: Git revision of the repository to download from.cache_dir: Local directory to use as a cache (Hugging Face Hub format), orNoneto use the default location.adapter_type: Adapter-type slot —"lora","alora", or"prompt".
Returns:
- Full path to the local copy of the
io.yamlfile, suitable for passing to IntrinsicsRewriter.
FUNC resolve_prompt_io_yaml
resolve_prompt_io_yaml() -> tuple[pathlib.Path, bool]
Resolve a prompt fallback's io.yaml, preferring a model-specific one.
Tries the model-specific prompt (\{intrinsic\}/\{base_model_name\}/prompt) first,
then the model-agnostic fallback (\{intrinsic\}/any/prompt). Advances from the
model-specific to the any slot only on the not-found AdapterNotFoundError;
any other error (network/auth/bad-revision) from the model-specific fetch
propagates.
Args:
intrinsic_name: Short name of the intrinsic model, such as"certainty".base_model_name: Name of the base model to look for a model-specific prompt under.repo_id: Hugging Face Hub repository containing the prompt collection.revision: Git revision of the repository to download from.cache_dir: Local directory to use as a cache (Hugging Face Hub format), orNoneto use the default location.
Returns:
- A
(path, used_any)tuple: the localio.yamlpath, andTrueif the - model-agnostic
anyfallback was used rather than a model-specific prompt - (callers warn on
True, since ananyprompt is sub-par).
Raises:
AdapterNotFoundError: Neither a model-specific prompt nor theanyfallback exists.
Classes
CLASS AdapterNotFoundError
Raised when no adapter or prompt io.yaml exists at the expected repository path.
Subclasses ValueError so a broad except ValueError still catches it, while
callers can catch AdapterNotFoundError specifically to treat "not found" as a
fallback signal without also swallowing other ValueErrors.