Skip to content

Utils

Defines utilities for gllm_inference.

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
  1. The function first attempts to decode the given string from base64.
  2. If decoding succeeds, it checks the MIME type of the decoded content.
  3. 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.

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.