Utils
Defines utilities for gllm_inference.
SizeUnit
Bases: IntEnum
Defines the valid size units.
base64_to_bytes(value, *, allowed_mimetypes=DEFAULT_BASE64_ALLOWED_MIMETYPES)
Decode a base64 string to bytes based on allowed MIME type.
The conversion steps are as follows
- The function first attempts to decode the given string from base64.
- If decoding succeeds, it checks the MIME type of the decoded content.
- When the MIME type matches one of the allowed patterns (e.g.,
"image/*"), the raw bytes are returned. Otherwise, the original string is returned unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Input data to decode. |
required |
allowed_mimetypes
|
tuple[str, ...]
|
MIME type prefixes that are allowed to be decoded into bytes. Defaults to ("image/", "audio/", "video/*"). |
DEFAULT_BASE64_ALLOWED_MIMETYPES
|
Returns:
| Type | Description |
|---|---|
str | bytes
|
str | bytes: Base64-encoded string or raw bytes if MIME type is allowed; otherwise returns original string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input is not a string. |
get_size(obj, unit=SizeUnit.BYTES)
Get the pickle-serialized size of an object in the given unit.
Note
This measures the size of the object when serialized using pickle, NOT its in-memory footprint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to get the size of. |
required |
unit
|
SizeUnit
|
The unit to get the size in. Defaults to SizeUnit.BYTES. |
BYTES
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The size of the object in the given mode. |
get_value_repr(value)
staticmethod
Get the string representation of a value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to get the string representation of. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The string representation of the value. |
load_langchain_model(model_class_path, model_name, model_kwargs)
Loads the LangChain's model instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class_path
|
str
|
The path to the LangChain's class, e.g. "langchain_openai.ChatOpenAI". |
required |
model_name
|
str
|
The model name. |
required |
model_kwargs
|
dict[str, Any]
|
The additional keyword arguments. |
required |
Returns:
| Type | Description |
|---|---|
BaseChatModel | Embeddings
|
BaseChatModel | Embeddings: The LangChain's model instance. |
parse_model_data(model)
Parses the model data from LangChain's BaseChatModel or Embeddings instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
BaseChatModel | Embeddings
|
The LangChain's BaseChatModel or Embeddings instance. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
dict[str, str]: The dictionary containing the model name and path. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the model name is not found in the model data. |
validate_string_enum(enum_type, value)
Validates that the provided value is a valid string enum value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enum_type
|
type[StrEnum]
|
The type of the string enum. |
required |
value
|
str
|
The value to validate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided value is not a valid string enum value. |