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://");
}
相关推荐
逛街的猫啊13 小时前
【AI 专栏】JSON-RPC
ai·rpc·json
小龙14 小时前
大模型训练全流程学习笔记
笔记·学习·ai·大模型
AC赳赳老秦14 小时前
企业级人工智能平台选型深度分析:天翼云 DeepSeek 与开源解决方案的部署考量与成本博弈
人工智能·elasticsearch·zookeeper·rabbitmq·github·时序数据库·deepseek
manjianghong8614 小时前
结合AI编码和VBA宏批量调整word2007文档中的多个图片
ai·ai应用·ai编码·ai助力word编辑
victory043114 小时前
后训练的起点 学术路线
chatgpt
中國龍在廣州14 小时前
2025,具身智能正在惩罚“持有者”
人工智能·深度学习·算法·自然语言处理·chatgpt
阿部多瑞 ABU14 小时前
第五章:林心
人工智能·ai·ai写作
Swizard15 小时前
数据不够代码凑?用 Albumentations 让你的 AI 模型“看”得更广,训练快 10 倍!
python·算法·ai·训练
CoderJia程序员甲15 小时前
GitHub 热榜项目 - 日榜(2025-12-27)
ai·开源·大模型·github·ai教程
Elastic 中国社区官方博客16 小时前
使用 LocalAI 和 Elasticsearch 构建本地 RAG 应用
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索