【代码优化】AI:完善 StabilityAiImageModelTests 单测,方便大家快速体验

This commit is contained in:
YunaiV 2024-07-06 22:34:59 +08:00
parent 6e64ae774e
commit c5ed07a091
5 changed files with 74 additions and 11 deletions

View File

@ -20,7 +20,7 @@
<description>
ai 模块下,接入 LLM 大模型,支持聊天、绘图、音乐、写作、思维脑图等功能。
目前已接入各种模型,不限于:
国内:通义千问、文心一言、讯飞星火
国内:通义千问、文心一言、讯飞星火、智谱 GLM、DeepSeek
国外OpenAI、Ollama、Midjourney、StableDiffusion、Suno
</description>

View File

@ -14,7 +14,7 @@
<description>
ai 模块下,接入 LLM 大模型,支持聊天、绘图、音乐、写作、思维脑图等功能。
目前已接入各种模型,不限于:
国内:通义千问、文心一言、讯飞星火
国内:通义千问、文心一言、讯飞星火、智谱 GLM、DeepSeek
国外OpenAI、Ollama、Midjourney、StableDiffusion、Suno
</description>

View File

@ -1,13 +1,10 @@
/**
* https://github.com/spring-projects/spring-ai 拷贝
* AI 大模型组件基于 Spring AI 拓展
*
* 最大目的适配 JDK8 兼容性
*
* 包路径
* 1. chatparsermodelparser https://github.com/spring-projects/spring-ai/tree/main/spring-ai-core 拷贝
* 2. models 对标 https://github.com/spring-projects/spring-ai/tree/main/models 拷贝
* 2.1 xinghuo 讯飞星火自己实现
* 2.2 midjourney Midjourney API对接 https://github.com/novicezk/midjourney-proxy 实现
* 2.3 suno Suno API对接 https://github.com/gcui-art/suno-api 实现
* models 包路径
* 1. xinghuo 讯飞星火自己实现
* 2. deepseek 深度求索DeepSeek自己实现
* 3. midjourney Midjourney API对接 https://github.com/novicezk/midjourney-proxy 实现
* 4. suno Suno API对接 https://github.com/gcui-art/suno-api 实现
*/
package cn.iocoder.yudao.framework.ai;

View File

@ -35,6 +35,7 @@ public class OpenAiImageModelTests {
// 方法调用
ImageResponse response = imageClient.call(prompt);
// 打印结果
System.out.println(response);
}

View File

@ -0,0 +1,65 @@
package cn.iocoder.yudao.framework.ai.image;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.thread.ThreadUtil;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.ai.image.ImageOptions;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.ai.stabilityai.StabilityAiImageModel;
import org.springframework.ai.stabilityai.api.StabilityAiApi;
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.TimeUnit;
/**
* {@link StabilityAiImageModel} 集成测试类
*
* @author fansili
*/
public class StabilityAiImageModelTests {
private final StabilityAiApi imageApi = new StabilityAiApi(
"sk-e53UqbboF8QJCscYvzJscJxJXoFcFg4iJjl1oqgE7baJETmx");
private final StabilityAiImageModel imageClient = new StabilityAiImageModel(imageApi);
@Test
@Disabled
public void testCall() {
// 准备参数
ImageOptions options = OpenAiImageOptions.builder()
.withModel("stable-diffusion-v1-6")
.withHeight(256).withWidth(256)
.build();
ImagePrompt prompt = new ImagePrompt("great wall", options);
// 方法调用
ImageResponse response = imageClient.call(prompt);
// 打印结果
String b64Json = response.getResult().getOutput().getB64Json();
System.out.println(response);
viewImage(b64Json);
}
public static void viewImage(String b64Json) {
// 创建一个 JFrame
JFrame frame = new JFrame("Byte Image Display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
// 创建一个 JLabel 来显示图片
byte[] imageBytes = Base64.decode(b64Json);
JLabel label = new JLabel(new ImageIcon(imageBytes));
// JLabel 添加到 JFrame
frame.getContentPane().add(label, BorderLayout.CENTER);
// 显示 JFrame
frame.setVisible(true);
ThreadUtil.sleep(1, TimeUnit.HOURS);
}
}