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://");
}
相关推荐
阡之尘埃9 小时前
Python数据分析案例85——大模型微调全流程(SFT的LoRA微调)
人工智能·python·深度学习·语言模型·llm·微调·千问
剑客的茶馆9 小时前
开发者,运维怎样转行FDE?
运维·ai·开发·fde
俊哥V10 小时前
每日 AI 研究简报 · 2026-08-29
人工智能·ai
Orange_sparkle11 小时前
从 Docker 到 Doris:建立后端基础设施全景图
运维·docker·ai·容器·claude code
玉&心12 小时前
GraalVM 21 Native Image 编译踩坑全记录:从 class 初始化冲突到成功构建
ai·springboot·graalvm·rag
tachibana212 小时前
复杂任务怎么做的任务拆分?
人工智能·ai·大模型·llm·agent
tachibana212 小时前
ReAct、Plan-and-Execute、Reflection 三种范式有什么核心区别
人工智能·ai·大模型·llm·agent
尘中远12 小时前
7大开源Agent源码对比解读——性能设计
ai·开源·agent·codex·deepseek·harness
VIP_CQCRE12 小时前
用 Ace Data Cloud 开启 AI 能力商业化:推广平台,或打造自己的白标 AI 平台
ai·api·mcp·ace data cloud·白标平台
TechEdu20260612 小时前
[人工智能]豆包(Doubao):模型能力、应用架构与工程实践
人工智能·ai