跳转至

多模态内容

多模态内容指不同类型的内容,例如文本、图像、音频、视频和文件。 Koog允许您在user消息中向LLM发送图像、音频、视频和文件以及文本。 您可以通过使用Kotlin中的相应函数或Java中的方法将它们添加到user消息中:

  • image():附加图像(JPGPNGWebPGIF)。
  • audio():附加音频文件(MP3WAVFLAC)。
  • video():附加视频文件(MP4AVIMOV)。
  • file() / binaryFile() / textFile():附加文档(PDFTXT、MD等)。

每个函数或方法支持两种配置附件参数的方式,因此您可以:

  • 将URL或文件路径传递给函数或方法,它会自动处理附件参数。对于file()binaryFile()textFile(),您还必须提供MIME类型。
  • 创建ContentPart对象并将其传递给函数或方法,以自定义控制附件参数。

Note

多模态内容支持因LLM提供商而异。 请查阅提供商文档以了解支持的内容类型。

自动配置的附件

如果您将URL或文件路径传递给附件函数或方法,Koog会根据文件扩展名自动构建相应的附件参数。

包含文本消息和自动配置附件列表的user消息的一般格式如下:

user {
    +"描述这些图像:"

    image("https://example.com/test.png")
    image(Path("/path/to/image.png"))

    +"重点关注主要主体。"
}

ContentPartsBuilder partsBuilder = new ContentPartsBuilder();
partsBuilder.text("描述这些图像:");
partsBuilder.image("https://example.com/test.png");
partsBuilder.text("重点关注主要主体。");

Prompt prompt = Prompt.builder("image_analysis")
    .user(partsBuilder.build())
    .build();

在Kotlin中,+运算符将文本内容与附件一起添加到用户消息中。在Java中,使用ContentPartsBuildertext()方法。

自定义配置的附件

ContentPart接口允许您为每个附件单独配置参数。

所有附件都实现了ContentPart.Attachment接口。 您可以为每个附件创建特定实现的实例,配置其参数,并将其传递给Kotlin中的相应image()audio()video()file()函数,或Java中的方法。包含文本消息和自定义配置附件列表的 user 消息通用格式如下:

user {
    +"描述这张图片"
    image(
        ContentPart.Image(
            content = AttachmentContent.URL("https://example.com/capture.png"),
            format = "png",
            mimeType = "image/png",
            fileName = "capture.png"
        )
    )
}

Prompt prompt = Prompt.builder("custom_image")
    .user(List.of(
        new ContentPart.Text("描述这张图片"),
        new ContentPart.Image(
            new AttachmentContent.URL("https://example.com/capture.png"),
            "png",
            "image/png",
            "capture.png"
        )
    ))
    .build();

Koog 为每种媒体类型提供了以下实现 ContentPart.Attachment 接口的专用类:

所有 ContentPart.Attachment 类型均接受以下参数:| 名称 | 数据类型 | 必填 | 描述 | |------------|--------------------------------------------------------------------------------------------------------------------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | content | AttachmentContent | 是 | 所提供文件内容的来源。 | | format | 字符串 | 是 | 所提供文件的格式。例如 png。 | | mimeType | 字符串 | 仅适用于 ContentPart.File | 所提供文件的 MIME 类型。
对于 ContentPart.ImageContentPart.AudioContentPart.Video,默认为 <type>/<format>(例如 image/png)。
对于 ContentPart.File,必须显式提供。 | | fileName | 字符串? | 否 | 所提供文件的名称(包含扩展名)。例如 screenshot.png。 |

附件内容

AttachmentContent 接口的实现定义了作为输入提供给 LLM 的内容类型和来源:

混合附件除了在单独的提示或消息中提供不同类型的附件外,您还可以在单个 user() 消息中提供多种混合类型的附件:

val prompt = prompt("mixed_content") {
    system("You are a helpful assistant.")

    user {
        +"Compare the image with the document content."
        image(Path("/path/to/image.png"))
        binaryFile(Path("/path/to/page.pdf"), "application/pdf")
        +"Structure the result as a table"
    }
}

Prompt prompt = Prompt.builder("mixed_content_example")
.system("You are a helpful assistant.")
.user(List.of(
    new ContentPart.Text("Please analyze this image and the attached document."),
    new ContentPart.Image(
        new AttachmentContent.URL("https://example.com/image.png"),
        "png",
        "image/png",
        "image.png"
    ),
    new ContentPart.File(
        new AttachmentContent.URL("https://example.com/document.pdf"),
        "pdf",
        "application/pdf",
        "document.pdf"
    ),
    new ContentPart.Text("Summarize the differences.")
))
.build();

后续步骤

  • 如果您与单个 LLM 提供商合作,请使用 LLM 客户端运行提示。
  • 如果您与多个 LLM 提供商合作,请使用 提示执行器运行提示。