Solon AI 开发学习18 - generate - 生成示例参考

GenerateModel 是非常自由的一个接口,本质是组装一个 http post 请求,并尝试解析响应内容。但仍然有大量的 ai 模型无法覆盖(花样太多了),可使用 HttpUtils 直接请求。

一般涉及图片、声音、视频的生成,都会比较慢。所以大多平台大多是异步的,生成结果一般会是个 taskUrl 拼装的地址(也会有 base64 输出)。

1、示例:输入文本,生成图片

java 复制代码
import org.junit.jupiter.api.Test;
import org.noear.solon.ai.generate.GenerateModel;
import org.noear.solon.ai.generate.GenerateResponse;

@Test
public void case1_text2image() throws IOException {
    //生成图片
    String apiUrl = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis";
    String taskUrl = "https://dashscope.aliyuncs.com/api/v1/tasks/";
    
    GenerateModel generateModel = GenerateModel.of(apiUrl)
            .apiKey(apiKey)
            .taskUrl(taskUrl)
            .model("wanx2.1-t2i-turbo")
            .headerSet("X-DashScope-Async", "enable")
            .build();

    //一次性返回
    GenerateResponse resp = generateModel.prompt("a white siamese cat")
            .options(o -> o.size("1024x1024"))
            .call();

    //打印消息
    log.info("{}", resp.getContent());
    assert resp.getContent().getUrl() != null;
    assert resp.getContent().getUrl().startsWith("https://");
}

2、示例:输入图片,生成新图片(调整图片)

java 复制代码
import org.junit.jupiter.api.Test;
import org.noear.solon.ai.generate.GenerateModel;
import org.noear.solon.ai.generate.GenerateResponse;

@Test
public void case2_image2image() throws IOException {
    //编辑图片
    String apiUrl = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis";
    String taskUrl = "https://dashscope.aliyuncs.com/api/v1/tasks/";
    
    GenerateModel generateModel = GenerateModel.of(apiUrl)
            .apiKey(apiKey)
            .taskUrl(taskUrl)
            .model("wanx2.1-imageedit")
            .headerSet("X-DashScope-Async", "enable")
            .build();

    GenerateResponse resp = generateModel.prompt(GeneratePrompt.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.getData());
    assert resp.getContent().getUrl() != null;
    assert resp.getContent().getUrl().startsWith("https://");
}

3、示例:输入文本,输出声音(音乐)

java 复制代码
import org.junit.jupiter.api.Test;
import org.noear.solon.ai.generate.GenerateModel;
import org.noear.solon.ai.generate.GenerateResponse;

@Test
public void case3_music() throws IOException {
    String apiUrl = "https://ai.gitee.com/v1/async/music/generations";
    String taskUrl = "https://ai.gitee.com/v1/task/";
   
    GenerateModel generateModel = GenerateModel.of(apiUrl)
            .apiKey(apiKey)
            .taskUrl(taskUrl)
            .model("ACE-Step-v1-3.5B")
            .build();

    //一次性返回
   GenerateResponse resp = generateModel.prompt(GeneratePrompt.ofKeyValues(
                        "prompt", "大海的哥",
                        "task", "text2music"
                ))
                .call();

    log.warn("{}", resp.getData());
    assert resp.getContent().getUrl() != null;
    assert resp.getContent().getUrl().startsWith("https://");
}

4、示例:输入文本,生成视频

java 复制代码
import org.junit.jupiter.api.Test;
import org.noear.solon.ai.generate.GenerateModel;
import org.noear.solon.ai.generate.GenerateResponse;

@Test
public void case4_video() throws IOException {
    //生成动画
    String apiUrl = "https://dashscope.aliyuncs.com/api/v1/services/aigc/video-generation/video-synthesis";
    String taskUrl = "https://dashscope.aliyuncs.com/api/v1/tasks/";
    
    GenerateModel generateModel = GenerateModel.of(apiUrl)
            .apiKey(apiKey)
            .taskUrl(taskUrl)
            .model("wan2.2-i2v-plus")
            .headerSet("X-DashScope-Async", "enable")
            .build();

    GenerateResponse resp = generateModel.prompt(GeneratePrompt.ofKeyValues(
                    "prompt", "一只猫在草地上奔跑",
                    "img_url", "https://cdn.translate.alibaba.com/r/wanx-demo-1.png")
            )
            .options(o -> o.optionAdd("resolution", "480P")
                    .optionAdd("prompt_extend", true))
            .call();

    log.warn("{}", resp.getData());
    assert resp.getContent().getUrl() != null;
    assert resp.getContent().getUrl().startsWith("https://");
}
相关推荐
张彦峰ZYF2 小时前
AI赋能原则4解读思考:AI 不是“可选的加分项”,而是重构生存方式的基础设施
人工智能·ai·ai赋能与落地
paopao_wu2 小时前
ComfyUI遇上Z-Image(3):文生图/图生图
人工智能·ai·文生图·图生图·comfyui·z-image·we
猿小猴子3 小时前
主流 AI IDE 之一的 Kiro 介绍
ide·ai·kiro
小霖家的混江龙3 小时前
Token 到底怎么来的? 一文读懂大模型分词的核心逻辑, 看完秒懂!
人工智能·python·llm
leo03083 小时前
Hugging Face多卡训练“假快”?一文讲透`per_device_train_batch_size`的“陷阱”
llm·dp·huggingface·ddp
吴佳浩3 小时前
什么?大模型部署需要多少显存你都不知道?
人工智能·llm·gpu
AI大模型13 小时前
小白 & 程序员速看!快速入行大模型应用开发的完整实战指南,建议收藏
程序员·llm·agent
阿正的梦工坊13 小时前
DreamGym:通过经验合成实现代理学习的可扩展化
人工智能·算法·大模型·llm
山顶夕景13 小时前
【LLM应用】Codex & Codex CLI使用
大模型·llm·ai编程