Functions
FUNC import_optional
extra_name: Package extra to suggest in the install hint (e.g.pip install granite_io[extra_name]).
FUNC nltk_check
feature_name: Name of the feature that requires NLTK, used in the error message.
ImportError: If thenltkpackage is not installed, re-raised with a descriptive message and installation instructions.
FUNC find_substring_in_text
substring: The string to search for.text: The string to search within.
- List of dicts with
begin_idxandend_idxfor each match found.
FUNC random_uuid
- Hexadecimal UUID string suitable for use as a unique identifier.
FUNC load_transformers_lora
transformers and peft are installed.
Args:
local_or_remote_path: Local directory path of the LoRA adapter.
- Tuple of
(model, tokenizer)wheremodelis the loaded LoRA model and tokenizeris the corresponding HuggingFace tokenizer.
ImportError: Ifpeftortransformerspackages are not installed.NotImplementedError: Iflocal_or_remote_pathdoes not exist locally (remote loading from the Hugging Face Hub is not yet implemented).
FUNC chat_completion_request_to_transformers_inputs
generate() call.
Args:
request: Request as parsed JSON or equivalent dataclass.tokenizer: HuggingFace tokenizer for the model. Only required if the request uses constrained decoding.model: HuggingFace model object. Only required if the request uses constrained decoding.constrained_decoding_prefix: Optional generation prefix to append to the prompt.
- Tuple of
(generate_input, other_input)wheregenerate_inputcontains - kwargs to pass directly to
generate()andother_inputcontains - additional parameters for
generate_with_transformers.
ImportError: Iftorch,transformers, orxgrammarpackages are not installed (the latter only when constrained decoding is used).TypeError: Iftokenizer.apply_chat_template()returns an unexpected type.ValueError: If padding or end-of-sequence token IDs cannot be determined from the tokenizer, or if a constrained-decoding request is made without passing atokenizerormodelargument.
FUNC generate_with_transformers
generate() method of a
Transformers model and get back usable results, rolled into a single function.
There are quite a few extra steps.
Args:
tokenizer: HuggingFace tokenizer for the model, required at several stages of generation.model: Initialized HuggingFace model object.generate_input: Parameters to pass to thegenerate()method, usually produced bychat_completion_request_to_transformers_inputs().other_input: Additional kwargs produced bychat_completion_request_to_transformers_inputs()for aspects of the original request that Transformers APIs don’t handle natively.
- A chat completion response in OpenAI format.