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://");
}
相关推荐
chaors14 分钟前
DeepResearchSystem 0x06:LLM as Judge
llm·agent·ai编程
城管不管3 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
武子康4 小时前
GPT-5.6 Luna 降价 80%:Agent 真正该重算的是单次成功成本
人工智能·chatgpt·llm
Summer-Bright5 小时前
深度 | Agent框架大洗牌:AutoGen退场后的新秩序
人工智能·ai·语言模型·ai软件
土星云SaturnCloud5 小时前
抽水蓄能设备智慧运营:基于土星云边缘计算实现机组全生命周期预测性维护
服务器·人工智能·ai·边缘计算
AI大模型-小华7 小时前
Codex 反复重试仍完不成任务?判断 ChatGPT Plus 是否需要调整到 Pro
人工智能·chatgpt·ai编程·codex·开发效率·chatgpt plus·chatgpt pro
wang_yb7 小时前
用相关性分析消除“冗余特征”
ai·databook
阿文和她的Key8 小时前
GPT-5.6 降价后, API 账单的三层漏斗该怎么拆
人工智能·gpt·ai·chatgpt
总捣什么乱18 小时前
ChatGpt 系列文章——编码与智能体
人工智能·chatgpt
thesky1234569 小时前
27届大模型岗面试准备(十四):多模态大模型 VLM——从视觉编码器到多模态推理的完整链路
人工智能·ai·大模型