Solon v3.5.1

image - 使用复杂提示语

</> markdown

(v3.5.1 后支持)有些生成模型的提示语会是一个结构体,此时就需要使用 ImagePrompt 接口。可以快速使用,或者定制强类型实体。

1、使用快速方法

使用阿里百炼调整一张图片,把它转成法国绘本风格

String apiUrl = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis";
ImageModel imageModel = ImageModel.of(apiUrl)
        .apiKey(apiKey)
        .model("wanx2.1-imageedit")
        .headerSet("X-DashScope-Async", "enable")
        .build();

ImageResponse resp = imageModel.prompt(ImagePrompt.ofKeyValues(
                "function", "stylization_all",
                "prompt", "转换成法国绘本风格",
                "base_image_url", "http://wanx.alicdn.com/material/20250318/stylization_all_1.jpeg")
        )
        .options(o -> o.optionAdd("n", 1))
        .call();

log.warn("{}", resp.getImage().getUrl());

2、定制提示语结构体

定义提示语结构体

@Builder
class ImageEditPrompt implements ImagePrompt {
    private String function;
    private String prompt;
    private String base_image_url;

    @Override
    public Map<String, Object> toMap() {
        return Utils.asMap("function", function, "prompt", prompt, "base_image_url", base_image_url);
    }
}

应用示例

String apiUrl = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis";
ImageModel imageModel = ImageModel.of(apiUrl)
        .apiKey(apiKey)
        .model("wanx2.1-imageedit")
        .headerSet("X-DashScope-Async", "enable")
        .build();

ImageResponse resp = imageModel.prompt(ImageEditPrompt.builder()
            .function("stylization_all")
            .prompt("转换成法国绘本风格")
            .base_image_url("http://wanx.alicdn.com/material/20250318/stylization_all_1.jpeg")
            .build())
        .options(o -> o.optionAdd("n", 1))
        .call();

log.warn("{}", resp.getImage().getUrl());