mistral_common.tokens.tokenizers.multimodal
ImageChunk(**data)
Bases: BaseContentChunk
Image chunk.
Attributes:
Name | Type | Description |
---|---|---|
image |
SerializableImage
|
The image to be sent to the model. |
Examples:
>>> from PIL import Image
>>> image_chunk = ImageChunk(image=Image.new('RGB', (200, 200), color='blue'))
Source code in .venv/lib/python3.13/site-packages/pydantic/main.py
from_openai(openai_chunk)
classmethod
Converts the OpenAI chunk to the Mistral format.
Source code in src/mistral_common/protocol/instruct/messages.py
to_openai()
Converts the chunk to the OpenAI format.
Source code in src/mistral_common/protocol/instruct/messages.py
ImageConfig(image_patch_size, max_image_size, spatial_merge_size=1)
dataclass
Configuration for the image tokenizers.
ImageEncoder(image_config, special_ids)
Image encoder for the image tokenizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_config
|
ImageConfig
|
Configuration for the image tokenizer. |
required |
special_ids
|
SpecialImageIDs
|
Special image tokens ids. |
required |
Source code in src/mistral_common/tokens/tokenizers/image.py
__call__(content)
Converts an image chunk to an image encoding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content
|
Union[ImageChunk, ImageURLChunk]
|
image chunk to be converted. |
required |
Returns:
Type | Description |
---|---|
ImageEncoding
|
Image encoding. |
Source code in src/mistral_common/tokens/tokenizers/image.py
ImageEncoding(tokens, image)
dataclass
ImageURLChunk(**data)
Bases: BaseContentChunk
Image URL chunk.
Attributes:
Name | Type | Description |
---|---|---|
image_url |
Union[ImageURL, str]
|
The URL of the image or a base64 encoded image to be sent to the model. |
Examples:
Source code in .venv/lib/python3.13/site-packages/pydantic/main.py
from_openai(openai_chunk)
classmethod
Converts the OpenAI chunk to the Mistral format.
Source code in src/mistral_common/protocol/instruct/messages.py
to_openai()
Converts the chunk to the OpenAI format.
Source code in src/mistral_common/protocol/instruct/messages.py
SpecialImageIDs(img, img_break, img_end)
dataclass
download_image(url)
Download an image from a URL and return it as a PIL Image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
The URL of the image to download. |
required |
Returns:
Type | Description |
---|---|
Image
|
The downloaded image as a PIL Image object. |
Source code in src/mistral_common/image.py
image_from_chunk(chunk)
Get a serializable image from a chunk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chunk
|
Union[ImageURLChunk, ImageChunk]
|
The chunk to get the image from. |
required |
Returns:
Type | Description |
---|---|
SerializableImage
|
The image as a PIL Image object. |
Source code in src/mistral_common/tokens/tokenizers/image.py
is_cv2_installed()
normalize(np_image, mean, std)
Normalize a tensor image with mean and standard deviation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
np_image
|
ndarray
|
Image to be normalized. |
required |
mean
|
Tuple[float, float, float]
|
Mean for each channel. |
required |
std
|
Tuple[float, float, float]
|
Standard deviation for each channel. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Normalized image with shape (C, H, W). |
Source code in src/mistral_common/tokens/tokenizers/image.py
transform_image(image, new_size)
Transform an image to a numpy array with the given size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
Image
|
Image to be transformed. |
required |
new_size
|
Tuple[int, int]
|
New size of the image. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Transformed image with shape (C, H, W). |