工业互联网平台2.0版本后端代码
houzhongjian
2025-05-29 41499fd3c28216c1526a72b10fa98eb8ffee78cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.iailab.framework.ai.image;
 
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.qianfan.QianFanImageModel;
import org.springframework.ai.qianfan.QianFanImageOptions;
import org.springframework.ai.qianfan.api.QianFanImageApi;
 
import static com.iailab.framework.ai.image.StabilityAiImageModelTests.viewImage;
 
// TODO @Iailab:百度千帆 API 提供了 V2 版本,目前 Spring AI 不兼容,可关键 <https://github.com/spring-projects/spring-ai/issues/2179> 进展
 
/**
 * {@link QianFanImageModel} 集成测试类
 */
public class QianFanImageTests {
 
    private final QianFanImageModel imageModel = new QianFanImageModel(
            new QianFanImageApi("qS8k8dYr2nXunagK4SSU8Xjj", "pHGbx51ql2f0hOyabQvSZezahVC3hh3e")); // 密钥
 
    @Test
    @Disabled
    public void testCall() {
        // 准备参数
        // 只支持 1024x1024、768x768、768x1024、1024x768、576x1024、1024x576
        QianFanImageOptions imageOptions = QianFanImageOptions.builder()
                .model(QianFanImageApi.ImageModel.Stable_Diffusion_XL.getValue())
                .width(1024).height(1024)
                .N(1)
                .build();
        ImagePrompt prompt = new ImagePrompt("good", imageOptions);
 
        // 方法调用
        ImageResponse response = imageModel.call(prompt);
        // 打印结果
        String b64Json = response.getResult().getOutput().getB64Json();
        System.out.println(response);
        viewImage(b64Json);
    }
 
}