Shortcuts

format_content_with_images

torchtune.data.format_content_with_images(content: str, *, image_tag: str, images: List[PIL.Image.Image]) List[Dict[str, Any]][source]

给定一个原始文本字符串,按指定的image_tag进行分割,并形成字典列表,用于消息内容字段:

[
    {
        "role": "system" | "user" | "assistant",
        "content":
            [
                {"type": "image", "content": <PIL.Image.Image>},
                {"type": "text", "content": "This is a sample image."},
            ],
    },
    ...
]
Parameters:
  • 内容 (str) – 原始消息文本

  • image_tag (str) – 用于分割文本的字符串

  • images (List["PIL.Image.Image"]) – 用于内容中的图像列表

Raises:

ValueError – 如果图像数量与内容中的图像标签数量不匹配

示例

>>> content = format_content_with_images(
...     "<|image|>hello <|image|>world",
...     image_tag="<|image|>",
...     images=[<PIL.Image.Image>, <PIL.Image.Image>]
... )
>>> print(content)
[
    {"type": "image", "content": <PIL.Image.Image>},
    {"type": "text", "content": "hello "},
    {"type": "image", "content": <PIL.Image.Image>},
    {"type": "text", "content": "world"}
]
Returns:

用于消息内容字段的字典列表

Return type:

列表[字典[str, 任意]]